Advertisement
rccharles

return asc command+s

Jan 15th, 2019
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    command key shortcuts 15Jan2019
  3.  
  4.    Intercepts command + s.  "Clicks" on save/edit button. Prevent command+s bubbling.  
  5.  
  6.      https://discussions.apple.com/thread/250080039
  7.  
  8.     interesting
  9.       https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
  10.       https://discussions.apple.com/thread/250079673?page=1
  11.    
  12.     from Greasemonkey hacks: tips & Tools for Remixing the web with Firefox
  13.     by Julien Couvreur
  14.     Code from book with his gracious permission
  15.  
  16.     Javascript: The Definitive Guide 6th Edition
  17.    
  18.     Avoids use of jquery.  Not working at this time.
  19.  
  20. https://stackoverflow.com/questions/2878983/capture-key-press-without-placing-an-input-element-on-the-page
  21. window.addEventListener('keydown', function (e) {
  22.   if (e.ctrlKey && e.keyCode == 90) {
  23.     // Ctrl + z pressed
  24.   }
  25. });
  26. MutationObserver
  27. Support for full regular expressions in include and exclude rules is also available.
  28. If the rule both starts and ends with a forward-slash (/) character,
  29. the contents inside those slashes are interpreted as a regular expression.
  30.  
  31. https://discussions.apple.com/thread/250075737
  32. */
  33. /* Separate options by a comma , */
  34. /* https://eslint.org/docs/rules/no-multi-spaces */
  35. /* eslint eqeqeq:   "ignoreEOLComments": true, "ignoreComments": true */
  36.  
  37. // ==UserScript==
  38. // @name         command key shortcuts 25Jan2019
  39. // @namespace   bubo-bubo/gmscripts
  40. // @description Implement save shortcut.
  41. // @include     /https://discussions\.apple\.com/+thread/+.*/
  42. // @version     25Jan2019
  43. // @grant       none
  44. // ==/UserScript==
  45.  
  46.  
  47. var debug                   = 2;   // 0 -- no debug
  48.                                    // 1 -- normal debug
  49.                                    // 2 -- intense debug
  50.  
  51. var aDate = new Date();
  52. console.log ("==++> command key shortcuts. " + aDate);
  53.  
  54.  
  55. // Setup once page is loaded.
  56. // modify thread content view behaviour (in post-load phase)
  57.  
  58. /* Most likely will be interactive.
  59.     The script will run after the main page is loaded, but before other resources
  60.     (images, style sheets, etc.) have loaded.
  61.     https://wiki.greasespot.net/Metadata_Block#.40run-at  */
  62. if (debug) console.log("document.readyState is " + document.readyState);
  63.  
  64. //  ready to roll? More stuff for later?
  65. if (document.readyState=="complete") {
  66.   console.log("rolling along")
  67.   // rolling along
  68.   pageLoadComplete()
  69. }
  70. else {
  71.   if (debug>=2)  console.log('adding listeners')
  72.   // wait out page loading.
  73.   window.addEventListener("load",
  74.                           pageLoadComplete,
  75.                           true);
  76. }
  77. // register event listeners
  78. // nothing like starting at the end ;-)
  79.  window.addEventListener('unload',
  80.         function(e)
  81.         {
  82.           if (debug) console.log('unloading.');
  83.           // may not be needed.  Wonder if that is bad?
  84.           window.removeEventListener("load",
  85.                                      pageLoadComplete,
  86.                                      true);
  87.           if (debug) console.log('removing e.type is '+ e.type + " arg is " +
  88.                                   arguments.callee);
  89.           // remove anonymous unload. [ Thus unload ]
  90.           window.removeEventListener(e.type,
  91.                                      arguments.callee,
  92.                                      true);
  93.         },
  94.         true);
  95.  
  96. // last set value is returned as return code.
  97. var done;
  98. done = 0;
  99.  
  100. // -----------------------------------------------------------------------------
  101. function pageLoadComplete() {
  102.   if (debug>=2)  console.log("All page resources finished loading!")
  103.   // find how many reply buttons there are.  Not used for nothing.
  104.   var theNodes = document.querySelectorAll(
  105.     "button.button.button-black:not(.hidden):not(.button-checked)")
  106.   spew ("theNodes",theNodes)
  107.   console.log("let's get started.")
  108.  
  109.   processPage()
  110.  
  111. }
  112.  
  113. /* --------------------------------------------------------------------- */
  114. /* main line code */
  115. function processPage() {
  116.   if (debug) console.log ("--> processPage.  ");
  117.  
  118.   /* Who knows, we might have to wait a bit */
  119.   /* <div class="editor-section"> */
  120.   //var theNode = document.querySelector("div.editor-section")
  121.   var theNode = document.querySelector("div#main-content")
  122.   console.log("theNode is ",theNode)
  123.   theNode.addEventListener("keypress",processKey,false);
  124.  
  125.   //theNode.addEventListener("keydown",processKey,false);
  126.   if (debug) console.log ("done with processPage  ");
  127. }  // end of processPage
  128.  
  129. // -----------------------------------------
  130. function processKey(e) {
  131.   console.log("  in processKey with event input e",e)
  132.  
  133.   console.log("  e.altKey is " + e.altKey)
  134.   console.log("  e.ctrlKey is " + e.ctrlKey)
  135.   console.log("  e.metaKey is " + e.metaKey)
  136.   console.log("  e.shiftKey is " + e.shiftKey)
  137. if ( e.charCode == 115){
  138.     console.log ("  s ")
  139.   }
  140. if (e.metaKey  ){
  141.     console.log ("  e.metaKey ")
  142.   }
  143.  
  144.   if (e.metaKey && e.charCode == 115 ){
  145.     console.log ("  found command + s!")
  146.     //  we will take control of command+s, so don't pass up.
  147.     e.preventDefault()
  148.     // <button class="button" data-action="submit-post">Post</button>
  149.     // same for repy and edit.
  150.     let saveButton = document.querySelector('button.button[data-action="submit-post"]')
  151.     console.log ("saveButton is ",saveButton)
  152.     clickAnchorTag(saveButton)
  153.    
  154.   }
  155.   //GetDescriptionFor(e,"  ")
  156.   console.log("  done with processKey")
  157. }
  158.  
  159.  
  160. /* --------------------------------------------------------------------- */
  161. function GetDescriptionFor(e,inDent)
  162. {
  163. console.log (inDent + "in GetDescriptionFor")
  164. console.log (inDent + "e.keyCode is " + e.keyCode )
  165. console.log (inDent + "e.keyCode in hex is " +toHexString(e.keyCode,inDent+"  ") )
  166. var result, code;
  167. if ((e.charCode) && (e.keyCode==0))
  168. {
  169.  result = "charCode: " + e.charCode;
  170.  code = e.charCode;
  171. } else {
  172.  result = "keyCode: " + e.keyCode;
  173.  code = e.keyCode;
  174. }
  175.  
  176. if (code == 8) result += " BKSP"
  177. else if (code == 9) result += " TAB"
  178. else if (code == 46) result += " DEL"
  179. else if ((code >= 41) && (code <=126)) result += " '" + String.fromCharCode(code) + "'";
  180.  
  181. if (e.altKey) result += " alt";
  182. if (e.ctrlKey) result += " ctrl";
  183. if (e.metaKey) result += " meta";
  184. if (e.shiftKey) result += " shift";
  185. console.log (inDent + "result is " + result)
  186. return result;
  187. }
  188.  
  189. /* --------------------------------------------------------------------- */
  190. /* print out objects:
  191.   https://stackoverflow.com/questions/957537/how-can-i-display-a-javascript-object
  192.   https://code-maven.com/logging-javascript-objects
  193. */
  194. function spew (objName,inputObj) {
  195.   if (debug) {
  196.     console.log ("starting spew")
  197.     console.log (objName + " is: ", inputObj)
  198.     console.log ("inputObj.length is " + inputObj.length)
  199.   }
  200.   if (debug>=2) {
  201.     console.log ("typeof inputObj is " + typeof inputObj)
  202.     //this will show object gandalf IN ALL BROWSERS!
  203.     console.log(JSON.stringify(inputObj));
  204.     //this will show object gandalf IN ALL BROWSERS! with beautiful indent
  205.     console.log(JSON.stringify(inputObj, null, 4));
  206.     console.log("keys",Object.keys(inputObj));
  207.     console.log("lenght is " + Object.keys(inputObj).length)
  208.     console.log(Object.values(inputObj));
  209.  
  210.     //There is also this new option if you're using ECMAScript 2016 or newer:
  211.     try {
  212.       Object.keys(inputObj).forEach(e => console.log(`key=${e}  value=${inputObj[e]}`));
  213.     }
  214.     catch (err) {
  215.       console.log (" You must need ECMAScript 2016 or newer \""+ err.message + "\"" )
  216.     }
  217.     // alert (inputObj)
  218.     //var object = this.window;
  219.     //console.log(object,'this is window object');
  220.     console.log ("ending spew")
  221.   }  // end of if debug
  222. } // end of function spew
  223.  
  224.  
  225. // -----------------------------------------
  226. //  https://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript
  227. function toHexString(value,inDent) {
  228.   hexString = value.toString(16);
  229.   console.log (inDent + "hexString is " + hexString)
  230.   if (hexString.length % 2) {
  231.     hexString = '0' + hexString;
  232.   }
  233. return hexString;
  234. }  // end toHexString
  235.  
  236. /* --------------------------------------------------------------------- */
  237. /* based on:
  238.      https://stackoverflow.com/questions/902713/how-do-i-programmatically-click-a-link-with-javascript
  239. */
  240. function clickAnchorTag(inputObj) {
  241.     console.log("in clickAnchorTag.\n inputObj is",inputObj)
  242.    
  243.     var event = document.createEvent('MouseEvent');
  244.     if (debug>=2) console.log ("proceeding to set new event")
  245.     event = new CustomEvent('click');
  246.     if (debug) console.log ("clicking...")
  247.     inputObj.dispatchEvent(event);
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement