我如何访问当前未连接的URL的本地存储?

huangapple go评论120阅读模式
英文:

How do I access the local storage of a URL that I am not currently connected to?

问题

JavaScript初学者。

在我没有主动访问该网站的情况下,是否有办法访问另一个URL的本地存储?

如果我在https://www.youtube.com/,并且我打开浏览器的本地存储(在这种情况下是Chrome),那里会有两个不同的URL,一个是https://www.youtube.com/,另一个是https://accounts.youtube.com/。

我不知道如何访问这个“次要”的本地存储,或者你可以怎么称呼它,以用于一个网站。

例如,当我访问youtube.com时,本地存储看起来是这样的:

我如何访问当前未连接的URL的本地存储?

请注意在本地存储选项卡中有两个不同的URL。

每个URL都有自己的本地存储,其中包含不同的值。

https://www.youtube.com/的本地存储:

我如何访问当前未连接的URL的本地存储?

https://accounts.youtube.com/的本地存储:

我如何访问当前未连接的URL的本地存储?

我该如何访问localStorage["foo"]以从accounts.youtube.com获得它?

当前,localStorage["foo"]将返回undefined,因为它正在查看主要URL(https://www.youtube.com/)的本地存储,但我想以某种方式访问“次要”URL(https://accounts.youtube.com/)

那么,有没有办法让我使用JavaScript访问这个“次要”的存储,还是我只是错误地看待了这个问题/误解了什么?

英文:

JavaScript beginner here.

Is there a way to access the local storage of another URL when I am not actively visiting that site?

If I am on https://www.youtube.com/ and I go to local storage on my browser (Chrome in this case), there are two separate URLs there, one for https://www.youtube.com/ and one for https://accounts.youtube.com/.

I don't know how to go about accessing this "secondary" local storage, or whatever you would call it, for a website.

For example, when I go to youtube.com, this is what the local storage looks like:

我如何访问当前未连接的URL的本地存储?

Note the two separate URLs in the Local Storage tab.

Each URL has its own respective local storage, with different values in each.

Local storage for https://www.youtube.com/:

我如何访问当前未连接的URL的本地存储?

Local storage for https://accounts.youtube.com/:

我如何访问当前未连接的URL的本地存储?

How would I go about accessing localStorage["foo"] from accounts.youtube.com?

Currently, localStorage["foo"] would just be undefined, because it is looking at the local storage for the primary url (https://www.youtube.com/) but I want to somehow access the "secondary" URL (https://accounts.youtube.com/)

So is there any way for me to access this "secondary" storage using JavaScript, or am I simply looking at this the wrong way/misunderstanding something?

答案1

得分: 1

这是评论中其他人已经提到的,这是不可能的。这是出于安全考虑而设计的。这里有一篇相当不错的文章,涵盖了同源策略、localStorage 和 sessionStorage。

https://auth0.com/blog/secure-browser-storage-the-facts/

英文:

As others have said in the comments, this is not possible. It's by design for security. Here is a pretty decent article that covers the same origin policy, localStorage, and sessionStorage.

https://auth0.com/blog/secure-browser-storage-the-facts/

huangapple
  • 本文由 发表于 2023年7月31日 22:10:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定