本文介绍了如何在扩展后台页面访问localStorage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Chrome扩展,它在内容脚本的JavaScript localStorage中设置一个字符串值。我已经通过抓取并显示它进行了测试。在加载后的同一页面上,我仍然可以访问JavaScript控制台中存储的字符串。如果我转到任何其他选项卡的js控制台,或者当我尝试在我的其他扩展后台页面中访问它时(浏览器操作被点击后)它将返回null。



extension 1 contentscript.js

  embeded.addEventListener 

message,
函数(消息)
{
localStorage.setItem(pdf,message.data);
loadPDF(message.data);
},
false
);

扩展名2 background.js

  chrome.browserAction.onClicked.addListener(function(tab){
if(tab.url.substr(tab.url.length - 4)===.pdf)
{
if(localStorage.getItem(pdf)!== null)
{
alert(boomy);
}
}
});


解决方案

localStorage 在扩展页面(背景,选项,弹出窗口)之间共享,但 不适用于两个扩展名之间。


I have a chrome extension that sets a string value in JavaScript localStorage in a content script. I have tested that it is being set by grabbing it and displaying it. While on the same page after it has loaded I can still access the stored string in the javascript console. If I go to any other tabs js console or when I try to access it in my other extensions background page(After the browser action has been clicked) it is returning null.

extension 1 contentscript.js

embeded.addEventListener
(
 "message",
 function(message)
 {
  localStorage.setItem("pdf", message.data);
  loadPDF(message.data);
 },
 false
);

extension 2 background.js

chrome.browserAction.onClicked.addListener(function(tab) {
  if (tab.url.substr(tab.url.length - 4) === ".pdf")
  {    
    if (localStorage.getItem("pdf") !== null)
    {
      alert("boomy"); 
    }
  }
});
解决方案

localStorage is shared among extension pages (background, options, popup), but With 2 separate extensions, only one option.


Not applicable between 2 extensions.

这篇关于如何在扩展后台页面访问localStorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:26