0

我正在尝试存储一些值,这些值将在 Greasemonkey 中通过页面加载和浏览器关闭持续存在。我的代码如下:

// @name     Netflix saw it button
// @description     Apparently Netflix can't afford enough storage space or programmers to make it easy to know which shows you've seen before. This fixes that. Unfortunately, it will only store data in one browser at a time so you have to use the same computer and browser for the data to store. Sorry about that, but I'm not a Netflix tech so this is the best we got.
// @version  1
// @include     https://www.netflix.com/*
// @grant    none
// @grant    GM.getValue
// @grant    GM.setValue
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js 
// ==/UserScript==


$(document).ready(function() {  
  $('[data-tracking-uuid]').each(function(){
    id= $(this).data('tracking-uuid');
    console.log(id);
    console.log(typeof id);
    GM.setValue(id,1);
    console.log(GM.getValue(id));
    if(GM.getValue($(this).data('tracking-uuid')))
      $(this).closest('.title-card-container').addClass('g_watched');
  });
});

如您所见,我正在测试持久存储,但它并没有给我预期的结果。当我检查控制台时,我可以看到 id 并且 id 的类型是字符串(GM.setValue 需要)。在它尝试设置值的下一行,它停止执行 JS 并且没有其他行运行。没有错误被抛出。它只是死了。

当我在那里没有 setvalue 而只有 getvalue 时也是一样的(如果之前没有设置它应该返回 null )。我究竟做错了什么?这是 Greasemonkey > 4.0 所以这应该是正确的语法,但没有任何错误或反馈,我被卡住了。

4

2 回答 2

1

GM.getValue&GM.setValueasync这意味着你必须等待它,然后再继续。

一个例子来解释这个过程:

// set  value
GM.setValue(id,1);            // async operation
// script runs but value is still in the process of setting
const a = GM.getValue(id);    // async operation
// script runs but value is still in the process of getting
console.log(a);               // a is not set yet

如何解决上述异步问题

(async() => {
  
  // set  value
  await GM.setValue(id,1);          // async opertaion wait for it before moving on
  const a = await GM.getValue(id);  // async opertaion wait for it before moving on
  console.log(a);                   // a is avialble

})();

这是您的脚本示例,async/await
首先,@grant none@grant GM.getValue等冲突

// @name          Netflix saw it button
// @description   Apparently Netflix can't afford enough storage space or programmers to make it easy to know which shows you've seen before. This fixes that. Unfortunately, it will only store data in one browser at a time so you have to use the same computer and browser for the data to store. Sorry about that, but I'm not a Netflix tech so this is the best we got.
// @version       1
// @include       https://www.netflix.com/*
// @grant         GM.getValue
// @grant         GM.setValue
// @require       https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js 
// ==/UserScript==


$(document).ready(async function() {  
  $('[data-tracking-uuid]').each(function(){
    id= $(this).data('tracking-uuid');
    console.log(id);
    console.log(typeof id);
    await GM.setValue(id,1);
    console.log(await GM.getValue(id));
    if(await GM.getValue($(this).data('tracking-uuid')))
      $(this).closest('.title-card-container').addClass('g_watched');
  });
});
于 2020-11-12T08:41:58.227 回答
0

我在 Github 上发布了这个并得到了答案。授予 none 使所有其他人无效,因此需要将其删除。

此外,4.0+ 中的 set 和 get value 方法是“承诺”,因此您需要按原样使用它们(在我的情况下,这意味着使用 await 使其同步)。

于 2020-09-13T12:46:55.627 回答