How can I access webpage data in a browser extension?

In summary, you can access the URL of the tab the user is watching in Chrome extensions by using browser.tabs.query().
  • #1
olgerm
Gold Member
531
34
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
  • #3
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 .
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.:
 
  • #7
jack action said:
Have you tried 'lastFocusedWindow' instead of 'currentWindow'?
Yes. It still made variable URL equal to "undefined".
 
Last edited:
  • #9
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

Q: What is a browser extension?

A: A browser extension is a software program that adds functionality to a web browser. It can modify or enhance the user's browsing experience by adding new features or tools.

Q: Why would someone want to make a browser extension?

A: People may want to make a browser extension for various reasons, such as to improve productivity, customize their browsing experience, or create a solution to a specific problem or need.

Q: What programming languages are commonly used to make browser extensions?

A: The most commonly used programming languages for making browser extensions are HTML, CSS, and JavaScript. Other languages such as Python, Java, and C++ can also be used depending on the browser and its supported technologies.

Q: How do you distribute a browser extension?

A: Browser extensions can be distributed through official extension marketplaces such as Chrome Web Store, Firefox Add-ons, or Microsoft Edge Add-ons. They can also be distributed through the developer's website or through third-party extension stores.

Q: Are there any limitations or restrictions when making a browser extension?

A: Yes, there are limitations and restrictions when making a browser extension. These can include browser compatibility, security measures, and adhering to the platform's guidelines and policies. It is important to thoroughly research and understand these limitations before creating an extension.

Similar threads

  • Computing and Technology
Replies
3
Views
1K
Replies
1
Views
784
Replies
1
Views
840
  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Computing and Technology
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
986
Replies
5
Views
946
Back
Top