Chrome扩展程序简单脚本未注入 [英] Chrome extension simple script not being injected
问题描述
我正在尝试编写最小的Chrome扩展程序的第一步,但我不知道为什么它不执行我的clientScript.js.
I'm trying my first steps in writing a minimal Chrome extension, and I cannot figure out why it does not execute my clientScript.js.
这是我的manifest.json:
This is my manifest.json:
{
"name": "Sit back, relax and enjoy",
"version": "0.1",
"description": "Finds and clicks the +extra channel points button when it is available",
"permissions": [ "activeTab" ],
"content_scripts": [
{
"matches": [ "https://twitch.tv/*" ],
"js": [ "contentScript.js" ],
"run_at": "document_idle"
}
],
"manifest_version": 2
}
这是我要在与 https://twitch.tv/*
匹配的页面上执行的脚本:
And this is the the script that I want executed on pages that match https://twitch.tv/*
:
let intervalTimer
function sibareaen() {
const btn = document.querySelector('.tw-button.tw-button--success.tw-interactive')
if (btn) {
btn.click()
console.log('At your service - clicked the button for you!')
}
}
function toggleSibareaen(on) {
switch (on) {
case true:
intervalTimer = setInterval(sibareaen, 750)
break
case false:
clearInterval(intervalTimer)
break
default:
clearInterval(intervalTimer)
}
}
console.log('At your service - ready to click for you!')
toggleSibareaen(true)
两个文件都在同一个文件夹中
I have both files in the same folder:
另外,我已经正确地安装"了扩展名:
Also, I have properly "installed" the extension:
控制台没有显示与扩展名相关的错误.
The console shows no errors related to the extension.
我想念什么?
推荐答案
假定您确实重新加载了选项卡(内容脚本仅在初始选项卡加载时注入,请参见臭名昭著的Google决定的受害者将 www
隐藏在地址栏中:如果进入编辑模式并选择/复制整个文本(或简单地双击地址),您将看到该站点的URL实际上是 https://www.twitch.tv/
,因此您manifest.json的网址格式不正确,与实际网站不匹配.
Assuming you did reload the tab (the content scripts are injected only on initial tab load, see this for more info and a workaround), you're probably a victim of the infamous Google's decision to hide www
in the address bar: if you enter the edit mode and select/copy the entire text (or simply double-click the address) you will see the site's URL is actually https://www.twitch.tv/
so your manifest.json has an incorrect URL pattern that doesn't match the real site.
只需使用类似于"https://*.twitch.tv/*"
的通用模式,即可同时匹配两个 https://www.twitch.tv/
和 https://twitch.tv/
以及任何其他子域.
Simply use a generalized pattern like "https://*.twitch.tv/*"
that will match both https://www.twitch.tv/
and https://twitch.tv/
and any other subdomain(s).
P.S.作为调试步骤,您可以通过查看devtools控制台工具栏或devtools-> Sources-> Content scripts面板中的上下文选择器来检查是否已注入内容脚本.而且,如果要还原传统的地址栏行为,请参见 https://superuser.com/a/1498561
P.S. as a debugging step, you can check if the content script is even injected by looking at the context selector in devtools console toolbar or in devtools -> Sources -> Content scripts panel. And if you want to restore the classic address bar behavior, see https://superuser.com/a/1498561
这篇关于Chrome扩展程序简单脚本未注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!