AutoSaveDiv

save and restores div.contenteditable texts

  1. // ==UserScript==
  2. // @name AutoSaveDiv
  3. // @namespace AutoSaveDiv
  4. // @description save and restores div.contenteditable texts
  5. // @include *
  6. // @exclude https://docs.google.com/*
  7. // @version 1.04
  8. // @grant GM.getValue
  9. // @grant GM.setValue
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
  13. // @author Dediggefedde
  14. // ==/UserScript==
  15.  
  16. var active=false;
  17. var isCtrl=false;
  18. var isShift=false;
  19. var isAlt=false;
  20. var name="";
  21. var altstil;
  22. var img=document.createElement("div");
  23.  
  24. // GM_registerMenuCommand("Clear stored Formular Fiels", function(){
  25. // var arrs=GM_listValues();
  26. // var i=0;
  27. // for (i = 0; i < cars.length; i++) {
  28. // GM_deleteValue(arrs[i]);
  29. // }
  30. // }, "c" );
  31.  
  32. img.addEventListener("contextmenu",function(e){
  33. restore(img.previousSibling);
  34. e.preventDefault();
  35. return false;
  36. },false);
  37.  
  38. img.addEventListener("click",function(e){
  39. e.preventDefault();
  40. if(e.which==1)listenView(img.previousSibling);
  41. // else if(e.which==3)
  42. return false;
  43. },true);
  44. function save(el){
  45. if(el.tagName=="DIV")
  46. GM.setValue(name,document.activeElement.innerHTML);
  47. else if(el.tagName=="TEXTAREA")
  48. GM.setValue(name,document.activeElement.value);
  49. }
  50. async function restore(el){
  51. if(el.tagName=="DIV")
  52. el.innerHTML=await GM.getValue(name);
  53. else if(el.tagName=="TEXTAREA")
  54. el.value=await GM.getValue(name);
  55. }
  56. async function getVal(n){
  57. return await GM.getValue(n);
  58. }
  59. function listenView(el){
  60. active=!active;
  61. if(active){
  62. img.style.backgroundColor="red";
  63. altstil=el.style.border;
  64. el.style.border="1px solid red";
  65. }else {
  66. img.style.backgroundColor="blue";
  67. el.style.border=altstil;
  68. }
  69. }
  70.  
  71. document.addEventListener("click",function(e){
  72. if(this==img||active)return false;
  73. if((document.activeElement.tagName=="DIV"&&document.activeElement.getAttribute("contenteditable"))||document.activeElement.tagName=="TEXTAREA"){
  74. (async function() {
  75. name=location.host+"::"+document.activeElement.tagName+"_"+location.pathname;
  76. img.setAttribute("style","width:20px;height:20px;position:relative;margin-top:-20px;opacity:0.5;border:1px solid blue;background-color:blue;border-top-right-radius:15px;");
  77. img.title="leftclick: de-/activate capturing text!\nrightclick: restore last text!\nstored:\n"+await GM.getValue(name);
  78. document.activeElement.parentNode.insertBefore(img, document.activeElement.nextSibling);
  79. })();
  80. }
  81. },true);
  82.  
  83. document.addEventListener("keydown",function(e){
  84. switch(e.which){
  85. case 17:isCtrl=true;break;
  86. case 16:isShift=true;break;
  87. case 18:isAlt=true;break;
  88. default:
  89. };
  90. },true);
  91.  
  92. document.addEventListener("keyup",function(e){
  93. switch(e.which){
  94. case 17:isCtrl=false;break;
  95. case 16:isShift=false;break;
  96. case 18:isAlt=false;break;
  97. case 68: //d
  98. if(isShift&&isCtrl&&isAlt){
  99. listenView(document.activeElement);
  100. }
  101. break;
  102. case 83: //s
  103. if(!active&&isShift&&isCtrl&&isAlt)restore(document.activeElement);
  104. break;
  105. default:
  106. };
  107. if(active){
  108. (async function(){
  109. save(document.activeElement);
  110. img.title="leftclick: de-/activate capturing text!\nrightclick: restore last text!\nstored:\n"+ (await GM.getValue(name));
  111. })();
  112. }
  113. },true);

QingJ © 2025

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