manifest.json, content_scripts limit matching to url with port number

二次信任 提交于 2019-12-13 15:19:22

问题


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

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.

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

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


回答1:


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...
})();


来源:https://stackoverflow.com/questions/18003543/manifest-json-content-scripts-limit-matching-to-url-with-port-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!