代码之家  ›  专栏  ›  技术社区  ›  connexo

未注入Chrome扩展简单脚本

  •  0
  • connexo  · 技术社区  · 5 年前

    我正在尝试编写一个最小的Chrome扩展的第一步,我不明白为什么它不执行我的clientScript.js。

    {
        "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/*

    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)
    

    两个文件都在同一文件夹中:

    folder structure

    另外,我已经正确地“安装”了扩展:

    enter image description here

    控制台不显示与扩展相关的错误。

    1 回复  |  直到 5 年前
        1
  •  1
  •   woxxom    5 年前

    假设您确实重新加载了选项卡(内容脚本仅在加载初始选项卡时注入,请参见 this 为了得到更多的信息和解决办法),你可能是 infamous Google's decision to hide www https://www.twitch.tv/ 因此manifest.json的URL模式不正确,与实际站点不匹配。

    简单地使用一个通用模式 "https://*.twitch.tv/*" 两者都能匹配 https://www.twitch.tv/ https://twitch.tv/

    P、 S.作为调试步骤,您可以通过查看devtools控制台工具栏或devtools->Sources->content scripts面板中的上下文选择器来检查是否已插入内容脚本。如果要恢复传统的地址栏行为,请参见 https://superuser.com/a/1498561