Page 1 of 1

Greasemonkey + Javascript- find a string in the current doc?

Posted: Sun Oct 21, 2007 12:49 pm
by Josh1billion
I don't know much about Javascript, but I would like to make a greasemonkey script that does the following:

1. Searches the current HTML document for a certain string and pulls a value from there.. example: "type=hidden value=3918", it would pull the 3918

2. Saves that information to the user's hard drive for later retrieval within the same script.

Is this possible?

Posted: Sun Oct 21, 2007 6:41 pm
by Weirdan
1. Searches the current HTML document for a certain string and pulls a value from there..
From your example it appears you'd better be searching for hidden form elements instead, like this:

Code: Select all

var hiddens = document.evaluate(
    '//input[@type="hidden" and @value]',
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);
for (var i = 0; l = hiddens.snapshotLength; i < l; i++) {
    var hidden = hiddens.snapshotItem(i);
    // do somethign to hidden
}
2. Saves that information to the user's hard drive for later retrieval within the same script.
That's easy, just use GM_setValue and GM_getValue functions:

Code: Select all

  // store value:
  GM_setValue('name', 'value');
  // retrive value:
  alert(GM_getValue('name'));

Posted: Mon Oct 22, 2007 12:45 pm
by Josh1billion
Excellent, thanks Weirdan. :)