gpt4 book ai didi

javascript - Chrome扩展程序弹出安装

转载 作者:行者123 更新时间:2023-11-28 19:18:44 25 4
gpt4 key购买 nike

这是一个愚蠢的问题,但我找不到任何帮助搜索。

我想知道如何让我的 chrome 扩展程序在由用户安装时将其重定向到带有我网站链接的新选项卡中?

我应该把这段代码放在哪里?关于background.js,我想知道。

到目前为止我的background.js就是这段代码

chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {
allFrames: true,
file: "content_script.js"
}, function() {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
}
});


});

知道我应该添加什么吗?

最佳答案

对于“正在安装时”,chrome.runtime API:中有一个特殊事件

onInstalled

Fired when the extension is first installed, when the extension is updated to a new version, and when Chrome is updated to a new version.

正如您猜对的那样,它应该转到您的后台脚本。

chrome.runtime.onInstalled.addListener( function(details) {
switch(details.reason) {
case "install":
// First installation
break;
case "update":
// First run after an update
break;
}
});

要使用您的 URL 打开新选项卡,您可以使用 chrome.tabs

chrome.tabs.create({url: "http://example.com/"});

关于javascript - Chrome扩展程序弹出安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29331302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com