0

Disclaimer: I'm ABSOLUTELY new with JS and HTML and CSS and I'm learning it as I go.

Issue: I have created a Chrome extension and it kinda works. The issue I'm having is that I'm unable to fetch the browser URL in JS (and later do stuff with it). I read a bunch of SO posts around fetching/getting the browser URL in JS by using window.location.href but in my case, all it does is get the index.html path!

For the sake of simplicity, assume the extension simply prints the URL that's on the page right now. So, imagine you visit YouTube.com/<random_string> on tab 1. When you click the extension, it's supposed to display a popup window and display the same URL there (in an HTML div tag). I've gotten this whole thing working, just that I'm unable to get the URL on the page I'm on, in the pop up. Instead, it's printing the index.html path itself (extension://.. index.html). Hope that makes sense.

What am I doing wrong?

Anonymous Person
  • 1,437
  • 8
  • 26
  • 47
  • 1
    Hi ! Something like that ? https://stackoverflow.com/a/14251218/4698373 – Philippe Sep 28 '21 at 16:51
  • 1
    @Philippe - Yes, this worked! It did not occur to me that getting the URL from the window was browser specific. Thanks! – Anonymous Person Sep 28 '21 at 17:14
  • In the context of a chrome extension, your `background page` (here you named it index.html but it could be anything else) hosts your scripts. So, if you want the script to access a tab, you have to query the `tabs` api to focus one of them, and then access to its properties. – Philippe Sep 28 '21 at 17:20
  • So will I need `tabs` AND `activeTab`? I was using `activeTab` before this, but `tabs` seems to be working. – Anonymous Person Sep 29 '21 at 03:36

0 Answers0