Y-marker

Sets up keyboard shortcuts for placing bookmarks and navigation inside webpage.

当前为 2014-06-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Y-marker
  3. // @namespace trespassersW
  4. // @description Sets up keyboard shortcuts for placing bookmarks and navigation inside webpage.
  5. // @include http://*
  6. // @include https://*
  7. // @include file://*
  8. // @exclude http*://www.google.com/reader/*
  9. // @version 2.014.0630
  10. // @license CC0 1.0 Universal (CC0 1.0)
  11. // @updated 2014-06-30
  12. // @released 2013-12-11
  13. // @run-at document-end
  14. // @grant GM_log
  15. // ==/UserScript==
  16. /* 14-06-30 patch
  17. * 13-12-12 click on msg removes all marks
  18. * 1.1 don't run in editable fields
  19. *
  20. */
  21. (function(){ "use strict";
  22. if(top!=self || !document || !document.body ) return;
  23. var W = window; // unsafeWindow??
  24. /* key kombinations to invoke the skript */
  25. var kShift = 1, kCtrl = 2, kAlt = 4, kWin = 8;
  26. var kJump = 0//kAlt;
  27. var kMark = kAlt//+kShift;
  28.  
  29. /* sec; 0 to disable; -1 status bar only */
  30. var tipShowtime = 2.2;
  31.  
  32. var kNobs = "-0123456789";
  33. var kKeys = {
  34. 173: 0, 48: 1, 49: 2, 50: 3, 51: 4, 52: 5, 53: 6, 54: 7, 55: 8, 56: 9, 57: 10,
  35. 189: 0, /* chrome, opera caps lock */
  36. 109: 0 /* opera */
  37. };
  38. var minus1="-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1";
  39.  
  40. function kNob(k){ return kNobs.charAt(k); }
  41.  
  42. var U=undefined, D=W.document;
  43. function _L(s) { 0 && console.log(s) };
  44.  
  45. var statMsg;
  46. function msg(t,k, y){
  47. statMsg = "";
  48. if(U!==k)
  49. statMsg += "["+kNob(k)+"] ";
  50. if(U!==y)
  51. statMsg += ""+pt(y)+"% ";
  52. statMsg += t+ (noCoo ? "\u2262 ": "");
  53. }
  54.  
  55. var locStor;
  56. var noCoo;
  57. // see SQlite manager -> %FFpath%\webappstore.sqlite -> find -> key -> contains Ym#
  58.  
  59. if(!locStor) try {
  60. locStor = W.localStorage;
  61. locStor.getItem("Ym#");
  62. } catch(e){ locStor =null; };
  63.  
  64. if(!locStor) try {
  65. noCoo="no cookies!!1";
  66. locStor = W.sessionStorage;
  67. locStor.getItem("Ym#");
  68. } catch(e){ locStor =null };
  69.  
  70. if(!locStor && U===W.YmStorage){
  71. noCoo= "cookies are disabled!!!11";
  72. W.YmStorage= {
  73. setItem: function(k,v) {this.s[k]=v},
  74. getItem: function(k) {return this.s[k]? this.s[k]: null},
  75. removeItem: function(k) {this.s[k]=null},
  76. s: {}
  77. };
  78. locStor=W.YmStorage;
  79. }
  80.  
  81. function pt(y) {
  82. try{
  83. y =Math.round(y*1000/(document.body.scrollHeight+1))/10;
  84. }catch(e){console.log("math\n"+e); return -1}
  85. return y;
  86. }
  87.  
  88. var Ym;
  89. var pz;
  90.  
  91. function ldYm(){
  92. var s= locStor.getItem(Ym);
  93. if(!s) s=minus1;
  94. pz=s.split(",")
  95. }
  96.  
  97. function svYm(){
  98. var s=pz.join(",");
  99. locStor.setItem(Ym,s)
  100. }
  101.  
  102. function jumpY(k){
  103. var y=pz[k];
  104. if(y > -1) {
  105. if( y!= scrollY ){
  106. pz[0] = scrollY;
  107. scroll(0,y);
  108. msg("jump",k,y);
  109. }else
  110. msg("here",k,y);
  111. return 1;
  112. }
  113. msg("none",k);
  114. return 0;
  115. }
  116.  
  117. function markY(k){
  118. pz[k]=scrollY;
  119. if(k>=1)
  120. svYm();
  121. msg("mark",k,pz[k]);
  122. return 1;
  123. }
  124.  
  125. var kMap= {16: kShift, 17:kCtrl, 18: kAlt, 91: kWin};
  126. var keyMod=0;
  127. var kkWin= (kMark | kJump) & kWin;
  128.  
  129. function klear(e) {
  130. var k= e.keyCode;
  131. if(!k) return;
  132. var km=kMap[k];
  133. if(km) {
  134. keyMod &= -1 ^ km;
  135. }
  136. }
  137.  
  138. function onKeydown(e) {
  139. var rc=0, k= e.keyCode;
  140. if(!k) return;
  141. // Don't run in input, select, textarea etc.
  142. var E = D.activeElement.tagName.toLowerCase();
  143. if (E == "input" || E == "select" || E == "textarea" ||
  144. (E=D.activeElement.contentEditable) == "true" ||
  145. E == "")
  146. { keyMod = 0; return; }
  147. var km =kMap[k]; // ff doesnt track metaKey. stsuko
  148. if(km) {
  149. keyMod |= km;
  150. if( kkWin && km === kWin ) //???
  151. e.preventDefault(), e.stopPropagation();
  152. return;
  153. }
  154. km=keyMod;
  155. if( U=== (k=kKeys[k]) ){ keyMod = 0; return; }
  156. if( km === kJump ) {
  157. rc = jumpY(k);
  158. }else if( km === kMark ) {
  159. rc = markY(k);
  160. }else {
  161. return;
  162. }
  163. statSay();
  164. if(rc) e.preventDefault(),e.stopPropagation();
  165. }
  166. function mk(p, t, id, s) {
  167. var e = D.createElement(t);
  168. e.id=id;
  169. e.style.cssText = s;
  170. return p.appendChild(e);
  171. };
  172.  
  173. /* RIP status bar replacement */
  174. var sb = mk(
  175. //D.body,
  176. D.documentElement, // 2014-06-30 ???!!1
  177. 'div', "Y-marker-userjs-inf",
  178. "position: fixed!important;\
  179. z-index: 214748!important;\
  180. top: 0px; right: 1px; bottom: auto; left: auto;\
  181. background: rgba(221,255,221,.75);\
  182. padding: 2px 3px 2px 8px; margin:0;\
  183. border: none;\
  184. border-radius: 12px 3px 3px 12px; \
  185. color: #131;\
  186. opacity: 1; display:none;\
  187. font: normal 12px/14px sans-serif !important;\
  188. text-shadow: #373 2px 2px 4px, #7F7 -2px -2px 4px;\
  189. cursor:no-drop;\
  190. "
  191. );
  192.  
  193.  
  194.  
  195. var tO;
  196. function onTout(){
  197. tO=0; sb.style.display="none";
  198. }
  199. function noTout(){
  200. if(tO) clearTimeout(tO), onTout();
  201. }
  202.  
  203. function statSay(t) {
  204. if(statMsg){
  205. if(tipShowtime)
  206. W.status=statMsg;
  207. if(tipShowtime>0){
  208. noTout();
  209. sb.innerHTML= statMsg;
  210. sb.style.display="block";
  211. tO=setTimeout(onTout, (t? t: tipShowtime)*1000);
  212. }
  213. }
  214. statMsg="";
  215. }
  216.  
  217. Ym="Ym#"+location.pathname;
  218. _L("load: "+location+"\n >> "+Ym);
  219.  
  220. ldYm();
  221. for(var k=1; k<2; k++){ /* pz.length */
  222. if(pz[k]>-1){
  223. scroll(0,pz[k]);
  224. msg('jump',k,pz[k]);
  225. statSay(5);
  226. break;
  227. }
  228. };
  229. /* click removes all */
  230. if(tipShowtime>0){
  231. sb.addEventListener("click",function (e) {
  232. locStor.removeItem(Ym);
  233. ldYm(); noTout();
  234. },false);
  235. };
  236.  
  237. if(noCoo) msg(noCoo), statSay(7);
  238.  
  239. W.addEventListener("keydown",onKeydown,false);
  240. W.addEventListener("keyup",klear,false);
  241. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址