-2

Why does this code doesn't work ? (it is part of a chrome extension),

I don't understand why using this method to check if the hostname of the current page is in the array ac_websites does not work.

I am not sure what window.location.hostname returns (string or something else)

Maybe indexOf() only works with strings.

const ac_websites = ["sciencedirect.com", "ncbi.nlm.nih.gov", "pubmed.ncbi.nlm.nih.gov", "ieeexplore.ieee.org", "ci.nii.ac.jp"]


chrome.runtime.onMessage.addListener(function (request) {
    const url = document.location.href
    const hostname = window.location.hostname
    
    if (ac_websites.indexOf(hostname) != 1) {
        window.open("https://google.com/" + url);
    } else {
        alert("Not an academic website")
    }
})
Polymood
  • 397
  • 3
  • 14

1 Answers1

3

You can also use Array.includes().

if (ac_websites.includes(hostname)) {
        window.open("https://google.com/" + url);
    } else {
        alert("Not an academic website")
    }
  • 1
    No need to answer a typo question, even if this is good advice. Use a comment instead, please. –  Apr 28 '21 at 17:12