Chrome扩展后安装挂钩/ API函数:它存在吗? [英] Chrome extension post-install hook/API function: does it exist?

查看:182
本文介绍了Chrome扩展后安装挂钩/ API函数:它存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个Chrome扩展后安装钩子/ API函数,可让我在插件安装或更新后执行操作?

我想执行在我的扩展程序安装后的操作,并且仅在安装后立即执行。此操作只应执行一次(安装后或更新后)。



更新



有人建议在localStorage中设置扩展的版本,我遇到的问题是当第一次安装插件时,访问localStorage的内容脚本没有加载到页面中。



在插件安装完成后,AFAIK使用注入到标签/页面的内容脚本,页面必须重新载入。



我不知道如何从后台页面访问localStorage; localStorage只能从内容脚本访问。

要从后台页面获取版本号到内容脚本,需要使用chrome API函数来执行脚本:



chrome.tabs.executeScript(null,{code:function_to_execute},function(){// callback});



然而,当你安装一个插件时,这个插件需要注入一个内容脚本的页面已经被加载,它不会插入内容脚本,你必须重新加载页面。



update 2



查看更详细的一些提示,为了保存版本号,它是可以访问后台页面的localStorage。但是,对于我需要做的事情,即在安装或更新插件后,在特定URL(为了确保内容脚本是最新版本)上重新加载特定选项卡,它最终不必再打扰localStorage。

为了保留主题,我们给出了关于将版本号写入localStorage(在后台页面中)的建议,然后检查清单文件中的版本号足以让某人在第一次安装/更新脚本时运行脚本。

HowTo



使清单文件可用于后台页面(注意:这是从其他地方取得的,不要为此付出代价,但是我不记得消息来源,如果你知道,请告诉我,我会加上它。)

  //使MANIFEST FILE AVAILABLE 
chrome.manifest =(function(){
var manifestObject = false;
var xhr = new XMLHttpRequest();
xhr .onreadystatechange = function(){
if(xhr.readyState == 4){
manifestObject = JSON.parse(xhr.responseText);
}
};
xhr.open(GET,chrome.extension.getURL('/ manifest.json'),false);
try {
xhr.send();
} catch(e ){
console.log('Couldn''t load manifest.json');
}
return manifestObject;
})();

现在您可以像这样访问您的版本号: chrome.manifest.version



要写入localStorage,只需像下面这样传递它: localStorage ['my_plugin_version'] = chrome.manifest。版本

解决方案

您可以使用后台页面。当安装扩展程序时,后台页面将在后台打开并执行。为确保每次都不执行,只需将值存储在 localStorage 中。


Is there a Chrome extension post install hook/API function that will let me perform an action after the plugin is installed or updated?

I would like to perform an action after my extension is installed, and only right after it is installed. This action should only be performed once (post-install or post-update) of the extension.

Update

Some people have proposed setting the version of the extension in localStorage, the problem that I have is that the content script that has access to localStorage is not loaded into the page when the plugin is first installed.

AFAIK after a plugin is installed, and it makes use of a content script injected into the tab/page, the page has to be reloaded.

I don't know how to access localStorage from the background page; localStorage can only be accessed from a content script.

To get the version number from the background page to the content script requires the use of chrome API function to execute scripts:

chrome.tabs.executeScript(null, {code:function_to_execute}, function() { // callback });

However, when you install a plugin, and the page that this plugin needs to inject a content script into is already loaded, it does not inject the content script, you have to reload the page.

update 2

Looking at some of the tips provided in more detail, for the purpose of saving the version number, it is possible to access the localStorage of the background page. However, for what I need to do, which is reload a specific tab at a specific URL (in order to make sure the content script is the newest version) after installing or updating a plugin, it ended up being unnecessary to bother with localStorage.

For the sake of staying on topic, the advice given about writing the version number to localStorage (in the background page) and then checking against the version number in the manifest file is good enough to allow someone to run a script the first time it is installed/or updated.

HowTo

Make manifest file available to the background page (note: this is taken from somewhere else, I don't take credit for it, but I can't remember the source, if you know, let me know and I will add it).

// MAKE MANIFEST FILE AVAILABLE
chrome.manifest = (function() {
    var manifestObject = false;
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            manifestObject = JSON.parse(xhr.responseText);
        }
    };
    xhr.open("GET", chrome.extension.getURL('/manifest.json'), false);
    try {
        xhr.send();
    } catch(e) {
        console.log('Couldn\'t load manifest.json');
    }
    return manifestObject;
})();

Now you can access your version number like this: chrome.manifest.version

To write to localStorage just pass it in like so: localStorage['my_plugin_version'] = chrome.manifest.version

解决方案

You can do this using a background page. When the extension is installed, the background page is opened in the background, and thus executed. To make sure it's not executed every time, simply store a value in localStorage.

这篇关于Chrome扩展后安装挂钩/ API函数:它存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆