manifest.json,content_scripts使用端口号限制与url的匹配

时间:2013-08-01 20:07:11

标签: google-chrome-extension

我正在编写一个Google Chrome扩展程序,该扩展程序只应在网址与http://*:8000/*

匹配时运行

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

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

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

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

如果我将matches更改为<all_url>,它会正确打包并在每个页面上都有效。

如何才能让它只能使用包含端口8000的URL?

1 个答案:

答案 0 :(得分:3)

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

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