当用户导航到特定站点时显示弹出窗口

Show popup when user navigates to a particular site

提问人:Piyush 提问时间:2/15/2023 更新时间:2/15/2023 访问量:35

问:

我正在尝试创建一个 Chrome 扩展程序,当用户导航到特定站点时,它会在侧面自动生成一个小弹出窗口。

我浏览了Chrome开发者页面上的文档并按照教程进行了操作,但我不确定如何在用户访问特定站点时注入脚本。我希望弹出窗口仅显示在该特定站点上。事实上,我在background.json的 addListener 部分中看不到任何控制台日志

manifest.json

{
    "name": "Google helper",
    "version": "1.0",
    "manifest_version": 3,
    "description": "Shows you additional information on google",
    "icons": {
        "128": "icon128.png"
    },
    "action": {
        "default_icon": {
            "128": "icon128.png"
        },
        "default_title": "Google helper",
        "default_popup": "popup.html"
    },
    "background": {
        "service_worker": "background.js"
    },
    "permissions": [
        "tabs",
        "activeTab",
        "scripting"
        // ,
        // "https://google.com/"
    ]
}

背景:.js

// Register the service worker
console.log("Here in background.js before anything starts"); // Able to see this log in service worker view


// Called when the user clicks on the action.
chrome.action.onClicked.addListener(function(tab) {
  // No tabs or host permissions needed!
  console.log('Turning ' + tab.url + ' red!'); // Don't see this in logs
  chrome.scripting.executeScript({
    code: 'document.body.style.backgroundColor="red"'
  });
});
程序 Chrome 谷歌浏览器 应用程序

评论

1赞 wOxxOm 2/15/2023
删除 、 ,只需声明此站点的内容脚本,然后将您的内容作为 DOM 元素添加到页面中。backgroundpermissionsaction
0赞 Piyush 2/21/2023
谢谢@wOxxOm。这奏效了。我想在用户导航到网站时自动生成一个弹出窗口。我应该仍然操作 DOM 还是更确切地说是通过脚本注入弹出窗口?

答: 暂无答案