本文介绍了Chrome扩展程序弹出式安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is kind of a stupid question but i couldnt find any help searching around.

我想知道如何使我的Chrome扩展,当它的存在由用户安装,重定向他在一个新的选项卡与我的网站链接?

I would like to know how can i make my chrome extension, when its being installed by a user, to redirect him in a new tab with a link of my website?

我应该把这个代码放在哪里?
关于background.js,我简直就是这样。

And where should i put this code?On the background.js i quess.

到现在为止,我的background.js就是这个代码

until now my background.js is this code

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);
        }
    });


});

任何想法我应该添加??

Any idea of what should i add??

推荐答案

对于正在安装时,在
$ b

For "when it is being installed", there is a special event in chrome.runtime API:

首次安装扩展程序时,扩展程序更新为新版本时,以及Chrome更新为一个新版本。

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.

正如你猜对的那样,它应该放到你的后台脚本中。

As you guessed correctly, it should go to your background script.

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

To open a new tab with your URL, you can use chrome.tabs

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

这篇关于Chrome扩展程序弹出式安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:15
查看更多