Javascript Firefox本地存储在其他选项卡中不可用
我正在创建一个firefox扩展。我想使用localStorage作为整个浏览器的全局变量。但它仅适用于保存它的选项卡。我无法在其他选项卡中读取此值。我如何使其可以从任何选项卡访问,或者可能存在哪些问题 我使用它的方式如下:Javascript Firefox本地存储在其他选项卡中不可用,javascript,firefox,Javascript,Firefox,我正在创建一个firefox扩展。我想使用localStorage作为整个浏览器的全局变量。但它仅适用于保存它的选项卡。我无法在其他选项卡中读取此值。我如何使其可以从任何选项卡访问,或者可能存在哪些问题 我使用它的方式如下: localStorage.getItem('variable') localStorage.setItem('variable','value') 更准确地说,我在页面加载时将javascript代码注入到页面中。从注入的代码中,我想将我的值保存到本地存储 选项卡具有不同
localStorage.getItem('variable')
localStorage.setItem('variable','value')
更准确地说,我在页面加载时将javascript代码注入到页面中。从注入的代码中,我想将我的值保存到本地存储
选项卡具有不同的url。我的代码试图在加载页面时使用localstorage。但它会检查localStorage值是否存在,如下所示:
if(localStorage.getItem('variable')){ ... }
我认为不能将
localStorage
用作全局变量
localStorage is also the same as globalStorage[location.hostname], with the exception
of being scoped to an HTML5 origin (scheme + hostname + non-standard port) and
localStorage being an instance of Storage as opposed to
globalStorage[location.hostname] being an instance of StorageObsolete which is covered
below. For example, http://example.com is not able to access the same localStorage
object as https://example.com but they can access the same globalStorage item.
localStorage is a standard interface while globalStorage is non-standard so you
shouldn't rely on these.
Please note that setting a property on globalStorage[location.hostname] does not set
it on localStorage and extending Storage.prototype does not affect globalStorage
items, only extending StorageObsolete.prototype does.
考虑使用globalStorage
,然后在需要的地方设置localStorage
来源:其他选项卡是否具有相同的来源?如果不是,就不要掷骰子……你是按那个顺序做的吗?在设置之前获取项目?@DavidStarkey是的,我正在尝试在更新之前阅读它。但在阅读之前,我会检查它是否存在。查看我的帖子更新,了解我的代码checking@Jigberto如果选项卡具有不同的域地址,则“否”,则无法在选项卡之间访问
localStorage
。如果选项卡具有相同的域,它应该可以工作。@Jigberto假设不同的服务器可以相互访问或使用公共资源,您可以为此使用服务器端解决方案。您不应该建议使用过时和非标准的解决方案。除非您使用的是介于2和13之间的FireFox版本,否则它不会工作。为什么你会认为这是一个好的解决方案?@Ian OP说的是“Firefox”,而不是“Firefox 1”或“Firefox 20”。此解决方案涵盖多个版本。如果你有更好的选择,请分享。当然,“多个版本”。约占Firefox市场份额的12%。不,我没有更好的选择,我从来没有这样说过或做过。但是肯定有一种更完整的方法,因为扩展是如何工作的。通常,浏览器扩展有一些“后台”进程正在运行(可以存储数据或某种形式的永久存储),并且DOM通常有某种方式与扩展通信,因此应该很容易将这些想法组合在一起。我从未专门开发过Firefox扩展,所以我不知道具体细节。