简体   繁体   中英

Chrome extension popup installation

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM