Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name ASC adjust to last page on display of posts. Part #2
- // @namespace bubo-bubo/gmscripts
- // @description ASC - Add last page button on list pages
- // @include https://discussions.apple.com/message/*
- // @include https://discussions.apple.com/thread/*
- // @include https://discussions.apple.com/docs/*
- // @version 4Apr2017
- // @grant none
- // ==/UserScript==
- /* We may not end up on the last page of a display of posts when Apple hosts
- deleted some posts.
- https://discussions.apple.com/docs/DOC-10241?start=45&tstart=0&begin=Prior-1
- https://discussions.apple.com/thread/5469616?start=1020&tstart=0&begin=yes
- How the input web address is contructed.
- constructedLink = "https://discussions.apple.com/thread/" +
- postNumber +
- "?start=" +
- lastPageStart +
- "&tstart=0&begin=yes";
- */
- var debug = 2; // 0 -- no debug
- // 1 -- normal debug
- // 2 -- intense debug
- var aDate = new Date();
- console.log ("--> adjustLastPage: add fast links Part #2. Debug is " + debug +
- " on " + aDate );
- // Setup once page is loaded.
- if (debug) console.log('Loaded; Now, initializing');
- var url = window.location.href;
- var params = url.split('?');
- if (debug) console.log("web page parms " + params[1]);
- // See if we have already adjusted this page.
- try
- {
- pageStatus = Window.asc_jx_last_page_load_remember;
- }// End of try
- catch(err)
- {
- console.log("looking for variable pageStatus " + err.message );
- //return;
- }
- if (debug) console.log("pageStatus is " + pageStatus);
- myPassVar = getURLParameter('begin').toLowerCase();
- if (debug) console.log("begin parm is " + myPassVar);
- if (debug) console.log("myPassVar.substr(0,6) " + myPassVar.substr(0,6));
- // Do we need to see if we need to adjust to the last page?
- if ( ( myPassVar == "yes"|| myPassVar == "last" || myPassVar.substr(0,6) == "prior-") && pageStatus != "yes")
- {
- // Global remember, so we can check if we already adjusted this page
- Window.asc_jx_last_page_load_remember = "yes";
- if (debug>= 2) console.log("Have set Window.asc_jx_last_page_load_remember to " +
- Window.asc_jx_last_page_load_remember);
- currentPageStart = getURLParameter('start');
- if (debug) console.log("start parm is " + currentPageStart);
- // Check what page we are on.
- var pageNumbers = $("span.current-page").text();
- if (debug) console.log("pageNumbers " + pageNumbers);
- // Ejaz explans how to pull words from a string
- // in http://stackoverflow.com/questions/16223043/get-second-and-third-words-from-string
- // The technique using split() will fail if the string has multiple spaces in it.
- // Don't forget to check your array length using .length property to avoid
- // getting undefined in your variables.
- var actualPages = pageNumbers.match(/\S+/gi);
- if (debug) console.log("closer "+ actualPages);
- var currentPage = parseInt(actualPages[1]);
- var lastPage = parseInt(actualPages[3]);
- if (debug) console.log("currentPage " + currentPage.toString() );
- if (debug) console.log("lastPage " + lastPage.toString() );
- if (myPassVar.substr(0,6) == "prior-")
- {
- /*
- <a href="/thread/7899185?start=90&tstart=0"
- title="Select to go to last page"
- role="button"
- aria-label="Select to go to last page"
- onclick="jspaginate.init('last', '7'); return false;"
- class="js-pagination-next j-paginate-last">last</a>
- */
- var lastInfo = $("a.js-pagination-next.j-paginate-last")
- var lastHref = $(lastInfo).attr("href")
- if (debug>= 2) console.log("lastHref is " + lastHref);
- /* Find where the last page starts. With this, we can figure out
- the post per page.
- lastHref is /thread/7899185?start=90&tstart=0
- */
- var pageStartCount = parseInt(lastHref.split("=")[1].split("&")[0].replace(/%2C/g, ''))
- if (debug>= 2) console.log("pageStartCount is " + pageStartCount);
- var itemsPerPage = pageStartCount/(lastPage-1) // 15
- if (debug>= 2) console.log("itemsPerPage is " + itemsPerPage);
- var backPrior = parseInt(myPassVar.split("-")[1])
- if (debug>= 2) console.log("backPrior is " + backPrior);
- if (debug>= 2) console.log("(currentPage + backPrior) " + (currentPage + backPrior));
- if ((currentPage + backPrior) != lastPage )
- {
- if (debug) console.log( "Need to adjust. lastPage(backPrior *itemsPerPage) is " +
- ( (lastPage-backPrior-1) * itemsPerPage ) +
- " currentPage is " +
- (lastPage-backPrior) );
- /* count of posts starts at zero whereas page count starts at 1 */
- jspaginate.update(
- ( (lastPage-backPrior-1) * itemsPerPage ), // target
- (lastPage-backPrior) // current
- )
- }
- else
- {
- if (debug) console.log("on correct page which is " + currentPage);
- }
- } // Processing for prior
- else
- {
- // If the current page is differernt than the last page, apple removed some post
- // and we need to adjust to last page.
- if ( currentPage != lastPage )
- {
- if (debug) console.log("mismatched pages ");
- // Adjust to the last page.
- // Handy dandy ASC function is used when user clicks on last button, so use it
- jspaginate.init('last', actualPages[3]);
- if (debug) console.log("Page adjusted! " );
- }
- }
- }
- else
- {
- if (debug) console.log("... Nothing to do! ...");
- }
- // Seems that javascript [ or someone ]
- // sends the last value from an assignment statement as the return code to the caller.
- done = 0;
- // ----------------------------------------------------------------------------------------
- //
- // http://stackoverflow.com/questions/11582512/how-to-get-url-parameters-with-javascript
- //
- // example:
- // myvar = getURLParameter('myvar');
- function getURLParameter(name) {
- return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) ||
- [null, ''])[1].replace(/\+/g, '%20')) || null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement