Chrome extension popup installation

天涯浪子 提交于 2019-12-29 09:34:35

问题


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

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?

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

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??


回答1:


For "when it is being installed", there is a special event in 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.

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

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

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


来源:https://stackoverflow.com/questions/29331302/chrome-extension-popup-installation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!