Aman Chawla
Aman Chawla

Reputation: 724

How can I save greasemonkey outputs across numerous tabs

I have a working greasemonkey script that adds a button to a page. When clicked the button scrapes some information from the page, puts it into a variable called str.

Right now I'm printing the variable to the page, which is working fine. I need to eventually make a file with this output from several similar pages.

For example:

output from one page might be "abcdef" and from the second page is "ghijkl". I am copying it one at a time to a text file to make:

abcdef
ghijkl

Is there a way to automatically save these to the same variable and just keep appending the new output to the variable? I know it isn't possible to write to a file from greasemonkey, but don't really know what to do in this situation. There are about 100 pages so I don't want to copy paste 100 times (I'm fine opening each page and clicking the button, that isnt terrible).

This is my code:

// Create a new div to put the links in and append it to the content div
links_div1 = document.createElement('div');//makes a div
//links_div1.setAttribute('class', 'box generic_datatable dataTable');//makes it look the same as rest of page
//links_div1.setAttribute('id', 'normal_wrapper');

//adds div to beginning of content section
var node = document.getElementById('navHeaderCell');
var parentDiv = node.parentNode;
parentDiv.insertBefore(links_div1,node);

//makes buttons in new divs
links_div1.innerHTML += '<table class="box generic_datatable dataTable" id="normal"><thead><tr class="colhead"><th></th></tr></thead><td>Scrape: <button type="button" id="btn_id">scrape</button></td></table>';

//makes them clickable
addButtonListener();

 function addButtonListener() {
    //first div
    document.getElementById("btn_id").addEventListener("click", function(){scrape()}, true);
    //alert("function");
} 

function scrape() {
    //alert(array);
    str = array.join('$');
    links_div1.innerHTML += str;

    }



//collect all the values in a NodeList
var linksToOpen = document.querySelectorAll ("#content_3col>table.form>tbody>tr>td.dash:nth-of-type(2)");

//alert(linksToOpen);

//--- linksToOpen is a NodeList, we want an array of links...
var array  = [];
for (var J = 0, numLinks = linksToOpen.length;  J < numLinks;  ++J) {

    array.push (linksToOpen[J].innerHTML.replace(/<[^>]*>/g,''));

}

Upvotes: 1

Views: 1034

Answers (1)

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

Most UserScript environments provide the very useful

GM_setValue and GM_getValue functions.

You do have to be careful about race conditions though, so I would be tempted to have each tab generate a random number (perhaps based on time) and then put their stuff in a key based upon that value.

Then, at the end, concatenate the strings together and display one large string at the end that can be cut-and-pasted.

Or, if you'd rather not cut-and-paste, you can generate a massive string and use the HTML5 download attribute.

Edit, based on OP's comment

(Typed on the fly, untested.)

// The documentation talks about the second parametere here -- a default value.
var value = GM_getValue("value", "");
value += str + "\n";
GM_setValue("value", value);

While all UserScript methods should understand that, there is also this:

var value = GM_getValue("value");
if (value === undefined) {
  value = "";
}
value += str + "\n";
GM_setValue("value", value);

or even:

var value = GM_getValue("value") || "";
value += str + "\n";
GM_setValue("value", value);

Upvotes: 2

Related Questions