Load a chrome extension and find current website javascript

I’m currently trying to make a chrome extension but I’m a bit lost -_-

I have two questions, my first one is how do I go about seeing if a user is on a certain website and execute the code ?

I have this so far:

if(href == "instagram") {
    console.warn("You are on instagram !!")
}

But it doesn’t seem to be working.

my second question is how do I add a chrome extension, I followed a tutorial but I it does not match what i see on my screen

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

First question answer

As for your first question, “href == ” doesn’t work, you need window.location.href and match, like so:

if(window.location.href.match("www.instagram") {
    console.warn("You are on instagram !!")
}

If you’re going to be using this a lot I recommend putting it into a variable like url = window.location.href, so then you can just use url.match

Second question answer

To add your chrome extension, go to chrome://extensions and click “developer mode” in the top right. Then you should see something called “load unpacked” – once you press this locate the folder for your chrome extension and add it.

Method 2

At the moment, you are doing href == "instagram". What this does, is compare them directly. But the href is actually going to be something like instagram.com or https://instagram.com/something, so the statement won’t work.

You probably want to do

if (window.location.href.includes('instagram') {
    console.log('You are on Instagram!');
}

For your second question, it’s a longer answer than SO is really meant for. Try taking a look at this: https://webkul.com/blog/how-to-install-the-unpacked-extension-in-chrome/


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x