0

Hey guys I have a tamper monkey script that I'm writing to scrape links off a webpage, store them into an array, then somehow going through each link of the array to grab information inside those links.

So lets say I have an array "turls" with 25 links scraped from the main page, and I use window.location.href to go into the links and window.history.back(); to go back to the main page. Once I return to the main page, the script would run again, and go into the first link again.

I think I can go on to the next link after I comeback to the main page where all the links are with GM_setvalue and GM_getvalue, but how? I'm not sure how to go about this to go through all 25 links.

thanks in advance,

(also the console.log result of urls and turls show up in the chrome console as an array of 300 and an array of 25, but urls and turls isnt defined when I typed console.log(urls) or console.log(turls) into the chrome console.)

// ==/UserScript==


(function() {
'use strict';

   var urls= [];
var turls = [];


$( document ).ready(function() {
    for (var i= document.links.length; i-->0;){
        if (document.links[i].hostname===location.hostname){
            if (document.links[i].href.indexOf("tournaments") > -1) {
                turls.push(document.links[i].href);    
            }
            urls.push(document.links[i].href);
        }
    }    
});


console.log(urls); 
console.log(turls);  






})();
Ray Lu
  • 51
  • 9
  • Is `i-->0;` a typographical error? Do you mean `for (var i= document.links.length; i >= 0; i--) {`? – guest271314 May 16 '17 at 03:25
  • 1
    works either way I think. http://stackoverflow.com/questions/20754582/what-does-while-i-0-mean – Ray Lu May 16 '17 at 03:32
  • What is issue with `javascript` at Question? – guest271314 May 16 '17 at 03:34
  • I dont know how to go to the next link in the array turls or urls everytime the page reloads. Basically every time the page reloads I want to be going into the next link. From turl[0] to turl[1] to turl[2].... – Ray Lu May 16 '17 at 03:36
  • You can store array at `localStorage`, use `.shift()` to remove element at index `0` at `load` event of `window`, see also [Global Variable usage on page reload](http://stackoverflow.com/questions/29986657/global-variable-usage-on-page-reload/) – guest271314 May 16 '17 at 03:39

1 Answers1

0

Try this.

// @grant GM_setValue
// @grant GM_getValue

(function() {
  'use strict';

  var turls = GM_getValue('turls', []);
  if(turls.length == 0) {
    $(document).ready(function() {
      for (var i = 0; i<document.links.length; i++) {
        if (document.links[i].hostname === location.hostname) {
          if (document.links[i].href.indexOf("tournaments") > -1) {
            turls.push(document.links[i].href);
          }
        }
      }
    });
  }

  if(turls.length > 0) {
    var turl = turls.shift();
    GM_setValue('turls', turls);
    window.location.href = turl;
  }
})();

To persist values in your TamperMonkey script as the window loads, i.e. forces your script to reload, you need to use GM_setValue and GM_getValue to store and retrieve values across sessions.

EyuelDK
  • 3,029
  • 2
  • 19
  • 27