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. Part #1.
- // @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 11Mar2017
- // @grant none
- // ==/UserScript==
- var howToOpen = "no"; // "new" Open the display of posts in a new page or tab.
- // How the user set their web browser determines which one.
- // Setting anyother value like "no" will open the link as before.
- // 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]
- https://discussions.apple.com/community/mac_os/mac_os_x_technologies/content
- but not:
- https://discussions.apple.com/community/mac_os/mac_os_x_technologies
- 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 = 2; // 0 -- no debug
- // 1 -- normal debug
- // 2 -- intense debug
- var priorLastThread = '';
- var aDate = new Date();
- console.log ("--> lastReadPageButton: add pulldown with 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>=2) 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 +
- ' <======');
- 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>=2) console.log("last thread on page is " + rememberLastThread);
- rememberLastThread = parseHref(rememberLastThread);
- if (debug) console.log("adjusted 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 parseHref(hrefString) {
- /* input: ASC thread URL
- output: thread id */
- /*
- from ASC:
- <a href="/thread/7903386 ">Find and delete duplicates within Photos</a>
- after I change the href:
- <a href="https://discussions.apple.com/thread/7900219?start=15&tstart=0&begin=last">
- Applescript to merge CSV files</a>
- /thread/7903386
- https://discussions.apple.com/thread/
- /thread/7903386
- 012345678901234567890123456789012345678901234567890123456789
- 0 1 2 3 4 5 */
- var threadId = "";
- if (debug>=2) console.log('input hrefString is ' + hrefString);
- if (hrefString.substr(0, 8) == "/thread/")
- {
- threadId = hrefString.split("/")[2];
- if (debug>=2) console.log('ASC threadId is ' + threadId);
- }
- else if (hrefString.substr(0, 37)() == "https://discussions.apple.com/thread/" )
- {
- if (debug>=2) console.log('hrefString.split("?")[0].split("/")[4] is ' + hrefString.split("?")[0].split("/")[4]);
- threadId = hrefString.split("?")[0].split("/")[4];
- if (debug>=2) console.log('my altered threadId is ' + threadId);
- }
- return threadId;
- }
- // -----------------------------------------------------------------------------
- function stopWatchDogButton(dog) {
- clearInterval(dog);
- if (debug) console.log('Cleared interval for : ' + dog);
- }
- // -----------------------------------------------------------------------------
- function zapPageInAnchor(passOption)
- {
- console.log("clicked.");
- /* What was clicked? */
- /* var actualValue = passedOption.options[passOption.selectedIndex].text;
- if (debug) console.log("actualValue is " + actualValue); */
- }
- // -----------------------------------------------------------------------------
- function insertLastPageAnchor()
- {
- 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;
- 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();
- 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;
- }
- if (debug>=2) console.log("here...");
- if (debug>=2) 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();
- /* Display Discussion Title */
- if (debug) console.log("------ " + $( this ).html() + " ------");
- /* Open in new tab or page? */
- if (howToOpen == "new")
- {
- $(this).attr("target","_blank");
- }
- postNumber = $( this ).attr("href").split("/")[2].trim();
- if (debug) console.log("a post number is " + postNumber);
- mathFloorInt = Math.floor(replyCounts[index]/entrySize);
- if (debug) console.log ("repies = " + replyCounts[index] + " mathFloorInt is " +
- mathFloorInt );
- if (mathFloorInt)
- {
- lastPageStart = mathFloorInt * entrySize;
- if (debug) console.log ("... lastPageStart is " + lastPageStart +
- " mathFloorInt = " + mathFloorInt +
- " entrySize is " + entrySize );
- constructedLink = "https://discussions.apple.com/thread/" +
- postNumber +
- "?start=" +
- lastPageStart +
- "&tstart=0&begin=last";
- if (debug) console.log("link is " + constructedLink);
- /* Make last link the default for the title link */
- $(this).attr("href",constructedLink);
- /* How the select tag looks:
- <select>
- <option value="volvo">Volvo</option>
- <option value="saab">Saab</option>
- <option value="mercedes">Mercedes</option>
- </select>
- */
- /*
- https://discussions.apple.com/thread/7889277?
- 012345678901234567890123456789012345678901234567890123456789
- 0 1 2 3 4 5 */
- insertA = '<select class="rcUserASCItemSelect"' +
- 'onchange="var debug = 1;' +
- 'if (debug) console.log(\' clicked.\');' +
- 'var actualText = this.options[this.selectedIndex].text;' +
- 'if (debug) console.log(\'actualText is \' + actualText);' +
- 'var actualValue = this.options[this.selectedIndex].value;' +
- 'if (debug) console.log(\'actualValue is \' + actualValue);' +
- 'var searchString = actualValue.substr(0, 45);' +
- 'if (debug) console.log(\'searchString is \' + searchString);' +
- 'var doubleQuote = String.fromCharCode(34);' +
- 'var searchFor = \'a[href^=\' + doubleQuote + searchString + doubleQuote + \']\';' +
- 'if (debug) console.log(\'searchFor is \' + searchFor);' +
- 'try{$(searchFor).attr(\'href\',actualValue);}catch(e){console.log(\'e is \'+e);}' +
- 'if (debug) console.log(\'Update completed \');' +
- '">' +
- '<option value="'+
- constructedLink +
- '" selected>Last</option>'
- /* ansylis by induction
- 68/15 is 4.53
- last should be 4
- pull down should be 3,2,1
- 60/15 is 4
- pull down should be 3,2,1
- 59 is 3.93
- pull down should be 2,1
- 16/15 is 1.06
- pull down nothing in pull down ...
- 15/15 is 1
- 14/15 is 0.93
- */
- var iStart = Math.floor(lastPageStart/entrySize)
- if (debug) console.log("iStart, number of pages, is " + iStart);
- if (iStart > 1 ) {
- /* the Last entry for highest page number */
- iStart--
- if (debug>=2) console.log("iStart is " + iStart);
- for (var i = iStart ; i>0 ; i--)
- {
- pageStart = i * entrySize;
- pageConstructedLink = "https://discussions.apple.com/thread/" +
- postNumber +
- "?start=" +
- pageStart +
- "&tstart=0&begin=" +
- i.toString();
- if (debug>=2) console.log("link for " + i.toString() + " is " +
- pageConstructedLink);
- var displayPage = (i + 1).toString() ;
- if (debug>=2) console.log("displayPage is " + displayPage);
- insertA = insertA +
- '<option value="' +
- pageConstructedLink +'">' +
- displayPage +
- '</option>';
- }
- } // end of if
- if (debug>=2) console.log("Add beginning " );
- /* put in beginning option in case user what to go to the beginning of the display of posts */
- pageStart = "0";
- var displayPage = "1";
- pageConstructedLink = "https://discussions.apple.com/thread/" +
- postNumber +
- "?start=" +
- pageStart +
- "&tstart=0&begin=" +
- displayPage;
- if (debug) console.log("link for is " + pageConstructedLink);
- /* Add Option */
- insertA = insertA +
- '<option value="' +
- pageConstructedLink +
- '">' +
- "beginning" +
- '</option>';
- insertA = insertA + "</select>"
- if (debug>=2) console.log("inserted is " + insertA);
- // Place in the document to insert selection.
- try
- {
- $( this ).after( insertA );
- }
- catch(e)
- {
- console.log('e is '+e);
- }
- } // end of if (mathfloor)
- } // 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