Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- command key shortcuts 15Jan2019
- Intercepts command + s. "Clicks" on save/edit button. Prevent command+s bubbling.
- https://discussions.apple.com/thread/250080039
- interesting
- https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
- https://discussions.apple.com/thread/250079673?page=1
- from Greasemonkey hacks: tips & Tools for Remixing the web with Firefox
- by Julien Couvreur
- Code from book with his gracious permission
- Javascript: The Definitive Guide 6th Edition
- Avoids use of jquery. Not working at this time.
- https://stackoverflow.com/questions/2878983/capture-key-press-without-placing-an-input-element-on-the-page
- window.addEventListener('keydown', function (e) {
- if (e.ctrlKey && e.keyCode == 90) {
- // Ctrl + z pressed
- }
- });
- MutationObserver
- Support for full regular expressions in include and exclude rules is also available.
- If the rule both starts and ends with a forward-slash (/) character,
- the contents inside those slashes are interpreted as a regular expression.
- https://discussions.apple.com/thread/250075737
- */
- /* Separate options by a comma , */
- /* https://eslint.org/docs/rules/no-multi-spaces */
- /* eslint eqeqeq: "ignoreEOLComments": true, "ignoreComments": true */
- // ==UserScript==
- // @name command key shortcuts 25Jan2019
- // @namespace bubo-bubo/gmscripts
- // @description Implement save shortcut.
- // @include /https://discussions\.apple\.com/+thread/+.*/
- // @version 25Jan2019
- // @grant none
- // ==/UserScript==
- var debug = 2; // 0 -- no debug
- // 1 -- normal debug
- // 2 -- intense debug
- var aDate = new Date();
- console.log ("==++> command key shortcuts. " + aDate);
- // Setup once page is loaded.
- // modify thread content view behaviour (in post-load phase)
- /* Most likely will be interactive.
- The script will run after the main page is loaded, but before other resources
- (images, style sheets, etc.) have loaded.
- https://wiki.greasespot.net/Metadata_Block#.40run-at */
- if (debug) console.log("document.readyState is " + document.readyState);
- // ready to roll? More stuff for later?
- if (document.readyState=="complete") {
- console.log("rolling along")
- // rolling along
- pageLoadComplete()
- }
- else {
- if (debug>=2) console.log('adding listeners')
- // wait out page loading.
- window.addEventListener("load",
- pageLoadComplete,
- true);
- }
- // register event listeners
- // nothing like starting at the end ;-)
- window.addEventListener('unload',
- function(e)
- {
- if (debug) console.log('unloading.');
- // may not be needed. Wonder if that is bad?
- window.removeEventListener("load",
- pageLoadComplete,
- true);
- if (debug) console.log('removing e.type is '+ e.type + " arg is " +
- arguments.callee);
- // remove anonymous unload. [ Thus unload ]
- window.removeEventListener(e.type,
- arguments.callee,
- true);
- },
- true);
- // last set value is returned as return code.
- var done;
- done = 0;
- // -----------------------------------------------------------------------------
- function pageLoadComplete() {
- if (debug>=2) console.log("All page resources finished loading!")
- // find how many reply buttons there are. Not used for nothing.
- var theNodes = document.querySelectorAll(
- "button.button.button-black:not(.hidden):not(.button-checked)")
- spew ("theNodes",theNodes)
- console.log("let's get started.")
- processPage()
- }
- /* --------------------------------------------------------------------- */
- /* main line code */
- function processPage() {
- if (debug) console.log ("--> processPage. ");
- /* Who knows, we might have to wait a bit */
- /* <div class="editor-section"> */
- //var theNode = document.querySelector("div.editor-section")
- var theNode = document.querySelector("div#main-content")
- console.log("theNode is ",theNode)
- theNode.addEventListener("keypress",processKey,false);
- //theNode.addEventListener("keydown",processKey,false);
- if (debug) console.log ("done with processPage ");
- } // end of processPage
- // -----------------------------------------
- function processKey(e) {
- console.log(" in processKey with event input e",e)
- console.log(" e.altKey is " + e.altKey)
- console.log(" e.ctrlKey is " + e.ctrlKey)
- console.log(" e.metaKey is " + e.metaKey)
- console.log(" e.shiftKey is " + e.shiftKey)
- if ( e.charCode == 115){
- console.log (" s ")
- }
- if (e.metaKey ){
- console.log (" e.metaKey ")
- }
- if (e.metaKey && e.charCode == 115 ){
- console.log (" found command + s!")
- // we will take control of command+s, so don't pass up.
- e.preventDefault()
- // <button class="button" data-action="submit-post">Post</button>
- // same for repy and edit.
- let saveButton = document.querySelector('button.button[data-action="submit-post"]')
- console.log ("saveButton is ",saveButton)
- clickAnchorTag(saveButton)
- }
- //GetDescriptionFor(e," ")
- console.log(" done with processKey")
- }
- /* --------------------------------------------------------------------- */
- function GetDescriptionFor(e,inDent)
- {
- console.log (inDent + "in GetDescriptionFor")
- console.log (inDent + "e.keyCode is " + e.keyCode )
- console.log (inDent + "e.keyCode in hex is " +toHexString(e.keyCode,inDent+" ") )
- var result, code;
- if ((e.charCode) && (e.keyCode==0))
- {
- result = "charCode: " + e.charCode;
- code = e.charCode;
- } else {
- result = "keyCode: " + e.keyCode;
- code = e.keyCode;
- }
- if (code == 8) result += " BKSP"
- else if (code == 9) result += " TAB"
- else if (code == 46) result += " DEL"
- else if ((code >= 41) && (code <=126)) result += " '" + String.fromCharCode(code) + "'";
- if (e.altKey) result += " alt";
- if (e.ctrlKey) result += " ctrl";
- if (e.metaKey) result += " meta";
- if (e.shiftKey) result += " shift";
- console.log (inDent + "result is " + result)
- return result;
- }
- /* --------------------------------------------------------------------- */
- /* print out objects:
- https://stackoverflow.com/questions/957537/how-can-i-display-a-javascript-object
- https://code-maven.com/logging-javascript-objects
- */
- function spew (objName,inputObj) {
- if (debug) {
- console.log ("starting spew")
- console.log (objName + " is: ", inputObj)
- console.log ("inputObj.length is " + inputObj.length)
- }
- if (debug>=2) {
- console.log ("typeof inputObj is " + typeof inputObj)
- //this will show object gandalf IN ALL BROWSERS!
- console.log(JSON.stringify(inputObj));
- //this will show object gandalf IN ALL BROWSERS! with beautiful indent
- console.log(JSON.stringify(inputObj, null, 4));
- console.log("keys",Object.keys(inputObj));
- console.log("lenght is " + Object.keys(inputObj).length)
- console.log(Object.values(inputObj));
- //There is also this new option if you're using ECMAScript 2016 or newer:
- try {
- Object.keys(inputObj).forEach(e => console.log(`key=${e} value=${inputObj[e]}`));
- }
- catch (err) {
- console.log (" You must need ECMAScript 2016 or newer \""+ err.message + "\"" )
- }
- // alert (inputObj)
- //var object = this.window;
- //console.log(object,'this is window object');
- console.log ("ending spew")
- } // end of if debug
- } // end of function spew
- // -----------------------------------------
- // https://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript
- function toHexString(value,inDent) {
- hexString = value.toString(16);
- console.log (inDent + "hexString is " + hexString)
- if (hexString.length % 2) {
- hexString = '0' + hexString;
- }
- return hexString;
- } // end toHexString
- /* --------------------------------------------------------------------- */
- /* based on:
- https://stackoverflow.com/questions/902713/how-do-i-programmatically-click-a-link-with-javascript
- */
- function clickAnchorTag(inputObj) {
- console.log("in clickAnchorTag.\n inputObj is",inputObj)
- var event = document.createEvent('MouseEvent');
- if (debug>=2) console.log ("proceeding to set new event")
- event = new CustomEvent('click');
- if (debug) console.log ("clicking...")
- inputObj.dispatchEvent(event);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement