manifest.json,content_scripts限制与具有端口号的url匹配 [英] manifest.json, content_scripts limit matching to url with port number

查看:1217
本文介绍了manifest.json,content_scripts限制与具有端口号的url匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Google Chrome浏览器扩展程序,该扩展程序只能在URL匹配 http:// *:8000 / *

I'm writing a Google chrome extension that should only run when the URL matches http://*:8000/*

我的manifest.json文件的一部分:

Part of my manifest.json file:

"content_scripts": [{
    "matches": ["http://*:8000/*"],
    "js" : ["client.js"],
    "run_at": "document_end"
}] 

尝试打包扩展程序时出现以下错误。

I get the following error when trying to pack the extension.

Invalid value for 'content_scripts[0].matches[0]': Invalid host wildcard.

如果我将匹配更改为< all_url> 它包装正常并且可以在每个页面上运行。

If I change matches to <all_url> it packs properly and works but on every page.

如何才能使它只与包含端口8000的URL一起工作?

How can I get it to work only with a URL that contains port 8000?

推荐答案

您将不得不以编程方式检查端口号,并在脚本不是您想要的端口时停止评估脚本。您可以检查 window.location.port (fyi,当 http 时,它是80端口的空字符串)或 window.location.host 其中包含主机名和端口(但不包括 window.location.hostname - 它不包含包括端口)。所以,我会用这样的东西开始我的脚本:

You're going to have to programmatically check the port number and stop the evaluation of the script if it's not the port you want. You can check window.location.port (fyi, which is an empty string for port 80 when on http) or window.location.host which includes the hostname and port (but not window.location.hostname- it doesn't include the port). So, I'd start my script with something like this:

(window.location.port === "8000") && (function() {
    // your code...
})();

这篇关于manifest.json,content_scripts限制与具有端口号的url匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆