Advertisement
rccharles

asc shortcut keys v0.5

Apr 1st, 2019
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    multiple command key shortcuts#4 24-Feb-2019
  3.  
  4.    Function:
  5.     command + s    to save the post.
  6.     control + command + h   Toggles between showing the underlying html and back to normal.
  7.                             Note: Apple filters the html.
  8.                             [ You should use this for view only.  Making changes is at your own risk. ]
  9.     control + command + p for pick picture
  10.     control + command + PageUp to decrease edit box size
  11.     control + command + PageDown to increase edit box size
  12.     control + command + t for quote
  13.  
  14.     If a reply page, make preferred community the default community
  15.    
  16.     Avoids use of jquery.  jquery is not working for me at this time.
  17.    
  18.     interesting
  19.       https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
  20.       https://discussions.apple.com/thread/250079673?page=1
  21.  
  22.     from Greasemonkey hacks: tips & Tools for Remixing the web with Firefox
  23.     by Julien Couvreur
  24.     Code from book with his gracious permission
  25.  
  26.     Javascript: The Definitive Guide 6th Edition
  27.  
  28.  
  29. https://stackoverflow.com/questions/2878983/capture-key-press-without-placing-an-input-element-on-the-page
  30. window.addEventListener('keydown', function (e) {
  31.   if (e.ctrlKey && e.keyCode == 90) {
  32.     // Ctrl + z pressed
  33.   }
  34. });
  35.  
  36. Assigns each level of a brace to a different color.
  37. http://www.balancebraces.com/
  38.  
  39. show utf character codes:
  40. https://mothereff.in/js-escapes#1%E6%BC%A2%E5%AD%97
  41.  
  42. getting and setting css variables
  43. https://davidwalsh.name/css-variables-javascript
  44.   How to initialize
  45.   :root {
  46.     --my-variable-name: #999999;
  47.   }
  48.  
  49.   getComputedStyle(document.documentElement)
  50.     .getPropertyValue('--my-variable-name'); // #999999
  51.  
  52.   document.documentElement.style
  53.     .setProperty('--my-variable-name', 'pink');
  54.    
  55. window.location.href returns the href (URL) of the current page.
  56.  
  57. What html characters do we need to escape?
  58.     &lt; (<)
  59.     &gt; (>)
  60.     &amp; (&)
  61.     &quot; double-quote (")
  62.     &apos; single quote (')
  63.   https://www.w3.org/International/questions/qa-escapes#use
  64.  
  65. Support for full regular expressions in include and exclude rules is also available.
  66. If the rule both starts and ends with a forward-slash (/) character,
  67. the contents inside those slashes are interpreted as a regular expression.
  68.  
  69. two types of url's we need to handle:
  70. https://discussions.apple.com/thread/250075737
  71. https://discussions.apple.com/create/question?communityId=2206020
  72. https://discussions.apple.com/create/userTip?communityId=2206020
  73. https://discussions.apple.com/docs/DOC-250000368
  74.  
  75. */
  76.  
  77. /* eslint                                        */
  78. /* https://eslint.org/docs/rules/no-multi-spaces */
  79. /* Most of these rule adjustment don't work in   */
  80. /*   this browser :-(.                            */
  81. /* eslint "ignoreEOLComments": "off", "ignoreComments": "off" */
  82. // /*eslint no-caller: "error"*/
  83. // /* eslint no-multi-spaces: ["error", { ignoreEOLComments: true }] */
  84.  
  85. // ==UserScript==
  86. // @name        multiple command key shortcuts#5 1-Apr-2019
  87. // @namespace   bubo-bubo/gmscripts
  88. // @description Implement save and html edit shortcut.
  89. // @include     /https://discussions\.apple\.com/+thread/+.*/
  90. // @include     /https://discussions\.apple\.com/create/question.*/
  91. // @include     /https://discussions\.apple\.com/create/userTip.*/
  92. // @include     /https://discussions\.apple\.com/docs/.*/
  93. // @version     19Jan2019
  94. // @grant       none
  95. // ==/UserScript==
  96.  
  97.  
  98. var debug = 2; // 0 -- no debug
  99.                // 1 -- normal debug
  100.                // 2 -- intense debug
  101.                // 3 -- extremely intense
  102.  
  103. // Keeps tracks of what stage we are in our HTML edit.
  104. //  0 is not editing
  105. //  1 is editing.  Thereby displaying actually html.
  106. var trackHTMLEdit = false;
  107.  
  108. // Is event listner load deployed?
  109. var deployListnerLoad = false;
  110.  
  111. let aDate = new Date();
  112. console.log ("====> starting command key shortcuts. " + aDate + " <====");
  113.  
  114. // Setup once page is loaded.
  115. // This program monitors incoming keypress.  When a known key is observed, this program takes the
  116. // assigned action.
  117.  
  118. /* Most likely will be interactive.
  119.     The script will run after the main page is loaded, but before other resources
  120.     (images, style sheets, etc.) have loaded.
  121.     https://wiki.greasespot.net/Metadata_Block#.40run-at  */
  122. if (debug) console.log("document.readyState is " + document.readyState);
  123.  
  124. //  ready to roll? More stuff for later?
  125. if (document.readyState=="complete") {
  126.   console.log("rolling along")
  127.   // rolling along
  128.   pageLoadComplete()
  129. }
  130. else {
  131.   if (debug>=2) console.log('adding listeners')
  132.   // wait out page loading.
  133.   window.addEventListener("load",
  134.                           pageLoadComplete,
  135.                           true);
  136.   deployListnerLoad = true;
  137. }
  138. // register event listeners
  139. // nothing like starting at the end ;-)
  140.  window.addEventListener('unload',
  141.         function(e)
  142.         {
  143.           if (debug) console.log('unloading.');
  144.           // may not be needed.  Wonder if that is bad?
  145.           if (deployListnerLoad) {
  146.             window.removeEventListener("load",
  147.                                        pageLoadComplete,
  148.                                        true);
  149.             deployListnerLoad = false;
  150.           }
  151.           window.removeEventListener("keypress",
  152.                                      processKey,
  153.                                      false);
  154.           if (debug) console.log('removing e.type is '+ e.type + " arg is " + arguments.callee);
  155.           // remove anonymous unload. [ Thus unload ]
  156.           window.removeEventListener(e.type,
  157.                                      arguments.callee,
  158.                                      true);
  159.         },
  160.         true);
  161.  
  162. // last set value is returned as return code.
  163. var done;
  164. done = 0;
  165.  
  166. // -----------------------------------------------------------------------------
  167. function pageLoadComplete() {
  168.   if (debug) console.log ("--> pageLoadComplete.  ");
  169.   // find how many reply buttons there are.  Not used for nothing.
  170.   if (debug>=2){
  171.     console.log("All page resources finished loading!")
  172.     const theNodes = document.querySelectorAll(
  173.       "button.button.button-black:not(.hidden):not(.button-checked)")
  174.     spew ("theNodes",theNodes)
  175.   }
  176.  
  177.   /* Let's get started. */
  178.   processPage()
  179.  
  180. } // end function pageLoadComplete
  181.  
  182. /* --------------------------------------------------------------------- */
  183. /* main line code */
  184. function processPage() {
  185.   if (debug) console.log ("--> processPage.  ");
  186.  
  187.   // See if this is a reply page which we will then click on preferred link to make default
  188.   // https://discussions.apple.com/create/question?communityId=2140020
  189.   // Skip:
  190.   // https://discussions.apple.com/profile/rccharles/subscriptions
  191.   const currentPage = window.location.href  // returns the href (URL) of the current page.
  192.   console.log ("currentPage is ", currentPage)
  193.   if ( currentPage.search(/https:\/\/discussions.apple.com\/create\/question\?communityId=/)>=0) {
  194.     // Default post from perferred community
  195.     let saveRevealIcon = document.querySelector('div.create-post-topic div.create-post-selection-list div.selector-item button.selector-choice');
  196.     if (debug>=2) console.log ("saveRevealIcon is ",saveRevealIcon)
  197.     if (saveRevealIcon) {
  198.       //clickAnchorTag(saveRevealIcon)
  199.     }                                                  
  200.   }
  201.  
  202.   /* Who knows, we might have to wait a bit */
  203.   /* <div class="editor-section"> */
  204.   //var theNode = document.querySelector("div.editor-section")
  205.   var theNode = document.querySelector("div#main-content")
  206.   console.log("theNode is ",theNode)
  207.   theNode.addEventListener("keypress",
  208.                            processKey,
  209.                            false);
  210.  
  211.   if (debug) console.log ("done with processPage  ");
  212. } // end of processPage;
  213.  
  214. // -----------------------------------------;
  215. function processKey(event) {
  216.   if (debug) console.log("in processKey with event input event",event);
  217.   if (debug>=2) {
  218.     console.log("  event.altKey is " + event.altKey);
  219.     console.log("  event.ctrlKey is " + event.ctrlKey);
  220.     console.log("  event.metaKey is " + event.metaKey);
  221.     console.log("  event.shiftKey is " + event.shiftKey);
  222.     console.log("  event.charCode is " + event.charCode + "  event.key is " + event.key);
  223.   } // end if debug;
  224.  
  225.   // Check modifier keys;
  226.   /*
  227.     command + s for save post
  228.     control + command + h toggle for html viewing
  229.                           [ You should use this for view only.  Making changes is at your own risk. ]
  230.     control + command + p for pick picture
  231.     control + command + PageUp to decrease edit box size
  232.     control + command + PageDown to increase edit box size
  233.     control + command + t for quote
  234.   */
  235.   if (event.altKey) {
  236.     // skip;
  237.     if (debug>=2) console.log ("  skipping altKey! no actions assigned.");
  238.   }
  239.   else if (event.shiftKey) {
  240.     // skip;
  241.     if (debug>=2) console.log ("  skipping shiftKey! no actions assigned.");
  242.   }
  243.   // meta key is required for save, html edit, quote and select picture;
  244.   else if (event.metaKey) {
  245.     if (debug>=2) console.log ("  metaKey found!");
  246.     // control key is required for html edit, quote, select picture etc.
  247.     if (event.ctrlKey) {
  248.       if (debug>=2) console.log ("  ctrlKey found!");
  249.       // could be control + command + h for html edit;
  250.       if (event.charCode === 104) {
  251.         if (debug>=2) console.log ("  command + control + h!");
  252.         htmlEdit(event);
  253.       }
  254.       // could be control + command + p for pick picture;
  255.       else if (event.charCode === 112) {
  256.         if (debug>=2) console.log ("  command + control + p!");
  257.         findPicture(event);
  258.         if (debug>=2) console.log ("  processed image");
  259.       }
  260.       // could be control + command + PageUp to decrease edit box size;
  261.       else if (event.key === "PageUp") {
  262.         if (debug>=2) console.log ("  command + control + PageUp!");
  263.         changeEditBoxSize(event,-100);
  264.         if (debug>=2) console.log ("  processed PageUp");
  265.       }
  266.        // could be control + command + PageDown to increase edit box size;
  267.       else if (event.key === "PageDown" ) {
  268.         if (debug>=2) console.log ("  command + control + PageDown!");
  269.         changeEditBoxSize(event,100);
  270.         if (debug>=2) console.log ("  processed PageDown");
  271.       }
  272.       // could be control + command + t to input a quote
  273.       else if (event.key === "t" ) {
  274.         if (debug>=2) console.log ("  command + control + t!");
  275.         makeQuote(event);
  276.         if (debug>=2) console.log ("  processed quote");
  277.       }
  278.     } // end of ctrlKey;
  279.     else {
  280.       if (debug>=2) console.log ("  processing metaKey!");
  281.       // could be command + s for save post
  282.       if (event.charCode === 115 ){
  283.         if (debug>=2) console.log ("  found command + s!");
  284.         // <div class="editor-section hidden">;
  285.         //   ... clipped ...
  286.         // <button class="button" data-action="submit-post">Post</button>
  287.         // same for repy and edit.
  288.         // div.editor-section
  289.         // let saveButton = document.querySelector('button.button[data-action="submit-post"]')
  290.         // noticed after reply, reminants are left hidden :-(. So, avoid hidden.;
  291.         let saveButton = document.querySelector(
  292.                 'div.editor-section:not(.hidden) button.button[data-action="submit-post"], section.create-post div.create-post-buttons button.button.create-post-button');
  293.         if (debug>=2) console.log ("saveButton is ",saveButton);
  294.         if (saveButton) {
  295.           clickAnchorTag(saveButton);
  296.           //  we will take control of command+s, so don't pass up to other event handlers.;
  297.           event.preventDefault();
  298.         }
  299.         else {
  300.           if (debug) console.log("We have no reply to save; bubble up.");
  301.         }
  302.       } // 115 or s;
  303.     } // end of else not control key;
  304.   } // metaKey;
  305.   //GetDescriptionFor(e,"  ");
  306.   if (debug>=1) console.log("  done with processKey");
  307. } // end of processKey;
  308.  
  309. /* --------------------------------------------------------------------- */
  310. function htmlEdit(event)
  311. {
  312.   /*
  313.       This html is for the reply and edit reply.  There is a slightly different format for
  314.       a new post and a user tip.
  315.       <div class="editor-section">
  316.         <div id="editor-placeholder" ... >
  317.           <div class="editor ql-container" ... >
  318.             <div class="ql-editor"
  319.               data-gramm="false"
  320.               data-placeholder="Tell us what’s on your mind."
  321.               contenteditable="true">
  322.   */
  323.   // find the editor section.
  324.   // Reply and Edit Reply catch first selector.
  325.   // Create new post and new user tip catch on second selector.
  326.   let saveTextNode = document.querySelector(
  327.     'div.editor-section:not(.hidden) div#editor-placeholder div.ql-editor, section.create-post div.editor.ql-container div.ql-editor')
  328.   if (debug>=2) console.log("saveTextNode is ",saveTextNode)
  329.   // is the user really doing an edit?
  330.   if (saveTextNode) {
  331.     // should we begin the HTML edit?
  332.     if (trackHTMLEdit===false) {
  333.       // request to edit.
  334.       let savedInnerHTML = saveTextNode.innerHTML
  335.  
  336.       displayHexString(savedInnerHTML,"    ")
  337.  
  338.       if (debug>=2) console.log("savedInnerHTML is ",savedInnerHTML)
  339.       // Convert display to HTML
  340.       // amazing.
  341.       // https://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript
  342.       saveTextNode.innerText = savedInnerHTML
  343.       // toggle
  344.       trackHTMLEdit = true
  345.       //  we will take control of control + command + h, so don't pass up
  346.       //    to other event handlers.
  347.       event.preventDefault()
  348.     }
  349.     else {
  350.       // end HTML edit.  Convert to regular text.
  351.       if (debug>=2) console.log("displaying html. Convert back. ")
  352.       // already doing HTML edit.
  353.       let displayedText=saveTextNode.innerText
  354.       if (debug>=2) console.log("displayedText is ",displayedText)
  355.       saveTextNode.innerHTML = displayedText
  356.       // toggle
  357.       trackHTMLEdit = false
  358.       //  we will take control of control + command + h, so don't pass up
  359.       //    to other event handlers.
  360.       event.preventDefault()
  361.     }
  362.   } // end doing an edit found "right" node
  363.   else {
  364.     if (debug) console.log("reply edit note not found.")
  365.   }
  366. } // end of function htmlEdit
  367.  
  368. /* --------------------------------------------------------------------- */
  369. function findPicture(event) {
  370.    if (debug>=1) console.log ("in findPicture")
  371.    var savePicture = document.querySelector('div.toolbar div.row  section.buttons-group:not(.hide-mobile) button.rte-button.rte-hl-bg-active:first-child')
  372.    if (debug>=2) console.log ("savePicture is ",savePicture)
  373.    if (savePicture) {
  374.      clickAnchorTag(savePicture)
  375.      //  we will take control of command+s, so don't pass up to other event handlers.
  376.      event.preventDefault();
  377.    }
  378. } // end of findPicture
  379.  
  380. /* --------------------------------------------------------------------- */
  381. /*
  382. // Set multiple styles in a single statement
  383. elt.style.cssText = "color: blue; border: 1px solid black";
  384. // Or
  385. elt.setAttribute("style", "color:red; border: 1px solid blue;");
  386.  
  387. // Set specific style while leaving other inline style values untouched
  388. elt.style.color = "blue";
  389.  
  390. https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style
  391.  
  392. */
  393. function changeEditBoxSize(event,changeSize) {
  394.   if (debug>=1) console.log ("in changeEditBoxSize")
  395.   // if we find a messed up style, continue on our merry way.
  396.   try {
  397.    var saveEdit = document.querySelector('div.editor.ql-container');
  398.    if (debug>=2) console.log ("saveEdit is ",saveEdit);
  399.    if (saveEdit) {
  400.      let allStyle = saveEdit.getAttribute("style")
  401.      if (debug>=2) console.log("allStyle are ",allStyle);
  402.      // Have we been through here before?
  403.      if ( allStyle.search(/!important;/)>=0) {
  404.        // been there done that
  405.        editorHeight=saveEdit.style.getPropertyValue('--editor-height');
  406.        console.log("editorHeight via html is ",editorHeight)      
  407.      }
  408.      else {
  409.        // need to take from composite style
  410.        editorHeight = window.getComputedStyle(saveEdit).getPropertyValue('--editor-height')
  411.        console.log("editorHeight via getComputedStyle is ",editorHeight)
  412.      }
  413.        
  414.      /* https://stackoverflow.com/questions/8811027/
  415.      get-elements-custom-css-properties-mystyle-using-javascript/36609605#36609605 */
  416.    
  417.      // fyi: returns an array
  418.      editorHeightNumber = editorHeight.split("px")
  419.      console.log("editorHeightNumber is ",editorHeightNumber)
  420.      let count = Number(editorHeightNumber[0])
  421.      count+=changeSize
  422.      if (count <= 100){ count = Math.abs(changeSize) }
  423.      console.log("count is ",count)
  424.      const injectionString = count.toString()+"px !important;"
  425.      console.log("injectionString is ",injectionString, "typeof injectionString is "
  426.                  + typeof injectionString)
  427.      console.log("saveEdit.attributes are now ",saveEdit.attributes);
  428.      console.log("~1~style is ",saveEdit.getAttribute("style"));
  429.      // Up cascade priority
  430.      // setting css variable, --editor-height, drops off second word, "!important;".  :-(.
  431.      editorHeightHTML=saveEdit.style.getPropertyValue('--editor-height');
  432.      console.log ("editorHeightHTML is ", editorHeightHTML)
  433.      const alteredString = allStyle.replace(editorHeightHTML,injectionString)  
  434.      console.log (" alteredString is ", alteredString)
  435.      saveEdit.style.cssText = alteredString
  436.      console.log("~2~style is ",saveEdit.getAttribute("style"));
  437.      
  438.      if (debug>=2) console.log ("eat character")
  439.      //  we will take control of command+s, so don't pass up to other event handlers.
  440.      event.preventDefault()
  441.    }  // end of if (saveEdit)
  442.    else {
  443.      console.log("\nCould not find edit box!!!\n")
  444.    }
  445.    if (debug>=2) console.log ("bye from changeEditBoxSize")
  446.  
  447.    //Object.keys(inputObj).forEach(e => console.log(`key=${e}  value=${inputObj[e]}`));
  448.   } // end of try
  449.   catch (err) {
  450.     console.log ("Don't understand style format.\"" + err.message + "\"" )
  451.   } // end catch
  452.  
  453. } // end function changeEditBoxSize
  454.  
  455. /* --------------------------------------------------------------------- */
  456. // Click on the quote icon.
  457. function makeQuote(event) {
  458.   if (debug>=1) console.log ("in makeQuote")
  459.   try {
  460.     // Click on the reveal secondary icons.  Aa icon
  461.     let saveRevealIcon = document.querySelector('div.toolbar div.row  section.buttons-group button.rte-button[data-rte="to-formats"]');
  462.     if (debug>=2) console.log ("saveRevealIcon is ",saveRevealIcon)
  463.     if (saveRevealIcon) {
  464.       clickAnchorTag(saveRevealIcon)
  465.  
  466.       // Now, click on the quote icon
  467.       let saveQuoteIcon = document.querySelector('div.toolbar div.row.sub-row div.sub-row-wrapper section.buttons-group button.rte-button.rte-hl-bg-active[data-rte="blockquote"]');
  468.       if (debug>=2) console.log ("saveQuoteIcon is ",saveQuoteIcon)
  469.       if (saveQuoteIcon) {
  470.         clickAnchorTag(saveQuoteIcon)
  471.         // Well, we quoted the stuff added to the input area.
  472.         // seems good enough to count as processed request.
  473.         // we will take control of control + command+t,
  474.         // so don't pass up to other event handlers.
  475.         event.preventDefault();
  476.  
  477.         // Finally, click on the close secondary icon line.  X
  478.         let saveQuitIcon = document.querySelector('div.toolbar div.row.sub-row button.rte-close-button');
  479.         if (debug>=2) console.log ("saveQuitIcon is ",saveQuitIcon)
  480.         if (saveQuitIcon) {
  481.           clickAnchorTag(saveQuitIcon)
  482.         } // end of saveQuitIcon
  483.         else {
  484.           console.log ("saveQuitIcon not found!!! ")
  485.         }
  486.       } // end of saveQuoteIcon
  487.     } // end of saveRevealIcon
  488.   } // end of try
  489.   catch (err) {
  490.     console.log ("makeQuote got confused. \"" + err.message + "\"" )
  491.   } // end catch
  492. } // end of function makeQuote
  493.  
  494. /* --------------------------------------------------------------------- */
  495. function GetDescriptionFor(e,inDent)
  496. {
  497. if (debug>=2) {
  498.   console.log (inDent + "in GetDescriptionFor")
  499.   console.log (inDent + "e.keyCode is " + e.keyCode )
  500.   console.log (inDent + "e.keyCode in hex is " +toHexString(e.keyCode,inDent+"  ") )
  501. }
  502. var result, code;
  503. if ((e.charCode) && (e.keyCode==0))
  504. {
  505.  result = "charCode: " + e.charCode;
  506.  code = e.charCode;
  507. } else {
  508.  result = "keyCode: " + e.keyCode;
  509.  code = e.keyCode;
  510. }
  511.  
  512. if (code == 8) result += " BKSP"
  513. else if (code == 9) result += " TAB"
  514. else if (code == 46) result += " DEL"
  515. else if ((code >= 41) && (code <=126)) result += " '" + String.fromCharCode(code) + "'";
  516.  
  517. if (e.altKey) result += " alt";
  518. if (e.ctrlKey) result += " ctrl";
  519. if (e.metaKey) result += " meta";
  520. if (e.shiftKey) result += " shift";
  521. console.log (inDent + "result is " + result)
  522. return result;
  523. } // end of GetDescriptionFor
  524.  
  525. /* --------------------------------------------------------------------- */
  526. /* print out objects:
  527.   https://stackoverflow.com/questions/957537/how-can-i-display-a-javascript-object
  528.   https://code-maven.com/logging-javascript-objects
  529. */
  530. function spew (objName,inputObj) {
  531.   if (debug>=1) console.log ("starting spew")
  532.   if (debug>=2) {
  533.     console.log (objName + " is: ", inputObj)
  534.     console.log ("inputObj.length is " + inputObj.length)
  535.     console.log ("typeof inputObj is " + typeof inputObj)
  536.     //this will show object gandalf IN ALL BROWSERS!
  537.     console.log(JSON.stringify(inputObj));
  538.     //this will show object gandalf IN ALL BROWSERS! with beautiful indent
  539.     console.log(JSON.stringify(inputObj, null, 4));
  540.     console.log("keys",Object.keys(inputObj));
  541.     console.log("lenght is " + Object.keys(inputObj).length)
  542.     console.log(Object.values(inputObj));
  543.  
  544.     //There is also this new option if you're using ECMAScript 2016 or newer:
  545.     try {
  546.       Object.keys(inputObj).forEach(e => console.log(`key=${e}  value=${inputObj[e]}`));
  547.     }
  548.     catch (err) {
  549.       console.log (" You must need ECMAScript 2016 or newer \""+ err.message + "\"" )
  550.     } // end catch
  551.  
  552.   } // end of if debug printing code
  553.   if (debug>=1) console.log ("ending spew")
  554. } // end of function spew
  555.  
  556. // -----------------------------------------
  557. //  demo
  558. //  http://mothereff.in/js-escapes#1%E6%BC%A2%E5%AD%97
  559. //;
  560. function displayHexString(value,inDent) {
  561.   if (debug>=1) console.log("in function displayHexString");
  562.   var hexString = value.hexEncode();
  563.   if (debug>=2) console.log (inDent + "hexString is " + hexString);
  564.   if (hexString.length % 2) {
  565.     hexString = '0' + hexString;
  566.   }
  567.   const increment = 16;
  568.   var prefixNumber = 0;
  569.   var prefixNumberSpan = increment;
  570.  
  571.   // print groups of sixteen;
  572.   while( hexString.length > increment ) {
  573.     let prefixHex = prefixNumber.toString(increment);
  574.     if (prefixHex.length % 2) {
  575.       prefixHex = '0' + prefixHex;
  576.     } // end of if;
  577.     // Print out on console.;
  578.     // four hex digits per one printable.;
  579.     console.log(prefixHex + " " + hexString.substring(0,increment) + "  " +
  580.                 value.substring(prefixNumber/4,prefixNumberSpan/4)               );
  581.     hexString = hexString.substring(increment);
  582.     if (debug>=3) console.log( "hexString is "+ hexString);
  583.     prefixNumber += increment;
  584.     prefixNumberSpan += increment;
  585.     if (debug>=3) console.log("prefixNumber is "+ prefixNumber + " prefixNumberSpan is " + prefixNumberSpan);
  586.   } // end of while;
  587.  
  588.   if (debug>=3) console.log("done with groups of  " + increment);
  589.   if ( hexString.length ) {
  590.     let prefixHex = prefixNumber.toString(increment);
  591.     if (prefixHex.length % 2) {
  592.       prefixHex = '0' + prefixHex;
  593.     } // end of if
  594.     // Print out on console.
  595.     console.log(prefixHex + " " + hexString);
  596.   } // end of if;
  597.  
  598. } // end of toHexString;
  599.  
  600. /* --------------------------------------------------------------------- */
  601. /*
  602. https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style
  603.  
  604. <!DOCTYPE HTML>
  605. <html>
  606.   <body style="font-weight:bold;">
  607.     <div style="color:red" id="myElement">..</div>
  608.   </body>
  609. </html>
  610.  
  611. The style property is not useful for completely learning about the styles applied on the element,
  612. since it represents only the CSS declarations set in the element's inline style attribute, not
  613. those that come from style rules elsewhere, such as style rules in the <head> section, or
  614. external style sheets. To get the values of all CSS properties for an element you should use
  615. Window.getComputedStyle() instead.
  616.  
  617. The output would be something like:
  618.  
  619. ...
  620. fontWeight = '' > 'bold'
  621. color = 'red' > 'rgb(255, 0, 0)'
  622. ...
  623.  
  624. Note the presence of the value "bold" for font-weight in the computed style and the absence
  625. of it in the element's style property
  626. */
  627. function printCombinedStyle(element) {
  628. //var element = document.getElementById("myElement");
  629. var out = "";
  630. var elementStyle = element.style;
  631. var computedStyle = window.getComputedStyle(element, null);
  632.  
  633. for (prop in elementStyle) {
  634.   if (elementStyle.hasOwnProperty(prop)) {
  635.     out += "  " + prop + " = '" + elementStyle[prop] + "' > '" + computedStyle[prop] + "'\n";
  636.   }
  637. }
  638. console.log(out)
  639. } // end of function printCombinedStyle
  640.  
  641. /* --------------------------------------------------------------------- */
  642. // https://stackoverflow.com/questions/21647928/javascript-unicode-string-to-hex
  643. String.prototype.hexEncode = function(){
  644.     var hex, i;
  645.  
  646.     var result = "";
  647.     for (i=0; i<this.length; i++) {
  648.         hex = this.charCodeAt(i).toString(16);
  649.         result += ("000"+hex).slice(-4);
  650.     }
  651.  
  652.     return result
  653. }
  654. /* --------------------------------------------------------------------- */
  655. /* based on:
  656.      https://stackoverflow.com/questions/902713/how-do-i-programmatically-click-a-link-with-javascript
  657. */
  658. function clickAnchorTag(inputObj) {
  659.     if (debug>=1) console.log("in clickAnchorTag.\n inputObj is",inputObj)
  660.  
  661.     var event = document.createEvent('MouseEvent');
  662.     if (debug>=2) console.log ("proceeding to set new event")
  663.     event = new CustomEvent('click');
  664.     if (debug>=2) console.log ("clicking...")
  665.     inputObj.dispatchEvent(event);
  666. } // end of clickAnchorTag
  667.  
  668. /*
  669. Exception: Error while performing task "pretty-print": Error: Unexpected character '`' (529:53)
  670. [4]</pp.raise@resource://devtools/shared/acorn/acorn.js:940:13
  671. [13]</pp.getTokenFromCode@resource://devtools/shared/acorn/acorn.js:2785:3
  672. [13]</pp.readToken@resource://devtools/shared/acorn/acorn.js:2490:10
  673. [13]</pp.nextToken@resource://devtools/shared/acorn/acorn.js:2482:66
  674. [13]</pp.next@resource://devtools/shared/acorn/acorn.js:2431:3
  675. [13]</pp.getToken@resource://devtools/shared/acorn/acorn.js:2435:3
  676. prettyFast@resource://devtools/shared/pretty-fast/pretty-fast.js:778:15
  677. @resource://devtools/server/actors/pretty-print-worker.js:41:24
  678. createHandler/<@resource://devtools/shared/worker/helper.js:85:24
  679.  
  680. @resource://devtools/server/actors/pretty-print-worker.js:51:12
  681. createHandler/<@resource://devtools/shared/worker/helper.js:85:24
  682.  
  683. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement