I have some code for firefox that runs to check whether an element on the page exists with a certain event handler, in this case onclick. Ever since FF4 came out I've been getting NS_ERROR_NOT_AVAILABLE and I'm guessing it has to do with the XPCNativeWrapper around the elements. Here is the code I use :
var elem = null;
var elems = doc.getElementsByTagName('td');
var firefoxWindow = window;
for (a = 0; a < elems.length; a++) {
if (((elems[a] != null && elems[a].hasAttributes() == true && elems[a].getAttribute('onclick') != null && elems[a].getAttribute('onclick').toString().match(/Goodbye Wonderful/gim) != null && elems[a].getAttribute('onclick').toString().match(/Goodbye Wonderful/gim).length >= 0) || (elems[a] != null && elems[a].onclick != null && elems[a].onclick.toString().match(/Goodbye Wonderful/gim) != null && elems[a].onclick.toString().match(/Goodbye Wonderful/gim).length >= 0))) elem = elems[a];
}
var found = false;
var window = null;
for (var i = 0; i < firefoxWindow.frames.length; i++) {
if (firefoxWindow.frames[i].toString().toLowerCase().indexOf('object window') > -1) {
window = firefoxWindow.frames[i];
break;
}
}
function recursiveSearch(frames) {
for (var i = 0; i < frames.length; i++) {
var elems = frames[i].document.getElementsByTagName('td');
for (a = 0; a < elems.length; a++) {
if (((elems[a] != null && elems[a].hasAttributes() == true && elems[a].getAttribute('onclick') != null && elems[a].getAttribute('onclick').toString().match(/Goodbye Wonderful/gim) != null && elems[a].getAttribute('onclick').toString().match(/Goodbye Wonderful/gim).length >= 0) || (elems[a] != null && elems[a].onclick != null && elems[a].onclick.toStrin开发者_Python百科g().match(/Goodbye Wonderful/gim) != null && elems[a].onclick.toString().match(/Goodbye Wonderful/gim).length >= 0))) elem = elems[a];
}
if (elem) {
found = true;
return;
} else {
if (frames[i].frames.length > 0) {
recursiveSearch(frames[i].frames);
}
}
}
}
if (!elem && window.frames.length > 0) {
recursiveSearch(window.frames);
}
if (elem != null) {
print('##Result##' + elem.tagName);
} else {
print('failed');
I apologize for the long line in the middle but it does well to make sure that there are no null references. I have not been able to find anything on the changes made to events as this works just fine in FF 3.6. I have found that assigning events is different, but no mention of reading event properties.
It turns out that the context that the javascript was running in was incorrect and was running on the firefoxwindow (which is why it could not access the event handlers) instead of running on the document of the window.
精彩评论