How can I access webpage data in a browser extension?

  • Thread starter Thread starter olgerm
  • Start date Start date
  • Tags Tags
    Extension Firefox
AI Thread Summary
Creating a browser extension primarily involves JavaScript, but users are exploring alternatives. Accessing the URL of the active tab can yield "undefined" if the script is run in the extension's sidebar instead of the main window. Suggestions include using 'lastFocusedWindow' in the query to retrieve the URL, but issues persist with permissions and same-origin policies. XMLHttpRequests are recommended for fetching webpage data, though users are encountering security restrictions in newer Firefox versions. Adjusting permissions in the manifest.json file may be necessary to resolve access issues.
olgerm
Gold Member
Messages
532
Reaction score
35
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?
 
Technology 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.
 
  • #11
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
 
  • #12
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.
 
  • #13
What should I do to access window.content.location.href? Add some permission into manifest.json?
 
  • #14
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 jim mcnamara

Similar threads

Replies
3
Views
2K
Replies
3
Views
2K
Replies
4
Views
3K
Replies
21
Views
6K
Replies
8
Views
2K
Replies
9
Views
2K
Replies
10
Views
1K
Back
Top