Advertisement
rccharles

add asc command+2

Jan 16th, 2019
584
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.  
  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. two types of url's we need to handle
  32.  
  33. https://discussions.apple.com/thread/250075737
  34. https://discussions.apple.com/create/question?communityId=2206020
  35. */
  36.  
  37. /* eslint                                        */
  38. /* https://eslint.org/docs/rules/no-multi-spaces */
  39. /* Most of these rule adjustment don't work in   */
  40. /*   this browser :-(.                            */
  41. /* eslint "ignoreEOLComments": "off", "ignoreComments": "off" */
  42. // /*eslint no-caller: "error"*/
  43. // /* eslint no-multi-spaces: ["error", { ignoreEOLComments: true }] */
  44.  
  45. // ==UserScript==
  46. // @name         command key shortcuts 15Jan2019
  47. // @namespace   bubo-bubo/gmscripts
  48. // @description Implement save shortcut.
  49. // @include     /https://discussions\.apple\.com/+thread/+.*/
  50. // @include     /https://discussions\.apple\.com/create/question.*/
  51. // @version     25Jan2019
  52. // @grant       none
  53. // ==/UserScript==
  54.  
  55.  
  56. var debug = 2; // 0 -- no debug
  57.                // 1 -- normal debug
  58.                // 2 -- intense debug
  59.  
  60. var aDate = new Date();
  61. console.log ("====> starting command key shortcuts. " + aDate);
  62.  
  63.  
  64. // Setup once page is loaded.
  65. // modify thread content view behaviour (in post-load phase)
  66.  
  67. /* Most likely will be interactive.
  68.     The script will run after the main page is loaded, but before other resources
  69.     (images, style sheets, etc.) have loaded.
  70.     https://wiki.greasespot.net/Metadata_Block#.40run-at  */
  71. if (debug) console.log("document.readyState is " + document.readyState);
  72.  
  73. //  ready to roll? More stuff for later?
  74. if (document.readyState=="complete") {
  75.   console.log("rolling along")
  76.   // rolling along
  77.   pageLoadComplete()
  78. }
  79. else {
  80.   if (debug>=2) console.log('adding listeners')
  81.   // wait out page loading.
  82.   window.addEventListener("load",
  83.                           pageLoadComplete,
  84.                           true);
  85. }
  86. // register event listeners
  87. // nothing like starting at the end ;-)
  88.  window.addEventListener('unload',
  89.         function(e)
  90.         {
  91.           if (debug) console.log('unloading.');
  92.           // may not be needed.  Wonder if that is bad?
  93.           window.removeEventListener("load",
  94.                                      pageLoadComplete,
  95.                                      true);
  96.           if (debug) console.log('removing e.type is '+ e.type + " arg is " + arguments.callee);
  97.           // remove anonymous unload. [ Thus unload ]
  98.           window.removeEventListener(e.type,
  99.                                      arguments.callee,
  100.                                      true);
  101.         },
  102.         true);
  103.  
  104. // last set value is returned as return code.
  105. var done;
  106. done = 0;
  107.  
  108. // -----------------------------------------------------------------------------
  109. function pageLoadComplete() {
  110.   if (debug>=2) console.log("All page resources finished loading!")
  111.   // find how many reply buttons there are.  Not used for nothing.
  112.   var theNodes = document.querySelectorAll(
  113.     "button.button.button-black:not(.hidden):not(.button-checked)")
  114.   if (debug>=2) spew ("theNodes",theNodes)
  115.   if (debug)console.log("let's get started.")
  116.  
  117.   /* Let's get started. */
  118.   processPage()
  119.  
  120. }
  121.  
  122. /* --------------------------------------------------------------------- */
  123. /* main line code */
  124. function processPage() {
  125.   if (debug) console.log ("--> processPage.  ");
  126.  
  127.   /* Who knows, we might have to wait a bit */
  128.   /* <div class="editor-section"> */
  129.   //var theNode = document.querySelector("div.editor-section")
  130.   var theNode = document.querySelector("div#main-content")
  131.   console.log("theNode is ",theNode)
  132.   theNode.addEventListener("keypress",processKey,false);
  133.  
  134.   //theNode.addEventListener("keydown",processKey,false);
  135.   if (debug) console.log ("done with processPage  ");
  136. } // end of processPage
  137.  
  138. // -----------------------------------------
  139. function processKey(e) {
  140.   console.log("in processKey with event input e",e)
  141.   if (debug>=2) {
  142.     console.log("  e.altKey is " + e.altKey)
  143.     console.log("  e.ctrlKey is " + e.ctrlKey)
  144.     console.log("  e.metaKey is " + e.metaKey)
  145.     console.log("  e.shiftKey is " + e.shiftKey)
  146.     if ( e.charCode === 115){
  147.       console.log ("  s ")
  148.     }
  149.    if (e.metaKey){
  150.       console.log ("  e.metaKey ")
  151.    }
  152.   } // end if debug
  153.  
  154.   // Check for command + s
  155.   if (e.metaKey && e.charCode === 115 ){
  156.     if (debug>=2) console.log ("  found command + s!")
  157.  
  158.     // <div class="editor-section hidden">
  159.     //   ... clipped ...
  160.     // <button class="button" data-action="submit-post">Post</button>
  161.     // same for repy and edit.
  162.     // div.editor-section
  163.     // let saveButton = document.querySelector('button.button[data-action="submit-post"]')
  164.     // noticed after reply, reminants are left hidden :-(. So, avoid hidden.
  165.     let saveButton = document.querySelector(
  166.             'div.editor-section:not(.hidden) button.button[data-action="submit-post"]')
  167.     if (debug>=2) console.log ("saveButton is ",saveButton)
  168.     if (saveButton) {
  169.       clickAnchorTag(saveButton)
  170.       //  we will take control of command+s, so don't pass up to other event handlers.
  171.       e.preventDefault()
  172.     }
  173.     else {
  174.       if (debug) console.log("We have no reply to save; bubble up.")
  175.     }
  176.  
  177.   }
  178.   //GetDescriptionFor(e,"  ")
  179.   if (debug>=1) console.log("  done with processKey")
  180. } // end of processKey
  181.  
  182.  
  183. /* --------------------------------------------------------------------- */
  184. function GetDescriptionFor(e,inDent)
  185. {
  186. console.log (inDent + "in GetDescriptionFor")
  187. console.log (inDent + "e.keyCode is " + e.keyCode )
  188. console.log (inDent + "e.keyCode in hex is " +toHexString(e.keyCode,inDent+"  ") )
  189. var result, code;
  190. if ((e.charCode) && (e.keyCode==0))
  191. {
  192.  result = "charCode: " + e.charCode;
  193.  code = e.charCode;
  194. } else {
  195.  result = "keyCode: " + e.keyCode;
  196.  code = e.keyCode;
  197. }
  198.  
  199. if (code == 8) result += " BKSP"
  200. else if (code == 9) result += " TAB"
  201. else if (code == 46) result += " DEL"
  202. else if ((code >= 41) && (code <=126)) result += " '" + String.fromCharCode(code) + "'";
  203.  
  204. if (e.altKey) result += " alt";
  205. if (e.ctrlKey) result += " ctrl";
  206. if (e.metaKey) result += " meta";
  207. if (e.shiftKey) result += " shift";
  208. console.log (inDent + "result is " + result)
  209. return result;
  210. } // end of GetDescriptionFor
  211.  
  212. /* --------------------------------------------------------------------- */
  213. /* print out objects:
  214.   https://stackoverflow.com/questions/957537/how-can-i-display-a-javascript-object
  215.   https://code-maven.com/logging-javascript-objects
  216. */
  217. function spew (objName,inputObj) {
  218.   if (debug>=1) console.log ("starting spew")
  219.   if (debug>=2) {
  220.     console.log (objName + " is: ", inputObj)
  221.     console.log ("inputObj.length is " + inputObj.length)
  222.     console.log ("typeof inputObj is " + typeof inputObj)
  223.     //this will show object gandalf IN ALL BROWSERS!
  224.     console.log(JSON.stringify(inputObj));
  225.     //this will show object gandalf IN ALL BROWSERS! with beautiful indent
  226.     console.log(JSON.stringify(inputObj, null, 4));
  227.     console.log("keys",Object.keys(inputObj));
  228.     console.log("lenght is " + Object.keys(inputObj).length)
  229.     console.log(Object.values(inputObj));
  230.  
  231.     //There is also this new option if you're using ECMAScript 2016 or newer:
  232.     try {
  233.       Object.keys(inputObj).forEach(e => console.log(`key=${e}  value=${inputObj[e]}`));
  234.     }
  235.     catch (err) {
  236.       console.log (" You must need ECMAScript 2016 or newer \""+ err.message + "\"" )
  237.     } // end catch
  238.  
  239.   } // end of if debug printing code
  240.   if (debug>=1) console.log ("ending spew")
  241. } // end of function spew
  242.  
  243. // -----------------------------------------
  244. //  https://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript
  245. function toHexString(value,inDent) {
  246.   let hexString = value.toString(16);
  247.   if (debug>=2) console.log (inDent + "hexString is " + hexString)
  248.   if (hexString.length % 2) {
  249.     hexString = '0' + hexString;
  250.   }
  251. return hexString;
  252. } // end of toHexString
  253.  
  254. /* --------------------------------------------------------------------- */
  255. /* based on:
  256.      https://stackoverflow.com/questions/902713/how-do-i-programmatically-click-a-link-with-javascript
  257. */
  258. function clickAnchorTag(inputObj) {
  259.     if (debug>=1) console.log("in clickAnchorTag.\n inputObj is",inputObj)
  260.  
  261.     var event = document.createEvent('MouseEvent');
  262.     if (debug>=2) console.log ("proceeding to set new event")
  263.     event = new CustomEvent('click');
  264.     if (debug>=2) console.log ("clicking...")
  265.     inputObj.dispatchEvent(event);
  266. } // end of clickAnchorTag
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement