stickyNotes

在你感兴趣的网页上添加便笺或笔记 按 "shift+鼠标左键"" 添加笔记//Press "shift + left key" to add sticky notes in any webpage wherever you like

目前为 2022-03-06 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name stickyNotes
  3. // @namespace
  4. // @version v1
  5. // @require https://code.jquery.com/jquery-3.6.0.min.js
  6. // @description 在你感兴趣的网页上添加便笺或笔记 按 "shift+鼠标左键"" 添加笔记//Press "shift + left key" to add sticky notes in any webpage wherever you like
  7. // @include /^https?://*/
  8. // @exclude http://www.baidu.com/*
  9. // @exclude /^https?://www\.google\./
  10. // @exclude /^https?://\.bing\./
  11. // @encoding utf-8
  12. // @grant GM.xmlHttpRequest
  13. // @grant GM_registerMenuCommand
  14. // @license MIT license
  15. // @run-at document-end
  16. // @namespace https://greasyfork.org/users/19216
  17. // ==/UserScript==
  18.  
  19. (function($) {
  20. $.fn.draggable = function(options) {
  21. var settings = $.extend({
  22. handle: undefined,
  23. msg: {},
  24. callfunction: function() {}
  25. }, options);
  26. var _eleFunc = function() {
  27. var x0, y0,
  28. ele = $(this),
  29. handle;
  30. handle = (settings.handle === undefined ? ele : ele.find(settings.handle).eq(0) === undefined ? ele : ele.find(settings.handle).eq(0));
  31. ele.css({
  32. position: "absolute"
  33. }); //make sure that the "postion" is "absolute"
  34. handle.bind('mousedown', function(e0) {
  35. handle.css({
  36. cursor: "move"
  37. }); //set the appearance of cursor
  38. x0 = ele.offset().left - e0.pageX; //*1
  39. y0 = ele.offset().top - e0.pageY; //*1
  40. $(document).bind('mousemove', function(e1) { //bind the mousemove event, caution:this event must be bind to "document"
  41. ele.css({
  42. left: x0 + e1.pageX,
  43. top: y0 + e1.pageY
  44. }); //this expression and the expression of *1 equal to "ele.origin_offset+mouse.current_offset-mouse.origin_offset"
  45. });
  46. $(document).one('mouseup', settings.msg, function(e) { //when the mouse up,unbind the mousemove event,bind only once
  47. settings.callfunction(e); //callback function
  48. $(document).unbind('mousemove');
  49. handle.css({
  50. cursor: "auto"
  51. });
  52. });
  53. });
  54.  
  55. // 從這裡開始
  56. };
  57. return this.each(_eleFunc);
  58. };
  59. this.request = function(mothed, url, param){ //网络请求
  60. return new Promise(function(resolve, reject){
  61. GM_xmlhttpRequest({
  62. url: url,
  63. method: mothed,
  64. data:param,
  65. onload: function(response) {
  66. var status = response.status;
  67. var playurl = "";
  68. if(status==200||status=='200'){
  69. var responseText = response.responseText;
  70. resolve({"result":"success", "data":responseText});
  71. }else{
  72. reject({"result":"error", "data":null});
  73. }
  74. }
  75. });
  76. })
  77. };
  78. })(jQuery);
  79. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  80.  
  81. var pNote = (function($) {
  82.  
  83.  
  84. function setCSS() {
  85. var css = ' <style type="text/css">\
  86. .wxz-noteDiv{\
  87. z-index:99;\
  88. box-shadow:0 0 9px rgba(0,0,0,.9);\
  89. background:#FFCC00;\
  90. width:200px;\
  91. position:absolute;\
  92. outline:0 none;\
  93. }\
  94. .wxz-noteDiv-head{\
  95. background:#FFCC00;\
  96. outline:0 none;\
  97. text-align:center;\
  98. width:200px;\
  99. line-height: initial;\
  100. font:13px/1.5 "微软雅黑",arial,serif;\
  101. }\
  102. .wxz-noteDiv-head-title{\
  103. text-align:center;\
  104. }\
  105. .wxz-noteDiv-head-close{\
  106. cursor:pointer;\
  107. position:absolute;\
  108. right:5px;\
  109. }\
  110. .wxz-noteDiv-content{\
  111. background:#FFFF66 ;\
  112. padding:5px 9px;\
  113. font:13px/1.5 "微软雅黑",arial,serif;\
  114. word-wrap:break-word;\
  115. min-height:200px;\
  116. outline:0 none;\
  117. text-align:left;\
  118. }\
  119. </style>';
  120. $('head:first').append(css);
  121. }
  122.  
  123. function getSTO() {
  124. //import localStorage.mysto to stotage
  125. var sto;
  126. if (localStorage.getItem('wxz-sto') !== null) {
  127. sto = JSON.parse(localStorage.getItem('wxz-sto'));
  128. } else {
  129. sto = {};
  130. }
  131. return sto;
  132. }
  133.  
  134. function upDateSTO(storage) {
  135.  
  136. localStorage.setItem('wxz-sto', JSON.stringify(storage)); //update localStorage.mysto with storage
  137.  
  138. }
  139.  
  140. function addNoteToStorage(keyName, x, y, text) {
  141. var path = {},
  142. temp = {},
  143. storage = getSTO(); //just call for add notes not for update or delete
  144. temp.keyName = keyName;
  145. temp.x = x;
  146. temp.y = y;
  147. temp.text = text;
  148. if (storage[location.pathname] !== undefined) {
  149. path = storage[location.pathname];
  150. }
  151. path[temp.keyName] = temp; //save notes to path
  152. storage[location.pathname] = path;
  153. upDateSTO(storage); //update local storage
  154.  
  155. path = null;
  156. temp = null;
  157. storage = null;
  158. }
  159.  
  160. function removeNoteFromStorage(keyName) {
  161.  
  162. var path = {},
  163. storage = getSTO();
  164. path = storage[location.pathname];
  165. if (path !== undefined) {
  166. delete path[keyName];
  167. if (Object.keys(path).length === 0) {
  168. delete storage[location.pathname];
  169. } else {
  170. storage[location.pathname] = path;
  171. }
  172. //update the localStorage.pathname
  173. upDateSTO(storage);
  174. }
  175. path = null;
  176. storage = null;
  177. }
  178.  
  179. function save(keyName) {
  180. var
  181. x, y, text,
  182. selector = "div[keyName='" + keyName + "']";
  183. x = $(selector).css('left');
  184. y = $(selector).css('top');
  185. text = $(selector).find('.wxz-noteDiv-content').html();
  186. addNoteToStorage(keyName, x, y, text);
  187. $(selector).find('.wxz-noteDiv-head-flag').text('');
  188. }
  189.  
  190. function del(keyName) {
  191. if (confirm("Do you like to delete the note?")) {
  192. var selector = "div[keyName='" + keyName + "']";
  193. $(selector).remove();
  194. removeNoteFromStorage(keyName);
  195. }
  196. }
  197.  
  198. function show(keyName, x, y, text) {
  199. var
  200. html = '<div class="wxz-noteDiv" >\
  201. <div class="wxz-noteDiv-head">\
  202. <nobr class="wxz-noteDiv-head-flag">*</nobr><nobr class="wxz-noteDiv-head-title"></nobr><nobr class="wxz-noteDiv-head-close">X</nobr>\
  203. </div>\
  204. <div class="wxz-noteDiv-content" contenteditable="true"></div>\
  205. </div>',
  206. thisNote,
  207. tempTime = new Date(parseInt(keyName, 10));
  208. thisNote = $(html);
  209. thisNote.appendTo('body:first');
  210. thisNote.attr('keyName', keyName);
  211. thisNote.find('.wxz-noteDiv-head-title').html(tempTime.toDateString());
  212. //set the coordinate
  213. thisNote.css({
  214. position: 'absolute',
  215. top: y,
  216. left: x
  217. });
  218. //write text to content
  219. thisNote.find('.wxz-noteDiv-content').html(text);
  220. // draggable;
  221. thisNote.draggable({
  222. handle: '.wxz-noteDiv-head',
  223. msg: {
  224. msg: keyName
  225. },
  226. callfunction: function(e) {
  227. save(e.data.msg);
  228. }
  229. }
  230. );
  231. //bind click event
  232. thisNote.find('.wxz-noteDiv-head-close').bind('click', {
  233. msg: keyName
  234. }, function(e) {
  235. del(e.data.msg);
  236. });
  237. //save when it lost focus
  238. thisNote.focusout({
  239. msg: keyName
  240. }, function(e) {
  241. save(e.data.msg);
  242. });
  243. thisNote.focusin(function() {
  244. thisNote.find('.wxz-noteDiv-head-flag').text('*');
  245. });
  246.  
  247. }
  248.  
  249. function loadNotes() {
  250. var
  251. noteList,
  252. storage = getSTO();
  253. if (storage[location.pathname] !== undefined) {
  254. noteList = storage[location.pathname];
  255. $.each(noteList, function(i, e) {
  256. show(e.keyName, e.x, e.y, e.text);
  257. });
  258. $('.wxz-noteDiv-head-flag').text('');
  259. }
  260. console.log('load notes successfully');
  261. }
  262.  
  263. // function showNotes() {
  264. // $("wxz-noteDiv").css({
  265. // 'display': 'inline'
  266. // });
  267. // }
  268.  
  269. // function closeNotes() {
  270. // $("wxz-noteDiv").css({
  271. // 'display': 'none'
  272. // });
  273. // }
  274.  
  275. return {
  276.  
  277. init: function() {
  278. // GM_registerMenuCommand("显示全部笔记...", showNotes, "s");
  279. // GM_registerMenuCommand("关闭笔记...", closeNotes, "c");
  280. setCSS();
  281. loadNotes();
  282. $("body").mousedown(function(e) {
  283. if (e.shiftKey) {
  284. var
  285. x = e.pageX,
  286. y = e.pageY,
  287. keyName = (new Date()).getTime();
  288. // keyName = e.timeStamp;//a bug in firefox since 2004
  289. e.preventDefault();
  290. show(keyName, x, y, '');
  291. console.log('new note');
  292. }
  293. });
  294. console.log('initialized successfully');
  295. }
  296.  
  297. };
  298.  
  299. })(jQuery);
  300.  
  301. pNote.init();