Local Storage代码在Opera中完美工作,但在Safari中只有一半

发布于 2025-02-14 01:17:20 字数 554 浏览 1 评论 0 原文

我已经编码PWA已有大约一个星期了,我需要一些帮助,以了解为什么代码在Opera中完全有效,而在Safari中不起作用。

如何复制我的问题:

  1. 打开我的项目( link> link ) )和Opera(或Chrome或其他任何)
  2. 单击左侧的第三个按钮,
  3. 一旦做饭,就做饭(随机做一些随机的东西),然后注意切换打开并打开它。
  4. 重新加载页面,看看餐点仍然显示出来,但切换不会显示Safari。
  5. 由于某种原因,它在Safari中不起作用,

我是在使用Safari中不允许的代码吗?我如何解决这个问题?

我还通过firebase 在这里

I have been coding my PWA for about a week now and I need some help finding out why the code works completely in Opera, but not in Safari.

How to replicate my issue:

  1. Open my project (link) in both Safari (on mac or iphone or whatever) and Opera (or chrome or whatever)
  2. Click the 3rd button from the left and make a meal (make some random stuff up)
  3. Once you have made the meal, then notice the toggle switch and switch it on.
  4. Reload the page and see that the meal still shows but the toggle doesn't show for safari.
  5. It doesn't work in Safari for some reason

Am I using code that shouldn't be allowed in Safari or something? How do I work around this?

I have deployed a link as well to the site via firebase here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不弃不离 2025-02-21 01:17:20

在对不和谐进行了一些调试之后,我们发现在 script.js 中,这些行引起了问题:

if (mealTargetted) {
      userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText).addedToShoppingList = false;
      localStorage.setItem('data', JSON.stringify(userData));
} else {
      userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText).addedToShoppingList = true;
      localStorage.setItem('data', JSON.stringify(userData));
}

https://github.com/rohilpatel1/calebra/blob/78b13a86859c90d1897a6d9865e37124db636db636d58/script.jscript.jspript.js#l77

“ Safari, e.target.parentnode.parentnode.innertext 与已保存的LocalStorage相比,有一个尾随空间:

console.log(userdata.mealInformation,e.target.parentnode.parentnode.innertext)

这意味着 a.name == e.target.parentnode.parentnode.innode.innertext.innertext.trim()永远不会满足,因此 userdata.mealInformation.find(a => a.name ==== e.target.parentnode.parentnode.innertext)返回未定义,扔 undefined不是对象错误,导致切换似乎不更改。

解决方案

是什么意思是我们必须修剪 e.target.parentnode.parentnode.innertext

if (mealTargetted) {
      userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText.trim()).addedToShoppingList = false;
      localStorage.setItem('data', JSON.stringify(userData));
} else {
      userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText.trim()).addedToShoppingList = true;
      localStorage.setItem('data', JSON.stringify(userData));
}

所以这不是局部通讯问题,只是代码问题

After some debugging on discord, we found that in script.js, these lines were causing the issue:

if (mealTargetted) {
      userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText).addedToShoppingList = false;
      localStorage.setItem('data', JSON.stringify(userData));
} else {
      userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText).addedToShoppingList = true;
      localStorage.setItem('data', JSON.stringify(userData));
}

Github

Error

What was happening was that somehow, in safari, e.target.parentNode.parentNode.innerText had a trailing space compared to the saved localStorage result:
As you can see, e.innerText has a trailing space

console.log(userData.mealInformation, e.target.parentNode.parentNode.innerText)

This meant that a.name == e.target.parentNode.parentNode.innerText.trim() was never satisfied, and so userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText) returns undefined, throwing an undefined is not an object error, causing the toggle to appear not to change.

Solution

What this means was that we had to trim e.target.parentNode.parentNode.innerText:

if (mealTargetted) {
      userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText.trim()).addedToShoppingList = false;
      localStorage.setItem('data', JSON.stringify(userData));
} else {
      userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText.trim()).addedToShoppingList = true;
      localStorage.setItem('data', JSON.stringify(userData));
}

So this wasn't a localStorage issue, just a code issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文