bgmnote

简易备注

  1. // ==UserScript==
  2. // @name bgmnote
  3. // @namespace https://github.com/hentaiPanda
  4. // @description 简易备注
  5. // @author niR
  6. // @version 0.0.3 pre 飞呀飞呀
  7. // @license MIT License
  8. // @encoding utf-8
  9. // @require http://code.jquery.com/jquery-2.1.1.js
  10. // @grant GM_setClipboard
  11. // @grant GM_getResourceText
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM_listValues
  15. // @grant GM_registerMenuCommand
  16. // @include http://bangumi.tv/group/topic/*
  17. // @include http://bgm.tv/group/topic/*
  18. // @include http://chii.in/group/topic/*
  19. // @include http://bangumi.tv/subject/topic/*
  20. // @include http://bgm.tv/subject/topic/*
  21. // @include http://chii.in/subject/topic/*
  22. // @include http://bangumi.tv/rakuen/topic/group/*
  23. // @include http://bgm.tv/rakuen/topic/group/*
  24. // @include http://chii.in/rakuen/topic/group/*
  25. // @include http://bangumi.tv/rakuen/topic/ep/*
  26. // @include http://bgm.tv/rakuen/topic/ep/*
  27. // @include http://chii.in/rakuen/topic/ep/*
  28. // @include http://bangumi.tv/blog/*
  29. // @include http://bgm.tv/blog/*
  30. // @include http://chii.in/blog/*
  31. // @include http://bangumi.tv/ep/*
  32. // @include http://bgm.tv/ep/*
  33. // @include http://chii.in/user/*
  34. // @include http://bangumi.tv/user/*
  35. // @include http://bgm.tv/user/*
  36. // @exclude http://chii.in/user/*/*
  37. // @exclude http://bangumi.tv/user/*/*
  38. // @exclude http://bgm.tv/user/*/*
  39. // ==/UserScript==
  40.  
  41.  
  42. var addBtn, addNotes, appendStyle, base64toutf8, confirmHiding, exportData, exportPlainText, formatNum, getTm, getVal, hideNote, importData, main, readFile, replaceNlc, saveFile, showNote, updateNote, utf8tobase64;
  43.  
  44. getVal = function() {
  45. var userlink, val;
  46. userlink = $('.headerAvatar > a.avatar').attr('href');
  47. val = GM_getValue(userlink, '');
  48. return val;
  49. };
  50.  
  51. updateNote = function() {
  52. var userlink, val;
  53. val = $('#bgmnote textarea').val();
  54. userlink = $('.headerAvatar > a.avatar').attr('href');
  55. GM_setValue(userlink, val);
  56. return hideNote();
  57. };
  58.  
  59. confirmHiding = function() {
  60. var answer, new_val, val;
  61. val = getVal();
  62. new_val = $('#bgmnote textarea').val();
  63. if (val === new_val) {
  64. return hideNote();
  65. } else {
  66. answer = confirm('备注似乎已经被更改,是否保存更改?');
  67. if (answer) {
  68. return updateNote();
  69. } else {
  70. return hideNote();
  71. }
  72. }
  73. };
  74.  
  75. showNote = function() {
  76. var val;
  77. $('#user_home .user_box').html(function(i, old) {
  78. var nvct;
  79. nvct = '<div id="bgmnote"><textarea name="bgmnote" class="reply" style="width:97.5%; height: 150px;"></textarea><div id="submitBtnO"><input id="updatenote" class="inputBtn" value="写好了" name="submit" type="submit">&nbsp;&nbsp; <a id="cancelbtn" href="javascript:;">取消</a></div></div>';
  80. return nvct + old;
  81. });
  82. val = getVal();
  83. $('#bgmnote textarea').val(val);
  84. $('body').off('click.show_n');
  85. $('body').on('click.textarea', '#bgmnotebtn', confirmHiding);
  86. $('body').on('click.textarea', '#cancelbtn', hideNote);
  87. return $('body').on('click.textarea', '#updatenote', updateNote);
  88. };
  89.  
  90. hideNote = function() {
  91. $('body').off('click.textarea');
  92. $('#bgmnote').remove();
  93. return $('body').on('click.show_n', '#bgmnotebtn', showNote);
  94. };
  95.  
  96. addBtn = function() {
  97. var new_btn, rr;
  98. rr = $('#headerProfile div.rr');
  99. new_btn = '<a href="javascript:;" class="chiiBtn" id="bgmnotebtn"><span>备注</span></a>';
  100. rr.html(rr.html() + new_btn);
  101. return $('body').on('click.show_n', '#bgmnotebtn', showNote);
  102. };
  103.  
  104. addNotes = function() {
  105. return $('a.avatar').each(function() {
  106. var note, userlink;
  107. if (this.parentNode.id.indexOf('post_') !== 0) {
  108. return 0;
  109. }
  110. userlink = this.getAttribute('href');
  111. note = GM_getValue(userlink, '');
  112. return this.setAttribute('title', note);
  113. });
  114. };
  115.  
  116. replaceNlc = function(str) {
  117. var _str, new_str;
  118. _str = str.replace(/>[\n\s]*</g, '><');
  119. new_str = _str.replace(/(>|^)([^<]*)\n([^>]*)(<|$)/g, function(s) {
  120. return s.replace(/\n/g, '<br>');
  121. });
  122. return new_str;
  123. };
  124.  
  125. utf8tobase64 = function(str) {
  126. return btoa(unescape(encodeURIComponent(str)));
  127. };
  128.  
  129. base64toutf8 = function(str) {
  130. return decodeURIComponent(escape(atob(str)));
  131. };
  132.  
  133. exportData = function() {
  134. var b64str, i, j, len, pairs, ref, value;
  135. pairs = [];
  136. ref = GM_listValues();
  137. for (j = 0, len = ref.length; j < len; j++) {
  138. i = ref[j];
  139. value = GM_getValue(i);
  140. if (!value.trim()) {
  141. continue;
  142. }
  143. pairs.push([utf8tobase64(i), utf8tobase64(value)].join(':'));
  144. }
  145. b64str = pairs.join(';');
  146. console.log(b64str);
  147. GM_setClipboard(b64str);
  148. return alert('已复制到剪贴板');
  149. };
  150.  
  151. exportPlainText = function() {
  152. var i, j, len, n, pairs, plain_text, ref, sub, subl, subr, sup, supl, supr, t, value;
  153. n = 3;
  154. sup = '♀';
  155. sub = '♂';
  156. t = n + 1;
  157. supl = Array(t).join(sup) + ' ';
  158. supr = ' ' + Array(t).join(sup);
  159. subl = Array(t).join(sub) + ' ';
  160. subr = ' ' + Array(t).join(sub);
  161. pairs = [];
  162. ref = GM_listValues();
  163. for (j = 0, len = ref.length; j < len; j++) {
  164. i = ref[j];
  165. value = GM_getValue(i);
  166. if (!value.trim()) {
  167. continue;
  168. }
  169. pairs.push([supl + i.slice(6) + supr, value, subl + i.slice(6) + subr].join('\n'));
  170. }
  171. plain_text = pairs.join('\n\n');
  172. console.log(plain_text);
  173. GM_setClipboard(plain_text);
  174. return alert('已复制到剪贴板');
  175. };
  176.  
  177. importData = function(b64str) {
  178. var error, i, j, key, len, pair, pairs, results, value;
  179. pairs = b64str.split(';');
  180. results = [];
  181. for (j = 0, len = pairs.length; j < len; j++) {
  182. i = pairs[j];
  183. pair = i.split(':');
  184. try {
  185. key = base64toutf8(pair[0]);
  186. value = base64toutf8(pair[1]);
  187. } catch (_error) {
  188. error = _error;
  189. console.log(error);
  190. continue;
  191. }
  192. results.push(GM_setValue(key, value));
  193. }
  194. return results;
  195. };
  196.  
  197. formatNum = function(n) {
  198. if (n < 10) {
  199. return "0" + n;
  200. }
  201. return "" + n;
  202. };
  203.  
  204. getTm = function() {
  205. var d, day, m, y;
  206. day = new Date();
  207. y = day.getFullYear();
  208. m = formatNum(day.getMonth() + 1);
  209. d = formatNum(day.getDate());
  210. return "" + y + m + d;
  211. };
  212.  
  213. saveFile = function() {
  214. var blob, day, j, key, len, pairs, ref, text, value;
  215. pairs = [];
  216. ref = GM_listValues();
  217. for (j = 0, len = ref.length; j < len; j++) {
  218. key = ref[j];
  219. value = GM_getValue(key);
  220. if (!value.trim()) {
  221. continue;
  222. }
  223. pairs.push([key, value]);
  224. }
  225. text = JSON.stringify(pairs);
  226. blob = new Blob([data], {
  227. type: "text/plain;charset=utf-8"
  228. });
  229. day = getTm();
  230. return saveAs(blob, "bgmnote_" + day + ".txt");
  231. };
  232.  
  233. readFile = function(evt) {
  234. var file, reader;
  235. if (!(window.File && window.FileReader && window.FileList)) {
  236. alert("浏览器不支持");
  237. return false;
  238. }
  239. file = evt.target.files[0];
  240. reader = new FileReader();
  241. reader.onload = function(e) {
  242. var j, len, pair, pairs, results, text;
  243. console.log(typeof e.target.result);
  244. text = e.target.result.trim();
  245. pairs = JSON.parse(text);
  246. results = [];
  247. for (j = 0, len = pairs.length; j < len; j++) {
  248. pair = pairs[j];
  249. results.push(GM_setValue(pair[0], pair[1]));
  250. }
  251. return results;
  252. };
  253. return reader.readAsText(file);
  254. };
  255.  
  256. appendStyle = function() {
  257. return $('head').append('<style type="text/css">\n .ui-helper-hidden {\n display: none;\n }\n .ui-helper-hidden-accessible {\n border: 0;\n clip: rect(0 0 0 0);\n height: 1px;\n margin: -1px;\n overflow: hidden;\n padding: 0;\n position: absolute;\n width: 1px;\n }\n .ui-helper-reset {\n margin: 0;\n padding: 0;\n border: 0;\n outline: 0;\n line-height: 1.3;\n text-decoration: none;\n font-size: 100%;\n list-style: none;\n }\n .ui-helper-clearfix:before,\n .ui-helper-clearfix:after {\n content: "";\n display: table;\n border-collapse: collapse;\n }\n .ui-helper-clearfix:after {\n clear: both;\n }\n .ui-helper-clearfix {\n min-height: 0; /* support: IE7 */\n }\n .ui-helper-zfix {\n width: 100%;\n height: 100%;\n top: 0;\n left: 0;\n position: absolute;\n opacity: 0;\n filter:Alpha(Opacity=0);\n }\n .ui-front {\n z-index: 100;\n }\n .ui-tooltip {\n padding: 8px;\n position: absolute;\n z-index: 9999;\n max-width: 500px;\n -webkit-box-shadow: 0 0 5px #aaa;\n box-shadow: 0 0 5px #aaa;\n }\n body .ui-tooltip {\n border-width: 2px;\n }\n</style>');
  258. };
  259.  
  260. main = function() {
  261. if (location.href.indexOf('user') > 0) {
  262. addBtn();
  263. return GM_registerMenuCommand('导出注释', exportPlainText);
  264. } else {
  265. appendStyle();
  266. addNotes();
  267. return $('a.avatar').tooltip({
  268. track: true,
  269. items: '[title]',
  270. content: function() {
  271. return replaceNlc(this.title);
  272. }
  273. });
  274. }
  275. };
  276.  
  277. main();