Greasemonkey user script: Infinite loop?

N

Newton

I'm having some issues with a greasemonkey script i've made (nothing
serious, and you'll notice that by the mess). I made it to learn
javascript properly at first but it ended handy and now i'm addicted to
it.. However, it doesn't quite work like i'd expect it to (and like it
used to with older versions) I realise that it may be a
greasemonkey-specific question, please bare with me if it is. I figured
some of you guys may have used it before considering it uses JS.

Problem: The first time i load the page it works perfectly (albeit
somewhat slowly; i bet there's fasters ways to do this.). However as
soon as i try another page within the same firefox session the script
seems to be called an indefinite amount of time. I have no idea why, i
tried some other way (that i know of) mainly associating the function
with the load event and not putting the code within the anonymous
function. They all came back with the same problem.

Unfortunately it can't be tested unless you're a newzbin.com prenium
member. I'm hoping it's something obvious about the javascript. If you
guys need to test, let me know and i'll mirror a test page.

Any help would be apreciated at this point, thanks in advance!

Here's the victim:

<pre>
// ==UserScript==
// @name Newton's Newzbin Mods
// @description Make various modifications to newzbin website.
// @include http://*newzbin.com*
// ==/UserScript==

(function() {
window.getElementsByClassName = function(clsName) {
var i, matches=new Array();
var els=document.getElementsByTagName('*');

for(i=0; i<els.length; i++) {
if(els.item(i).className==clsName) {
matches.push(els.item(i));
}
}
return matches;
}

window.Left = function(str, n) {
if (n <= 0)
return "";
else if (n > String(str).length)
return str;
else
return String(str).substring(0,n);
}

window.hideFunctionBar = function() {
var els=getElementsByClassName("invisible");
var htmlString;

for(i=0; i<els.length; i++) {
htmlString = Left(els.innerHTML, 50);
GM_log(htmlString.indexOf("["));
if (htmlString.indexOf("[") == 22)
els.style.display="none";
}
}

/*window.hideshows = function() {
if (hideShow == "hide") {
hideShow = "show";
document.getElementById('hideshow').innerHTML = 'S<br />H<br />O<br />W';
forminput = getElementsByClassName("forminput")[0];
forminput.style.display = "none";
} else {
hideShow = "hide";
document.getElementById('hideshow').innerHTML = 'H<br />I<br />D<br />E';
forminput = getElementsByClassName("forminput")[0];
forminput.style.display = "block";
}
}*/


var forminput = getElementsByClassName("forminput")[0];
if (!forminput) {return;}
var header = document.getElementById("newzBin");

getElementsByClassName("logo")[0].style.display = "none";

forminput.style.position = "fixed";
forminput.style.zindex = 700;
forminput.style.top = 0;
forminput.style.left = 0;
forminput.style.width = header.offsetWidth - 140 + "px";
forminput.style.marginLeft = "140px";

var tr = '<tr><td colspan="3">' +
'[<a href="/media/help/editor_buttons.html" onclick="openwin(href);
return false">?</a>]' +
'<span style="padding: 1px; border: 1px solid gray">' +
'<select name="tl" >' +
'<option value="1" >1</option>' +
'<option value="2" >2</option>' +
'<option value="3" >3</option>' +
'<option value="4" >4</option>' +
'<option value="5" >5</option>' +
'</select>' +
'<input type="submit" name="tag" value="Tag" />' +
'<input type="submit" name="tag_view" value="Tag &amp; View" />' +
'</span>' +
'&nbsp;' +
'<span style="padding: 1px; border: 1px solid gray">' +
'<input type="submit" name="hide" value="Hide" /> for ' +
'<input type="text" name="hideduration" value="60" size="2" /> mins' +
'</span>' +
'&nbsp;' +
'<span style="padding: 1px; border: 1px solid gray">' +
'<input type="submit" name="junk" value="Junk" />' +
'<input type="submit" name="unjunk" value="unJunk" />' +
'</span>' +
'&nbsp;' +
'<span style="padding: 1px; border: 1px solid gray">' +
'<input type="submit" name="msgid" value="Message-IDs" />' +
'</span>' +
'</td>' +
'<td class="Right" colspan="5">' +
'[<a href="/media/help/select_links.html" onclick="openwin(href);
return false">?</a>]&nbsp;&nbsp;' +
'<a href="javascript:checkAll(\'FileActions\')">all</a>&nbsp;&nbsp;' +
'<a href="javascript:invert(\'FileActions\')">inv</a>&nbsp;&nbsp;' +
'<a href="javascript:uncheckAll(\'FileActions\')">none</a>&nbsp;&nbsp;' +
'<a href="javascript:checkRanges(\'FileActions\')">range</a>' +
'</td></tr>';
forminput.innerHTML += tr;

var OSHeight = header.offsetHeight - forminput.offsetHeight;

header.innerHTML = '<div style="margin-top:' + forminput.offsetHeight +
'px;>' + header.innerHTML + '</div>';



hideFunctionBar();
})();</pre>
 
R

RobG

Newton said:
I'm having some issues with a greasemonkey script i've made (nothing
serious, and you'll notice that by the mess). I made it to learn
javascript properly at first but it ended handy and now i'm addicted to
it.. However, it doesn't quite work like i'd expect it to (and like it
used to with older versions) I realise that it may be a
greasemonkey-specific question, please bare with me if it is. I figured
some of you guys may have used it before considering it uses JS.

Problem: The first time i load the page it works perfectly (albeit
somewhat slowly; i bet there's fasters ways to do this.). However as
soon as i try another page within the same firefox session the script
seems to be called an indefinite amount of time. I have no idea why, i
tried some other way (that i know of) mainly associating the function
with the load event and not putting the code within the anonymous
function. They all came back with the same problem.

Unfortunately it can't be tested unless you're a newzbin.com prenium
member. I'm hoping it's something obvious about the javascript. If you
guys need to test, let me know and i'll mirror a test page.

Any help would be apreciated at this point, thanks in advance!

Here's the victim: [...]


var tr = '<tr><td colspan="3">' +
'[<a href="/media/help/editor_buttons.html"
onclick="openwin(href); return false">?</a>]' +
'<span style="padding: 1px; border: 1px solid gray">' +

[... large slab of concatenated HTML removed ...]
href="javascript:invert(\'FileActions\')">inv</a>&nbsp;&nbsp;' +
'<a
href="javascript:uncheckAll(\'FileActions\')">none</a>&nbsp;&nbsp;' +
'<a href="javascript:checkRanges(\'FileActions\')">range</a>' +
'</td></tr>';
forminput.innerHTML += tr;

Without testing anything, and ignoring many minor transgressions, I'd
say your major problem is trying to modify a table using innerHTML.

Create an element factory for your A, TD and TR elements and add them
using DOM. There have been many recent posts attempting to modify
tables using innerHTML, do a search.

While you're at it, how about getting rid of the A elements with
javascript pseudo-protocol href attributes? Try an appropriately styled
div or span with an onclick instead.

[...]
 
L

Lasse Reichstein Nielsen

Small comment:
window.getElementsByClassName = function(clsName) {
var i, matches=new Array();
var els=document.getElementsByTagName('*');

for(i=0; i<els.length; i++) {
if(els.item(i).className==clsName) {

Remember that the class attribute is a space separated sequence of
class names, not necessarily a single one. For that reason, I would
do:

var clsNameRE = new RegExp("\\b"+clsName+"\\b","");
for(i=0; i<els.length; i++) {
if(clsNameRE.test(els.item(i).className)) {

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top