Load a chrome extension and find current website javascript

Viewed 60

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

2 Answers

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.

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/

Related