AutoSaveDiv

save and restores div.contenteditable texts

当前为 2015-02-20 提交的版本,查看 最新版本

  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.03
  8. // @grant GM_listValues
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @grant GM_registerMenuCommand
  12. // @grant GM_deleteValue
  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. function restore(el){
  51. if(el.tagName=="DIV")
  52. el.innerHTML=GM_getValue(name);
  53. else if(el.tagName=="TEXTAREA")
  54. el.value=GM_getValue(name);
  55. }
  56. function listenView(el){
  57. active=!active;
  58. if(active){
  59. img.style.backgroundColor="red";
  60. altstil=el.style.border;
  61. el.style.border="1px solid red";
  62. }else {
  63. img.style.backgroundColor="blue";
  64. el.style.border=altstil;
  65. }
  66. }
  67.  
  68. document.addEventListener("click",function(e){
  69. if(this==img||active)return false;
  70. if((document.activeElement.tagName=="DIV"&&document.activeElement.getAttribute("contenteditable"))||document.activeElement.tagName=="TEXTAREA"){
  71. name=location.host+"::"+document.activeElement.tagName+"_"+location.pathname;
  72. 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;");
  73. img.title="leftclick: de-/activate capturing text!\nrightclick: restore last text!\nstored:\n"+GM_getValue(name);
  74. document.activeElement.parentNode.insertBefore(img, document.activeElement.nextSibling);
  75. // console.log(name);
  76. }
  77. },true);
  78.  
  79. document.addEventListener("keydown",function(e){
  80. switch(e.which){
  81. case 17:isCtrl=true;break;
  82. case 16:isShift=true;break;
  83. case 18:isAlt=true;break;
  84. default:
  85. };
  86. },true);
  87.  
  88. document.addEventListener("keyup",function(e){
  89. switch(e.which){
  90. case 17:isCtrl=false;break;
  91. case 16:isShift=false;break;
  92. case 18:isAlt=false;break;
  93. case 68: //d
  94. if(isShift&&isCtrl&&isAlt){
  95. listenView(document.activeElement);
  96. }
  97. break;
  98. case 83: //s
  99. if(!active&&isShift&&isCtrl&&isAlt)restore(document.activeElement);
  100. break;
  101. default:
  102. };
  103. if(active){
  104. save(document.activeElement);
  105. img.title="leftclick: de-/activate capturing text!\nrightclick: restore last text!\nstored:\n"+GM_getValue(name);
  106. }
  107. },true);