简体   繁体   English

从popup.js中清除Chrome扩展程序localStorage

[英]Clearing Chrome extension localStorage from within popup.js

I have everything in my extension working apart from one thing! 我的扩展程序中的所有内容都与一件事无关! I want to clear the local storage of this extension through popup.js. 我想通过popup.js清除此扩展程序的本地存储。 It's a simple extension that saves form data from a certain domain. 这是一个简单的扩展,可以保存来自特定域的表单数据。

Currently, in my content script (save.js) I'm accessing local storage through localStorage[key] however it seems I'm unable to do this through popup.js and I get an 'undefined' which I assume it expected behaviour. 当前,在我的内容脚本(save.js)中,我正在通过localStorage [key]访问本地存储,但是似乎无法通过popup.js进行此操作,并且我得到一个“未定义”,我认为它是预期的行为。

I have tried localStorage.clear(); 我已经尝试过localStorage.clear(); from my popup.js but this has no effect. 从我的popup.js中,但这没有效果。 Presumably because it's looking to clear non existent localStorage from within the extension itself? 大概是因为它要从扩展本身中清除不存在的localStorage吗?

How can I access and clear the local storage that my save.js has been using from popup.js? 如何从popup.js访问和清除save.js一直在使用的本地存储?

popup.js popup.js

 document.getElementById('clearAllData').addEventListener('click', clear); function clear(){ localStorage.clear(); } 

Snippet of how I'm saving data in save.js 我如何在save.js中保存数据的片段

 function saveTicket(ticket, contents) { localStorage[ticketID] = contents; } 

You're not using the callback correctly. 您没有正确使用回调。 You need to pass the function asynchronously. 您需要异步传递函数。 For example: 例如:

function clear(){
   chrome.storage.local.clear(function(obj){
      console.log("cleared");
      });
 }

however it might be this as well. 但是也可能是这样。 Chrome storage is already local. Chrome存储空间已在本地。

function clear(){
   chrome.storage.sync.clear(function(obj){
      console.log("cleared");
      });
 }

You should use chrome.storage instead of localStorage. 您应该使用chrome.storage而不是localStorage。 When you use localStorage in a content script, is it of the page, not of your extension. 在内容脚本中使用localStorage时,它是页面而不是扩展名。 However, when you use chrome.storage in a content script, it is the storage of your extension. 但是,当您在内容脚本中使用chrome.storage时,它就是扩展的存储空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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