Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name ASC Pagination Changing: e -- August 24, 2016 -- working
- // @namespace ASCPaginationGoto
- // @description script functions
- // @include https://discussions.apple.com/message/*
- // @include https://discussions.apple.com/thread/*
- // @version 1
- // @grant none
- // ==/UserScript==
- /*
- Add pagination to the top of Apple Support Communities board web page
- system introduced in 2016-06
- Copyright 2016 rccharles
- GNU General Public License
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, version 3
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- For a copy of the GNU General Public License
- see <http://www.gnu.org/licenses/>.
- For more information on how to install this script and see:
- https://discussions.apple.com
- v0.99
- - Beta
- v0.50
- - Alpha
- - Alpha testing by ccc
- written by rccharles, August 2016
- */
- // In case of a serious bug, run in the web console on the page you want to change.
- // Command+] to indent a highlighted block of text or conversely Command+[ to unindent
- /*
- document.getElementById("intro");
- document.getElementsByTagName("p");
- This example finds the element with id="main", and then finds all <p> elements inside "main":
- Example
- var x = document.getElementById("main");
- var y = x.getElementsByTagName("p");
- document.getElementsByClassName("intro");
- document.querySelectorAll("p.intro");
- document.getElementById("demo").innerHTML = text;
- */
- /*
- if (typeof variable === 'undefined') {
- // variable is undefined
- }
- The typeof operator, unlike the other operators, doesn't throw a ReferenceError exception when used with an undeclared variable.
- However, do note that typeof null will return "object". We have to be careful to avoid the mistake of initializing a variable to null. To be safe, this is what we could use instead:
- if (typeof variable === 'undefined' || variable === null) {
- // variable is undefined or null
- }
- */
- // What the "from" htlm looks like.
- // <div class="all-replies-container">
- // <!-- BEGIN pagination-->
- // <span class="j-pagination pagination top">
- // <a href="/thread/7632418?start=0&tstart=0"
- // title="global.first" class="js-pagination-first"
- // onclick="jspaginate.init('first'); return false;">first
- // </a>
- // <a href="/thread/7632418?start=15&tstart=0"
- // title="Select to go to the previous page"
- // class="js-pagination-prev icon icon-standalone icon-chevronleft"
- // onclick="jspaginate.init('prev'); return false;" >
- // <span class="a11y">Previous
- // </span>
- // </a>
- // <span class="current-page">
- // Page
- // <!--<a href="/thread/7632418?start=30&tstart=0"
- // class="j-pagination-current current-page-target font-color-normal" 3 </a>-->
- // 3
- // of
- // 3
- // <!--<a href="/thread/7632418?start=30&tstart=0"
- // class="j-pagination-current font-color-normal">-->
- // <!--3-->
- // <!--</a>-->
- // </span>
- // </span>
- // <!-- END pagination -->
- // What the "to" html looks like.
- // <ul class="apple-social-actions-toolbar" role="toolbar" aria-label="Social Actions">
- // <li class="j-js-follow-controls"
- // data-streamsassoc="2"
- // data-location="jive-macros"
- // aria-live="polite" aria-atomic="true"> ... </li>
- // <li class="apple-actions"
- // role="toolbar"
- // aria-labelledby="jive-action-sidebar-tab-header_thread-actions-tab"> ... </li>
- // </ul>
- // ------------------------------------------
- // Modify web page downloaded function
- function cloneGotoChange()
- {
- var debug = 0;
- // alter updateUI
- /* document.getElementById('tag-id').innerHTML = '<ol><li>html data</li></ol>'; */
- // Find where to insert the code
- // We place the code at the beginning of the function
- var stringed = jspaginate["updateUI"].toString();
- var position = stringed.indexOf("{");
- position += 1;
- /* blank lines before and after so to avoid any abuttle problems */
- newCode = (`
- /* very strange, copying data seems to avoid firefox javascript abnormality.
- What a pain to find.
- This will be run evertime the user click on a change page icon.*/
- var passData = data;
- var rc = cloneGotoUpate(passData);
- if (rc) {console.log("--> updateUI returning from cloneGotoUpate rc = " + rc);}
- `);
- /* Heavy work of doing the change */
- var changedCode = stringed.substr(0, position)
- + newCode
- + stringed.substr(position);
- if ( debug ) console.log ("--> cloneGotoChange: new code:\n" + changedCode);
- // Inject the changed code
- // need to use the eval function to make a function assignment instead of a string assignment
- eval( 'jspaginate["updateUI"] =' + changedCode);
- if ( debug ) console.log("--> updateUI:\n"+jspaginate["updateUI"].toString() );
- /* Template for changing a function.
- // ...............................
- newCode = (`
- `);
- // Inject the changed code
- // need to use the eval function to make a function assignment instead of a string assignment
- eval( 'jspaginate["getData"] =' + newCode);
- if ( debug ) console.log("--> igetData:\n"+window.jspaginate["init"].toString() );
- */
- if ( debug ) console.log("--> cloneGotoChange returning. ");
- } // end of function cloneGotoChange
- // ------------------------------------------
- // Inject the script functions into the web page.
- function cloneGoto()
- {
- var debug = 0;
- // Create first <script> code
- var firstScript = document.createElement("script");
- // This is goint to be a little confusing since there is code inside of the quote marks.
- // begin embeded code ------->
- var nodeScript = document.createTextNode(`
- /*
- Javascript strips out line ends, so:
- Must not use // for comments
- Must end all statements with ;
- */
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- /* Inject the pagination html */
- function cloneGotoInsert()
- {
- var debug = 0;
- var aDate = new Date();
- console.log ("--> cloneGotoInsert: building pagination block " + aDate );
- /* pageinations are after the original post and after the last post.
- They only appear when there is more than one page. */
- try
- {
- if ( debug) console.log ("looking for pagination");
- var item = document.getElementsByClassName("j-pagination pagination top")[0];
- }
- catch(err)
- {
- /* Something otherthan pagination not found. */
- alert("==> cloneGotoInsert # 11: " + err.message );
- console.log ("--> cloneGotoInsert # 11: found an error ");
- console.log (err);
- return 11; /* Sad goodbye */
- }
- if ( debug ) console.log ("something returned. pagination");
- /*
- Some uncertainty over what is the best way. All work. :
- if ( item === undefined || item === null)
- if (typeof item==='undefined' || item===null)
- if ( item == null)
- */
- /* See if we found a discussion with one page which happens when no paginatin exists */
- if ( item == null)
- {
- if ( debug) console.log ("no pagination");
- return 0; /* We are done. Nothing to do. Are we sure? Needs checkingout. */
- }
- if ( debug) console.log ("assume pagination");
- /* Copy the <span> element and its child nodes */
- var clone = item.cloneNode(true);
- cloneGotoInsertCommon(clone);
- } /* end of cloneGotoInsert */
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- /* Inject the pagination html */
- function cloneGotoInsertCommon(htmlToInsert)
- {
- try
- {
- var debug = 0;
- if ( debug ) console.log ("--> starting cloneGotoInsertCommon");
- /* Buid data structure
- Create first <li> note */
- var firstLi = document.createElement("lI");
- /* \u00A0 is */
- var node = document.createTextNode(" \u00A0\u00A0\u00A0\u00A0 ");
- firstLi.appendChild(node);
- /* within div with the same name */
- var firstDiv = document.createElement("DIV");
- /* supply an id so this div is easier to find */
- firstDiv.setAttribute("id","ASCPaginationGotoIcons");
- /* Append pagination
- End with three paginations on a multipage document page */
- firstDiv.appendChild(htmlToInsert);
- /* Create second <li> node */
- var secondLi = document.createElement("LI");
- /* combine */
- secondLi.appendChild(firstDiv);
- /* so we need a new place to append when person isn't logged in :-( */
- if ( debug ) console.log ("--> cloneGotoInsertCommon: choice");
- /* Note: always returns a array. Nothing found, first element will be
- undefined */
- var whereTo = document.getElementsByClassName("apple-social-actions-toolbar")
- if ( whereTo[0] == null)
- {
- if ( debug ) console.log ("--> user not signed in");
- /* user not signed in. We have to find a different place to put. jive-breadcrumb*/
- var signedOutId = document.getElementById("menubar");
- if ( signedOutId == null)
- {
- alert("==> cloneGotoInsert # 16: no place found for not signed in." );
- console.log ("--> cloneGotoInsert #16: no place found for not signed in. ");
- return 0; /* We are done. Nothing to do. */
- }
- if ( debug ) console.log ("found place to put pagination info when user not signed in");
- // firstDiv.setAttribute("style","margin-bottom: -120px !important;"); */
- /* Append the cloned <span> element to follow [pulldown] */
- signedOutId.appendChild(secondLi);
- }
- else
- {
- if ( debug ) console.log ("signed");
- /* Logged in path */
- whereTo[0].appendChild(firstLi);
- /* Append the cloned <span> element to follow [pulldown] */
- whereTo[0].appendChild(secondLi);
- }
- } /* end of try */
- /* avoid repeated errors at least
- first get will fail when there is only one page. */
- catch(err)
- {
- alert("==> cloneGotoInsert # 12: " + err.message );
- console.log ("--> cloneGotoInsert #12: found an error ");
- console.log (err);
- return 12; /* Sad goodbye */
- }
- } // end of function cloneGotoInsertCommon
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- /* called when user click on a pagination icon */
- function cloneGotoUpate(htmlToInsert)
- {
- var debug = 0;
- var aDate = new Date();
- console.log ("--> cloneGotoUpdate: building pagination block " + aDate );
- /* remove old pagination html */
- try
- {
- var whereTo = document.getElementById("ASCPaginationGotoIcons")
- if ( whereTo )
- {
- if ( debug ) console.log ("--> cloneGotoUpdate: found pagination");
- /* Remove top of page pagination */
- whereTo.parentNode.removeChild(whereTo);
- if ( debug ) console.log ("--> cloneGotoUpdate: pagination removed");
- }
- else
- {
- if ( debug ) console.log ("--> cloneGotoUpdate: no pagination found");
- }
- }
- catch(err)
- {
- alert("==> cloneGotoUpdate # 17: " + err.message );
- console.log ("--> cloneGotoUpdate #17: found an error ");
- console.log (err);
- return 17; /* Sad goodbye */
- }
- /* htmlToInsert contains a little extra data, like the whole bottom of the page.
- strip off after:
- <!-- END pagination -->
- */
- var position = htmlToInsert.indexOf("<!-- END pagination -->");
- if ( debug ) console.log ("--> cloneGotoUpdate: ending start of line position " + position);
- position = htmlToInsert.indexOf("-->",position);
- if ( debug ) console.log ("--> cloneGotoUpdate: near ending line position " + position);
- position += 3;
- if ( debug ) console.log ("--> cloneGotoUpdate: ending line position " + position);
- if ( debug ) console.log ("--> cloneGotoUpdate: htmlToInsert typeof = " + typeof htmlToInsert);
- if ( debug ) console.log("running data = "+htmlToInsert);
- var justPaginate = htmlToInsert.substr(0, position);
- if ( debug ) console.log ("--> cloneGotoUpdate: justPaginate typeof = " + typeof justPaginate);
- if ( debug ) console.log("running data = "+justPaginate);
- /* example:
- document.getElementById('tag-id').innerHTML = '<ol><li>html data</li></ol>'; */
- var firstSpan = document.createElement("span");
- firstSpan.innerHTML = justPaginate;
- /* add back the pagination. Assumed we end up with an updated pagination */
- var rc = cloneGotoInsertCommon(firstSpan);
- if (rc) {console.log("--> cloneGotoUpdate: returned from cloneGotoInsert rc = " + rc);}
- return 0;
- } /* End of cloneGotoUpate */
- `);
- // <----------- end embedded code.
- // Were back to normal run of the mill code that will ultimately insert the above code
- firstScript.appendChild(nodeScript);
- // Insert the script text
- try
- {
- var isASC = document.getElementById("body-apple");
- if ( isASC == null)
- {
- console.log ("--> cloneGotoInsert # 13: Does not have the layout of an ASC web page.");
- return 0; /* We are done. Ignore non ASC web page. Greasemonkey shouldn't have called us. */
- }
- /* Found a ASC web page */
- isASC.appendChild(firstScript);
- }
- catch (err)
- {
- alert ("==> cloneGotoInsert # 14: " + err.message);
- console.log("--> cloneGotoInsert # 14: Got some odd error when looking for script insertion point. ");
- console.log("--> " + err.message)
- console.log(err);
- return 14; // Sad goodbye
- }
- if ( debug ) console.log("--> cloneGotoInsert proceeding ");
- try
- {
- // Now that we moved the script inline, run it
- // Insert pagination at the top of the page.
- var cloneGotoInsertRc = cloneGotoInsert();
- if (cloneGotoInsertRc) {
- console.log("--> cloneGotoInsert rc = " + clonecloneGotoInsertRc);
- }
- }
- // avoid repeated errors at least
- // first get will fail when there is only one page.
- catch(err)
- {
- alert("==> cloneGotoInsert # 15: When inserting pagination, received this message \n"
- + err.message );
- console.log ("--> cloneGotoInsert # 15: found an error when inserting pagination");
- console.log (err);
- return 15; // Sad goodbye
- }
- if (debug) {console.log("--> cloneGoto calling cloneGotoChange()");}
- // Now all we have to do is modify jspaginate.init so we can fix up out added
- // paginate information at the top right of the page
- // Has a tap bit of a side effect.
- cloneGotoChange();
- if ( debug ) console.log("returning from cloneGoto");
- return 0;
- } // end of function cloneGoto
- // ----------------------------- Start of running code --------------------------------------------
- var debug = 0;
- // collect rc.
- var rc = cloneGoto();
- if (rc) {console.log("--> Last rc = " + rc);}
- if ( debug ) console.log ("--> : all ... done");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement