Page 1 of 1

[Help] - Converting Greasmonkey script

Posted: Fri Apr 15, 2011 8:47 am
by fatoms
Hi All,

I am attempting to convert a script (full listing at end of post )for use with Avant.

The start I have gives me (alerts to help me debug):

(function (){
var subreggix = /self./i;
alert (subreggix)
var matches=0;
alert (matches)
var xpath = "//p[@class='title']/span[@class='domain']";
alert (xpath)
})();

When I add either of the next line so that I have
(function (){
var subreggix = /self./i;
alert (subreggix)
var matches=0;
alert (matches)
var xpath = "//p[@class='title']/span[@class='domain']";
alert (xpath)
var rows = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
alert (rows)
})();

I do not get the alert alert (rows)

Can anyone offer any guidance on why this might be?

Dominic

ORIGINAL SCRIPT:
// ==UserScript==
// @name Self-Post Filter
// @namespace http://reddit.com
// @description Automatically hides self-post stories
// @include http://reddit.com/*
// @include http://*.reddit.com/*
// @exclude http://reddit.com/user/*
// @exclude http://*.reddit.com/user/*
// @exclude http://reddit.com/info/*
// @exclude http://*.reddit.com/info/*
// @exclude http://reddit.com/r/*/related/*
// @exclude http://*.reddit.com/r/*/related/*
// ==/UserScript==
// Based off of Reddit Content Filter


// Edit this line to include whichever subreddits you want to filter out

var subreggix = /self./i;
var matches=0;
var xpath = "//p[@class='title']/span[@class='domain']";
var rows = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for (var i = 0; i < rows.snapshotLength; i++) {
var row = rows.snapshotItem(i);
var text = row.childNodes[1].innerHTML;
if (subreggix.exec(text)) {
row.parentNode.parentNode.parentNode.style.display = 'none';
matches+=1;
}
}

if (matches > 0) {
var tbl = document.getElementById('siteTable'),
div = document.createElement('div'),
msg = '' + matches + ' post(s) were removed from this page';
GM_log(msg);
if (!tbl) return;
div.innerHTML = '<p><i>' + msg + ' (Reddit Content Filter).</i></p>';
tbl.parentNode.insertBefore(div, tbl.nextSibling);
}

END OF ORIGINAL SCRIPT

Re: [Help] - Converting Greasmonkey script

Posted: Mon Apr 18, 2011 4:41 pm
by sulpherdragon
There is a javascript standard, and no one is following it. Thus you get things like this, were code will work in one browser and not in the other.
The reason is because the IE document object model and the Gecko (Firefox) document object model are a little bit different.
Your second rows alert isn't happening because IE is breaking when it sees: document.evaluate.
That function does not exist in IE and you will need a workaround to fix it.
You should search for equivalents, but you might come up with different answers depending on what actual end result your code is attempting to get.

If I get time I'll take a better look at the specific code, but you should look for alternatives to "document.evaluate" in IE.

Re: [Help] - Converting Greasmonkey script

Posted: Thu Apr 21, 2011 9:32 am
by fatoms
sulpherdragon,

Thanks for the information. Will lookinto what alternatives are available in IE.

Dominic.