【问题标题】:Chrome extension popup installationChrome扩展弹出安装
【发布时间】:2015-03-30 01:37:15
【问题描述】:

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

我想知道如何让我的 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);
        }
    });


});

知道我应该添加什么吗??

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension


    【解决方案1】:

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

    onInstalled

    在首次安装扩展程序、扩展程序更新到新版本以及 Chrome 更新到新版本时触发。

    你猜对了,它应该进入你的后台脚本。

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

    要使用您的网址打开新标签页,您可以使用chrome.tabs

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

    【讨论】:

    • 所以如果我将chrome.tabs.creat 与我想要的链接放在//first installation 中,就可以了,对吧?
    • 完美男人!!我在开发者模式下试了一下,效果很好。 :)
    猜你喜欢
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    相关资源
    最近更新 更多