kamikaze_pilot
kamikaze_pilot

Reputation: 14834

using firefox extension javascript to get images from current page

so I'm building a custom firefox extension,

my question is, if I want to send info about the images that is on the current page to a remote server using this extension, how would I write the javascript such that it can fetch the images in the current document?

in general, how do you access any HTML content that is in the current page/document from the javascript that is attached to a custom firefox extension?

Upvotes: 1

Views: 1090

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57651

If your extension loads its JavaScript code in a browser overlay then it has access to the global gBrowser variable (the <tabbrowser> tag). To access the document loaded in the currently selected tab you use gBrowser.contentDocument. To get the URLs of all images:

var images = gBrowser.contentDocument.getElementsByTagName("img");
var urls = [];
for (var i = 0; i < images.length; i++)
  urls.push(images[i].src);

Note that this will only give you the <img> tags. You will not get things like <input type="image"> and you will not get images defined in the CSS stylesheets. If you want a more complete list you should have a look at how the Page Info dialog does it: http://hg.mozilla.org/mozilla-central/file/2bb8c0b664cf/browser/base/content/pageinfo/pageInfo.js#l614. This function is being called for all elements from function processFrames() which uses TreeWalker to look at all elements.

Upvotes: 1

Related Questions