Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name ASC Display links to last page on all pages of discussion.
- // @namespace bubo-bubo/gmscripts
- // @description ASC - Add last page button on list pages
- // @include https://discussions.apple.com/content?*
- // @include https://discussions.apple.com/community/*/content?*
- // @version 1
- // @grant none
- // ==/UserScript==
- // Uses Hiroto's event handler design.
- /*
- Add link to last page of long discussions.
- Supported lists:
- https://discussions.apple.com/content?filterID=following~objecttype~objecttype[thread]
- https://discussions.apple.com/community/ipad/ipad_in_business_and_education/content?filterID=contentstatus
- %5Bpublished%5D%7Eobjecttype%7Eobjecttype%5Bthread%5D
- https://discussions.apple.com/community/mac_os/os_x_el_capitan/content?filterID=contentstatus[published]
- ~objecttype~objecttype[thread]
- but not:
- https://discussions.apple.com/community/lounge/full_host_bar
- Why does apple slow down old devices? 66 18035 February 2, 2017 9:15 PM
- in Using iPad by gail from maine by gail from maine
- https://discussions.apple.com/thread/7527129
- https://discussions.apple.com/thread/7527129?start=15&tstart=0
- https://discussions.apple.com/thread/7527129?start=30&tstart=0
- https://discussions.apple.com/thread/7527129?start=45&tstart=0
- https://discussions.apple.com/thread/7527129?start=60&tstart=0
- https://discussions.apple.com/community/mac_os/os_x_yosemite/content?filterID=contentstatus%5Bpublished%5D~objecttype~objecttype%5Bthread%5D*/
- var watchdog1, watchdog2;
- var watchInterval = 500; // [ms] Waiting for the user to change page
- var shortWatchInterval = 100; // [ms] Waiting for the system to load new list
- var debug = 0; // 0 -- no debug
- // 1 -- normal debug
- // 2 -- intense debug
- var priorLastThread = '';
- var aDate = new Date();
- console.log ("--> lastReadPageButton: add fast links. Debug is " + debug +
- " on " + aDate );
- // Setup once page is loaded.
- // modify thread content view behaviour (in post-load phase)
- if (debug) console.log('Loaded; Now, initializing');
- // register event listeners
- window.addEventListener('unload', function(e)
- {
- if (debug) console.log('unloading.');
- stopWatchDogButton(watchdog1);
- stopWatchDogButton(watchdog2);
- window.removeEventListener('locationChangeButton',
- locationChangeButtonHandler,
- true);
- window.removeEventListener('waitingInsertLastPageAnchor',
- insertLastPageAnchor,
- 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);
- if (debug) console.log('adding locationChangeButton event listner');
- window.addEventListener('locationChangeButton',
- locationChangeButtonHandler,
- true);
- window.addEventListener('waitingInsertLastPageAnchor',
- insertLastPageAnchor,
- true);
- // Engage a watch dog
- var prev_href = '';
- if (debug) console.log('prev_href is ' + prev_href );
- // Spin around here watching for the web address to change.
- // Always spinning here.
- // It takes awhile after the page address changes before the list is loaded
- // so we don't need to check that often.
- watchdog1 = setInterval( function()
- {
- var curr_href = window.location.href;
- if (debug >= 2) console.log(' prev_href is ' + prev_href +
- "\n curr_href is " + curr_href);
- if (curr_href != prev_href)
- {
- // Page changed
- if (debug) console.log('---> found new page. ' +
- ' curr_href is ' + curr_href +
- ' Dispatching short event.');
- window.dispatchEvent(new Event('locationChangeButton'));
- prev_href = curr_href;
- }
- },
- watchInterval); // end of setting watch dog #1
- // -----------------------------------------------------------------------------
- function locationChangeButtonHandler(e)
- {
- if (debug) console.log('---> locationChangeButtonHandler is observed. '+
- "Debug is " + debug);
- // Give a little time for the page to settle down.
- // Spin around here watching for the web address in the last entry to change.
- // Stop spinning once change is found
- watchdog2 = setInterval( function()
- {
- var trSectionSpin = new Array();
- trSectionSpin = $("table tr.js-browse-item td.j-td-title");
- if (debug>=2) console.log("finding title" );
- // .size() is in Jquery and other many other lib
- rememberLastThread = $("a:first",
- trSectionSpin[trSectionSpin.size()-1]
- ).attr("href");
- if (debug) console.log("last thread on page is " + rememberLastThread);
- if (priorLastThread != rememberLastThread)
- {
- // Page changed
- if (debug) console.log('---> found new page: ' +
- rememberLastThread + ' Dispatching ' +
- 'insert anchors event.');
- window.dispatchEvent(new Event('waitingInsertLastPageAnchor'));
- priorLastThread = rememberLastThread;
- stopWatchDogButton(watchdog2);
- }
- },
- shortWatchInterval); // end of setting watch dog
- }
- // -----------------------------------------------------------------------------
- function stopWatchDogButton(dog) {
- clearInterval(dog);
- if (debug) console.log('Cleared interval for : ' + dog);
- }
- // -----------------------------------------------------------------------------
- var replyCounts = new Array();
- var titleCounts = new Array();
- var done = 0;
- var entrySize = 15; // number of discussion listed on web page
- var lastPageStart = 0;
- var constructedLink = "";
- var insertA = "";
- var rememberLastThread = "";
- var debugStop = 0;
- function insertLastPageAnchor()
- {
- /* debugStop++;
- if ( debugStop > 5 ) throw new Error();*/
- if (debug) console.log('\nInserting last page anchors');/**/
- try
- {
- $("table tr.js-browse-item td.j-td-replies").each(
- function( index )
- {
- replyCounts[index] = $( this ).text();
- console.log("index is " + index );
- if (debug>=2) console.log("reply index is " + index +
- " for " + $( this ).text() );
- }
- ); // End of each
- }
- catch(err)
- {
- console.log("within insertLastPageAnchor got error of " + err.message );
- return;
- }
- console.log("here...");
- if (debug) for ( i = 0;i<replyCounts.length;++i )
- {
- console.log("reply " + i + " is " + replyCounts[i] );
- }
- if (debug) console.log("Find the titles");
- var trSection = new Array();
- trSection = $("table tr.js-browse-item td.j-td-title");
- $("a:first", trSection ).each(
- function( index )
- {
- titleCounts[index] = $( this ).text();
- if (debug) console.log("------ " + $( this ).html() + " ------");
- postNumber = $( this ).attr("href").split("/")[2].trim();
- if (debug) console.log("a post number is " + postNumber);
- mathFloorInt = Math.floor(replyCounts[index]/entrySize);
- console.log ("repies = " + replyCounts[index] + " mathFloorInt is " +
- mathFloorInt );
- if (mathFloorInt)
- {
- lastPageStart = mathFloorInt * entrySize;
- constructedLink = "https://discussions.apple.com/thread/" +
- postNumber +
- "?start=" +
- lastPageStart +
- "&tstart=0";
- if (debug) console.log("link is " + constructedLink);
- insertA = '<span class="rcUserASCModLastReadPage">' +
- '<a style=" color: SlateBlue; ' +
- 'margin-left: 15px; border: 1px dotted black;" ' +
- 'href="' +
- constructedLink +
- '">Last Page</a>' +
- '</span>';
- if (debug) console.log("inserted is " + insertA);
- $( this ).after( insertA );
- }
- } // end of anonymous function
- ); // end of .each
- } // end of function insertLastPageAnchor
- // Seems that javascript [ or someone ]
- // sends the last value from an assignment statement as the return code to the caller.
- done = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement