1

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,''));

}
Aman Chawla
  • 704
  • 2
  • 8
  • 25
  • Are these tabs the same domain or cross domain? It makes a huge difference in what techniques are available. – Brock Adams Aug 21 '13 at 02:30
  • The same domain. Examples: http://www.worldportsource.com/ports/USA_AL_Bevill_Hook_Port_4839.php and http://www.worldportsource.com/ports/USA_AL_Port_of_Columbia_3562.php – Aman Chawla Aug 21 '13 at 02:38
  • I see you've already accepted an answer but, since this is same domain, you should use `localStorage` rather than `GM_setValue`. That way handles much more data and is less likely to crash or bog down the browser. Tabs can also listen for localStorage changes. For help pasting to a file, check out `GM_setClipboard`. – Brock Adams Aug 21 '13 at 03:59
  • GM_setClipboard looks awesome, is there a limit to how much it (or the clipboard) can hold? – Aman Chawla Aug 21 '13 at 14:08
  • I don't know what the limit is. I imagine it's in the low megabytes. I'll find out if it becomes an issue for me. ;) – Brock Adams Aug 21 '13 at 22:41

1 Answers1

2

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);
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • So I'm having trouble with this. I thought I would `GM_setValue("foo",GM_getValue("foo")+"/n"+str);` But I'm getting weird reference errors. How can I set this up such that it tests if the variable exists, appends if it does or creates if it doesnt? – Aman Chawla Aug 21 '13 at 02:36
  • Got it working, thanks! wish I could give you a vote but I have 14 reputation, need 15 to vote =( – Aman Chawla Aug 21 '13 at 03:44