PDA

View Full Version : Javascript problem - again


Muzer
4 Aug 2008, 10:34
As I said in my QuickQuote+ thread, I'm looking at porting it to FireFox. However, when I get it to run the script., I get:

txt is undefined

In the error console. Someone on IRC recommended I use a debugger, but I have no idea how to do so. All I managed to get it to do is watch the variable, which stays at null.

The code is pasted below:

"javascript:{var sel = document.createElement('div');
var s = window.getSelection();
if(s.toString() == '')
{
frm = document.getElementsByTagName('iframe')[document.getElementsByTagName('iframe').length - 1];
s = frm.contentWindow.getSelection();
}
if(s != '')sel.appendChild(s.getRangeAt(0).cloneContents() );
var ele = sel.getElementsByTagName('*');
for(var i=0, e; e=ele[i]; i++)
{
switch(e.tagName.toUpperCase())
{
case 'A' : e.innerHTML='[ url='+e.href+']'+e.innerHTML+'[/url ]';
break;
case 'FONT' : if(e.color)
{
e.innerHTML=''+e.innerHTML+''
};
if(/(?:^|,)\s*monospace\s*(?:,|$)/i.test(e.getAttribute('face')))
{
e.innerHTML='[ code]'+e.innerHTML+'[/code ]'
};
break;
case 'DIV' : if(e.className=='code')
{
e.innerHTML='[ code]'+e.innerHTML+'[/code ]'
}
else
{
if(e.className=='quote')
{
e.innerHTML='[ quote]'+e.innerHTML+'[/quote ]'
}
else
{
e.innerHTML='<br>'+e.innerHTML
}
};
break;
case 'B' : e.innerHTML='[ b]'+e.innerHTML+'[/b ]';
break;
case 'STRONG' : e.innerHTML='[ b]'+e.innerHTML+'[/b ]';
break;
case 'I' : e.innerHTML='[ i]'+e.innerHTML+'[/i ]';
break;
case 'U' : e.innerHTML='[ u]'+e.innerHTML+'[/u ]';
break;
case 'PRE' : e.innerHTML='[ code]'+e.innerHTML+'[/code ]';
break;
case 'BLOCKQUOTE' : e.innerHTML='[ quote]'+e.innerHTML+'[/quote ]';
break;
case 'IMG' : e.outerHTML+='[ img]'+e.src+'[/img ]';
break;
}
};
var txt = sel.innerText;
var n = txt.split('\n').length;
if(txt.toString() === '')
{
frm = document.getElementsByTagName('iframe')[document.getElementsByTagName('iframe').length - 1];
txt = frm.contentDocument.getSelection();
}
var ta = document.getElementsByTagName('textarea');
var tl=ta.length-1;
if(typeof window.qr_activate == 'function')
{
if(ta[tl].disabled)
{
qr_activate(1);
}
}
if(document.getElementById('qr_open'))
{
if(typeof window.my_show_div == 'function')
{
my_show_div(qr_open);
}
else
{
itm = document.getElementById('qr_open');
itm.style.display = '';
}
}
var t;
do
{
t=ta[tl];
tl--
}
while(t && (t.rows<5 || t.offsetHeight==0));
if(!t)
{
prompt('Please copy BBCode', txt);
exit(0);
}
if(t.rows < 15)
{
t.rows + n <= 15 ? t.rows += n : t.rows = 15
};
var nam='';
var url='';
var l=location.hostname;
var c=s.focusNode;
if(c && l=='forum.ru-board.com')
{
while(c.className!='tpc'&&c.parentNode)
{
c=c.parentNode
};
if(c.className!='tpc')exit(0);
var tn=c.parentNode.parentNode.childNodes[1].childNodes[0].childNodes[1];
nam='[ b]'+c.parentNode.firstChild.innerText.replace(/\n.*/g,'')+'[/b ] ';
url='[ url='+tn.childNodes[5].href+'][u ]'+tn.childNodes[7].innerText.replace(/^ +/,'').replace(/ +$/,'')+'[/u ][/url ] '
};
if(c && l=='operafan.net'||l=='opera-ac.com'||l=='bit-center.ru')
{
while(c.className!='windowbg'&&c.className!='windowbg2'&&c.parentNode)
{
c=c.parentNode
};
var dv=c.getElementsByTagName('div');
for(var i=0,di; di=dv[i]; i++)
{
if(di.className=='post')
{
c=di;
break
}
};
if(c.className!='post')exit(0);
var pn=c.parentNode.childNodes[1].firstChild.firstChild;
nam='[b ]'+c.parentNode.parentNode.firstChild.childNodes[1].innerText+'[/b ] ';
url='[ url='+pn.firstChild.firstChild.href+'][u ]'+/(\d\d\D\d.*\d).*$/.exec(pn.childNodes[1].innerText)[1]+'[/u ][/url ] '
};
if(t.value.indexOf(nam)!=-1){nam=''};
if(nam==''){var qtg='[ quote'}else{var qtg='[quote='};
if(t.value.indexOf(url)!=-1)url='';
t.focus();
document.selection.createRange().text += qtg+nam+']'+txt+'[/quote ]\r\n';
t.setSelectionRange(t.selectionEnd+n-1, t.selectionEnd+n-1);}"


EDIT: Please note I have added some spaces in order to stop the forum from parsing them

quakerworm
4 Aug 2008, 10:43
didn't m3ntal mention something about the code he gave you for selecting a frame being opera-specific?

edit:
You need to call getSelection() on the document object within the iframe rather than the iframe itself. I'm assuming this only needs to work in Opera 9, so i'll skip the cross-browser compatibility:


x = document.getElementsByTagName('iframe')[document.getElementsByTagName('iframe').length - 1];
insert = x.contentDocument.getSelection();

Muzer
4 Aug 2008, 10:47
that may be so, but it doesn't seem to relate to the code with "txt" in it. I'm thinking some other code, such as sel.innerText; for example, is Opera-only.

Muzer
4 Aug 2008, 10:58
I think .innerText might be opera only, because where sel is defined as in the code above in both FF and Opera, sel.innerText in opera is the selected text, and sel.innerText in FF is undefined.

EDIT: That is true, I'll change it to textContent which is the FF equivalent and see what happens

EDIT2: Now it gets further but still stops, I'll look at the error

EDIT3: document.selection is undefined

Muzer
4 Aug 2008, 11:41
a only, because where sel is defined as in the code
NGER FOR
ugh! http://myweb.tiscali.co.uk/digitaltv/oldsig.txt
code :: txt is undefined

In the error console. Someone on


In other words, it works!

M3ntal
4 Aug 2008, 15:34
Yeah, use textContent for FF. Also, for the second error, it's document.selection.createRange() on IE and document.getSelection() on FF:

if (document.getSelection) {
var str = document.getSelection();
} else if (document.selection && document.selection.createRange) {
var range = document.selection.createRange();
var str = range.text;
}

Muzer
4 Aug 2008, 16:52
document.getSelection() didn't work, I used a different thing.