views:

307

answers:

1

Hello,

I have made a firefox extension which loads a web page using xmlhttprequest.

My extension has it's own window opened alongside the main Firefox.

The idea of my extension is to load a webpage in memory, modify it and publish in newly opened tab in firefox.

The webpage has a div with id "Content". And that's the div i want to modify. I have been using XPath alot in greaseMonkey scripts and so i wanted to use it in my extension, however, i have a problem. It seems it doesn't work as i would want. I always get the result of 0.

var pageContents = result.responseText; //webpage which was loaded via xmlhttprequest
var localDiv = document.createElement("div"); //div to keep webpage data
localDiv.innerHTML = pageContents;
// trying to evaluate and get the div i need
var rList = document.evaluate('//div[@id="content"]', localDiv, null XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

The result is always 0 as i said. Now i have created the local div to store website data because i cannot parse the text using XPath. And document in this case is my extensions XUL document/window.

I did expect it to work, but i was wrong.

I know how to extract the div using string.indexOf(str) and then slice(..). However, thats very slow and is not handy, because i need to modify the contents. Change the background, borders of the many forms inside this div. And for this job, i have not seen a better method than evaluating XPath to get all the nodes i need.

So main question is, how to use XPath to parse loaded web page in firefox extension?

Thank you

+1  A: 

Why not load the page in a tab, then modify it in place, like Greasemonkey does?

As for your code, you don't say where it executes (i.e. what is document.location?), but assuming it runs in a XUL window, it makes no sense -- document.createElement will not create an HTML element (but a XUL div element, which has no special meaning), innerHTML shouldn't work for such element, etc.

Nickolay
Thanks. I loaded the page in a new tab and got its document object, then performed all the XPath and modifications with it and it worked good.