Click to See Complete Forum and Search --> : Simple user JS modifying


dave_is_sexy
12-29-2005, 04:22 AM
I'm trying to write a script for Firefox's Greacemonkey User Javascript extension. I want the script to check a page for the text:

8%><a href="javascript:ViewProfile('SOMETHING1','SOMETHING2',SOMETHING3);">

and replace each instance with

8%><a href="http://www.website.com/SOMETHING2" target=_blank>

Where SOMETHING* are variables which change in each occurance.
The 8% is important as it is the element common to the table column cells I want to alter.

I tried this, it's incomplete but doesn't do anything at all.

(function() {
var scriptBefore = javascript:ViewMode('SOMETHING1','
var scriptAfter = 'http://www.website.com/'
var xpath = "//a[contains(@href, scriptBefore)]";
var res = document.evaluate(xpath, document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var i, link;
for (i = 0; link = res.snapshotItem(i); i++) {
link.href = link.href.replace(scriptBefore, scriptAfter);
}
})();


By my understanding that script should've generated the url:

8%><a href="http://www.website.com/SOMETHING2',SOMETHING3);">


And I've gathered that this is helpful for grabbing SOMETHING2 but don't know how it fits in with things:

obj.href.split("'")[2]


obj.href = 'http:.....' + obj.href.split("'")[2]

-----------------------------------------------------------------------

Another solution would be to replace the function ViewMode in the source with the function ViewMode2.

I could then specify function ViewMode2:

function ViewMode2(fUser,tUser,sId)
{
window.open('http://www.website.com/'+tUser,'status=no,scrollbars=yes,resizable=yes');
}

dave_is_sexy
12-30-2005, 09:25 AM
Anyone? Just the function replace would be fine :)

methodpg
12-30-2005, 10:04 AM
Have you tried:

var scriptBefore = "javascript:ViewMode('SOMETHING1','";

instead of

var scriptBefore = javascript:ViewMode('SOMETHING1','

dave_is_sexy
12-30-2005, 10:11 AM
Thanks for that. I guess writing it propperly would help. I adjusted both before & after lines but unfortunately the script still has no effect on anything

methodpg
12-30-2005, 10:39 AM
Well.. in that case.. personally.. I'd change


for (i = 0; link = res.snapshotItem(i); i++) {
link.href = link.href.replace(scriptBefore, scriptAfter);
}


To something like


for (i = 0; link = res.snapshotItem(i); i++) {
alert('link.href='+link.href+'\nscriptBefore='+scriptBefore+'\nscriptAfter'+scriptAfter;
link.href = link.href.replace(scriptBefore, scriptAfter);
}


To help debug it.

methodpg
12-30-2005, 10:41 AM
Also... if res.snapshotItem is not a function.. but an array..

use square brackets instead. (eg. res.snapshotItem[i])

(I admit I'm not sure on that one.. but it's worth a try if nobody else can help you.)

dave_is_sexy
12-30-2005, 04:19 PM
Thanks Methodpg. Unfortunately, still no luck.
MY code is now:

(function() {
var scriptBefore = "ViewMode";
var scriptAfter = "ViewMode2";
var xpath = "//a[contains(@href, scriptBefore)]";
var res = document.evaluate(xpath, document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var i, link;
for (i = 0; link = res.snapshotItem(i); i++) {
alert('link.href='+link.href+'\nscriptBefore='+scriptBefore+'\nscriptAfter'+scriptAfter;
link.href = link.href.replace(scriptBefore, scriptAfter);
}
})();

And the goal is to monitor a page for function ViewMode and replace it with either ViewMode2, or monitor the page for a certain text string (in the source/of code) and replace it. The later preferred

Thanks

methodpg
12-30-2005, 10:17 PM
If you change:

alert('link.href='+link.href+'\nscriptBefore='+scriptBefore+'\nscriptAfter'+scriptAfter;
to
alert('link.href='+link.href+'\nscriptBefore='+scriptBefore+'\nscriptAfter'+scriptAfter);

you should at least get the pop-up with the info working. ;)

dave_is_sexy
12-31-2005, 09:02 AM
Thanks that's helped lots. I now have a working model, though still unfinished. My code is:

(function() {
var scriptBefore = "ViewMode";
var scriptAfter = "ViewMode2";
var xpath = "//a[contains(@href, scriptBefore)]";
var res = document.evaluate(xpath, document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var i, link;
for (i = 0; link = res.snapshotItem(i); i++) {
link.href = link.href.replace(scriptBefore, scriptAfter);
}
})();

While this is adequet, i'd rather the script only affect certain occurances of the function. These have in common the following preceding code
8%><a href="javascript: So
does such a modification to line 4 essentially filter these out?
var xpath = "//a[contains(@href, '8%><a href="javascript:'+scriptBefore)]";

Thanks, I am getting better ;)