如何设置“匹配”的多个选项,然后加载某个JS文件?
我有这段代码:
{
"manifest_version": 2,
"name": "My First Extension",
"description": "This is my first content-script extension",
"version": "1.0",
"content_scripts": [{
"run_at": "document_end",
"matches": "asdasd"
"js": ["jquery.min.js", "content-script-1.js"]
}],
"browser_action": {
},
"permissions": [
"activeTab"
]
}
所以如果“匹配”是“asd”,我想做
"js": ["jquery.min.js", "content-script-1.js"]
如果“匹配”是“dsa”,我想将“js”设置为
["jquery.min.js", "content-script-100.js"]
如何做到这一点?
最佳答案
您可以在内容脚本中添加另一个 block :
"content_scripts": [
{
"run_at": "document_end",
"matches": "asd"
"js": ["jquery.min.js", "content-script-1.js"]
},
{
"run_at": "document_end",
"matches": "dsa"
"js": ["jquery.min.js", "content-script-100.js"]
}
]
另一种选择是使用 executeScript - 然后您可以选择何时将您的 js 文件注入(inject)页面。
关于javascript - 在 Chrome 扩展中为 "matches"设置多个选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42946795/