How can I access webpage data in a browser extension?

  • Thread starter Thread starter olgerm
  • Start date Start date
  • Tags Tags
    Extension Firefox
Click For Summary

Discussion Overview

The discussion revolves around creating a browser extension, specifically focusing on accessing webpage data and the challenges encountered in retrieving the current tab's URL. Participants explore various programming languages for extension development and address issues related to permissions and the same-origin policy.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant questions whether browser extensions can be developed in languages other than JavaScript.
  • Another participant suggests that the issue with obtaining the URL may stem from testing in a development tool that does not reflect normal user behavior.
  • A participant mentions that using window.location.href returns the sidebar's URL instead of the active tab's URL.
  • There is a suggestion to use lastFocusedWindow instead of currentWindow in the browser.tabs.query method, but this does not resolve the issue for some participants.
  • One participant discusses the limitations imposed by the same-origin policy when trying to access data from an IFRAME.
  • Another participant raises a question about permission errors encountered when accessing window.content.location.href, speculating on the implications of the same-origin policy in the context of browser extensions.
  • A participant mentions using XMLHttpRequests as a solution to access data from a webpage.
  • Concerns are raised about new security features in multi-process Firefox affecting extension functionality.
  • There is a suggestion to check the manifest.json file for necessary permissions to access certain properties.

Areas of Agreement / Disagreement

Participants express differing views on the reasons behind the issues faced, particularly regarding the same-origin policy and the behavior of the browser's development tools. No consensus is reached on the best approach to resolve the URL retrieval problem.

Contextual Notes

Participants note that the same-origin policy may not apply to browser extensions, yet errors still occur, indicating potential changes in browser security features that could affect functionality.

olgerm
Gold Member
Messages
543
Reaction score
40
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   Reactions: jim mcnamara

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
1
Views
1K
  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K