我如何指示Chrome扩展程序在每次打开HTML时不重新加载我的HTML? [英] How can I instruct Chrome Extension to NOT reload my HTML each time it is opened?

查看:140
本文介绍了我如何指示Chrome扩展程序在每次打开HTML时不重新加载我的HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,我有一个输入元素,它需要在我关闭并重新打开扩展程序后保持填充。



这个页面,我找到了一个清单的例子。它引导我将以下内容添加到我的清单中,但它不起作用:

 background:{
persistent:true,
page:popup.html
}

有没有一种方法可以保持开放之间的扩展状态? 解决方案

=https://developer.chrome.com/extensions/overview#arch =nofollow> Chrome浏览器扩展的体系结构概览。



A弹出页面是只在弹出窗口打开时才存在的一次性页面;你不能影响这一点。一旦弹出窗口失去焦点,它的HTML文档将被销毁。

相比之下,背景页面(通常没有HTML标记,因此是典型用法scripts而不是page)与persistent:true <只要Chrome运行,code>就存在。因此,它可以保存状态信息。但它是一个隐形页面。



正确的做法是使弹出页面动态化,将其状态保存到后台和/或各种存储API,并在打开时恢复状态。



最小例子:



  // popup.js 
//假设#myInput是文本输入
document.addEventListener('DOMContentLoaded',function(){
chrome.storage。 local.get({setting:default value},function(data){
var inputElement = document.getElementById(myInput);
inputElement.value = data.setting;
inputElement.addEventListener(input,function(e){
chrome.storage.local.set({setting:e.target.value});
});
});
});


I am working on a Chrome Extension and I need it to maintain its state each time it is opened.

For example, I have an input element that needs to stay filled-in after I close and re-open the extension.

On this page, I found an example of the manifest. It lead me to add the following to my manifest, but it didn't work:

"background": {
 "persistent": true,
"page": "popup.html"
}

Is there a way to maintain the extensions state between openings?

解决方案

First things first, read the Architecture Overview of Chrome extensions.

A popup page is a "throwaway" page that only exists as long as the popup is open; you cannot influence that. As soon as the popup loses focus, its HTML document will be destroyed.

In contrast, a background "page" (which usually has no HTML markup, hence the typical use of "scripts" instead of "page") with "persistent": true exists as long as Chrome runs. As such, it can hold state information. But it's an invisible page.

The right approach would be to make the popup page dynamic, save its state to background and/or various storage APIs, and restore state when opening.

Minimal example:

// popup.js
// Suppose #myInput is a text input
document.addEventListener('DOMContentLoaded', function() {
  chrome.storage.local.get({setting: "default value"}, function(data) {
    var inputElement = document.getElementById("myInput");
    inputElement.value = data.setting;
    inputElement.addEventListener("input", function(e) {
      chrome.storage.local.set({setting: e.target.value});
    });
  });
});

这篇关于我如何指示Chrome扩展程序在每次打开HTML时不重新加载我的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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