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

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

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

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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'));
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Excellent, thanks Weirdan. :)
Post Reply