How can I access webpage data in a browser extension?

  • Thread starter Thread starter olgerm
  • Start date Start date
  • Tags Tags
    Extension Firefox
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
13 replies · 3K views
olgerm
Gold Member
Messages
551
Reaction score
43
I am trying to make a browser extension. Tutorial (https://developer.mozilla.org/en-US/Add-ons) from mozilla webpage teaches to make these in javascript, but is it possible to make browser extension in some other programming language?

How can I get URL of the tab user is watching. I tried browser.tabs.query({active: true, currentWindow: true},function(tabs){var URL = tabs[0].url;}) but it always makes variable URL equal to "undefined". Maybe because this script tries to get URL of extension's sidebar.

How can get data from webpage with given URL to further process it in same browser extension?
 
Physics news on Phys.org
FactChecker said:
Is it possible that you are testing it in a development tool that gives you an active window where your call will not work?
I am not using development tools. I load it as temporary add-on in Firefox to test it. It also makes variable URL equal to "undefined" if active tab is not about:debugging .
 
I tried URL=window.location.href; ,but it makes variable URL equal to "moz-extension://f0458eeb-2d31-4334-756d-d126f42534d6/sidebar.html" . It seems to be trying to get sidebars URL.
 
It sounds like the sidebar part of the window is active and you need to force the active window to be the main one with the web page of interest. Maybe you can make the web page window active if you know the browser tab name. This is beyond my knowledge base and I will have to leave this for others who know more about it.
 
olgerm said:
How can I get URL of the tab user is watching. I tried browser.tabs.query({active: true, currentWindow: true},function(tabs){var URL = tabs[0].url;}) but it always makes variable URL equal to "undefined". Maybe because this script tries to get URL of extension's sidebar.
Have you tried 'lastFocusedWindow' instead of 'currentWindow'?
JavaScript:
browser.tabs.query(
    {
        active: true,
        lastFocusedWindow: true
    },
    function(tabs){
        var URL = tabs[0].url;
    }
)

ref.:
 
jack action said:
Have you tried 'lastFocusedWindow' instead of 'currentWindow'?
Yes. It still made variable URL equal to "undefined".
 
Last edited:
I could open, the page I want data from, in IFRAME tag, but same-origin-policy does not let the extensions javascript to get data from the IFRAME.
 
olgerm said:
I tried
JavaScript:
lehe_URL=window.content.location.href;
, but got error:
Code:
Permission denied to access property "location"
Is it because same-origin-policy? I read from https://stackoverflow.com/questions/11849945/how-does-same-origin-policy-apply-to-browser-extensions that same-origin-policy doesn't apply to browser extensions. Then why did I get that error?

I got answer to my second question. I used XMLHttpRequests.
I'm not a developer, but you may be running into some new security features of the newer multi-process Firefox:

https://developer.mozilla.org/en-US/Firefox/Multiprocess_Firefox
https://developer.mozilla.org/en-US/Firefox/Multiprocess_Firefox/Message_manager
 
I think stoomart is correct - mozilla has a "love to hate" relationship with regard to browser addons or extensions, and with java as well. Chrome also has similar protections.
 
What should I do to access window.content.location.href? Add some permission into manifest.json?
 
olgerm said:
What should I do to access window.content.location.href? Add some permission into manifest.json?
You should start here to determine if your code is affected by the changes:

https://developer.mozilla.org/en-US/Add-ons/Working_with_multiprocess_Firefox
 
  • Like
Likes   Reactions: jim mcnamara