【问题标题】:Load a chrome extension and find current website javascript加载 chrome 扩展并查找当前网站 javascript
【发布时间】:2021-12-18 01:20:21
【问题描述】:

我目前正在尝试制作一个 chrome 扩展,但我有点迷茫 -_-

我有两个问题,我的第一个问题是如何查看用户是否在某个网站上并执行代码?

到目前为止我有这个:

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

但它似乎不起作用。

我的第二个问题是如何添加 chrome 扩展,我按照教程进行操作,但它与我在屏幕上看到的不匹配

【问题讨论】:

    标签: javascript html google-chrome


    【解决方案1】:

    此刻,你正在做href == "instagram"。这样做的目的是直接比较它们。但href 实际上会变成instagram.comhttps://instagram.com/something,所以该语句不起作用。

    你可能想做

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

    对于您的第二个问题,它的答案比 SO 的真正含义要长。试试看这个:https://webkul.com/blog/how-to-install-the-unpacked-extension-in-chrome/

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    第一个问题答案

    至于你的第一个问题,“href ==”不起作用,你需要window.location.hrefmatch,像这样:

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

    如果您要经常使用它,我建议将其放入 url = window.location.href 之类的变量中,这样您就可以使用 url.match

    第二个问题答案

    要添加您的 chrome 扩展程序,请转到 chrome://extensions 并点击右上角的“开发者模式”。然后你应该会看到一个叫做“load unpacked”的东西——一旦你按下这个找到你的 chrome 扩展的文件夹并添加它。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-30
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    相关资源
    最近更新 更多