开发者

Open a "Help" page after Chrome extension is installed first time

I am new to Chrome extension. I have a question about how to make the extension to open a "Help" page 开发者_运维百科automatically after installation. Currently, I am able to check whether the extension is running the first time or not by saving a value into localStorage. But this checking is only carried out when using click the icon on the tool bar. Just wondering if there is a way that likes FF extension which uses the javascript in to open a help page after the installation. Thanks.

Edit: Thanks for the answer from davgothic. I have solved this problem. I have another question about the popup. My extension checks the url of current tab,

if OK(url){
    //open a tab and do something
}
else{
    //display popup
}
Is it possible to show the popup in this way?


Check this updated and most reliable solution provided by Chrome: chrome.runtime Event

chrome.runtime.onInstalled.addListener(function (object) {
    let externalUrl = "http://yoursite.com/";
    let internalUrl = chrome.runtime.getURL("views/onboarding.html");

    if (object.reason === chrome.runtime.OnInstalledReason.INSTALL) {
        chrome.tabs.create({ url: externalUrl }, function (tab) {
            console.log("New tab launched with http://yoursite.com/");
        });
    }
});

Add this to your background.js I mean the the page you defined on manifest like following,

....
"background": {
      "scripts": ["background.js"],
      "persistent": false
  }
...


UPDATE: This method is no longer recommended. Please see Nuhil's more recent answer below.

I believe what you need to do is put something like this into a script in the <head> section of your extension's background page, e.g. background.html

function install_notice() {
    if (localStorage.getItem('install_time'))
        return;

    var now = new Date().getTime();
    localStorage.setItem('install_time', now);
    chrome.tabs.create({url: "installed.html"});
}
install_notice();


As of now (Aug 2022) the right way to execute code on first install or update of an extension using Manifest V3 is by using the runtime.onInstalled event.

This event is documented here: https://developer.chrome.com/extensions/runtime#event-onInstalled

There is one example for this exact case in the docs now: https://developer.chrome.com/docs/extensions/reference/tabs/#opening-an-extension-page-in-a-new-tab

Note: This example above is wrong as the callback function parameter is Object with the key reason and not reason directly.

And another example here (this one is correct but does not open a tab): https://developer.chrome.com/docs/extensions/reference/runtime/#example-uninstall-url

chrome.runtime.onInstalled.addListener((details) => {
  if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
    // Code to be executed on first install
    // eg. open a tab with a url
    chrome.tabs.create({
      url: "https://google.com"
    });
  } else if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
    // When extension is updated
  } else if (details.reason === chrome.runtime.OnInstalledReason.CHROME_UPDATE) {
    // When browser is updated
  } else if (details.reason === chrome.runtime.OnInstalledReason.SHARED_MODULE_UPDATE) {
    // When a shared module is updated
  }
});

This code can be added to a background service worker: https://developer.chrome.com/docs/extensions/mv3/migrating_to_service_workers/


It would be better to place a "version" number so you can know when an extension is updated or installed.

It has been answered here: Detect Chrome extension first run / update


All you need to do is adding the snippet below to your background.js file

chrome.runtime.onInstalled.addListener(function (object) {
    chrome.tabs.create({url: `chrome-extension://${chrome.runtime.id}/options.html`}, function (tab) {
        console.log("options page opened");
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜