李恒道 发表于 2022-11-27 00:14:34

wwwwwllllk 发表于 2022-11-26 23:12
写完了记得分享一下,嘿嘿。不知道那个api有没有坑

如果有坑的话那多半是不可能没有坑
如果没有坑的话肯定不可能有坑

王一之 发表于 2022-11-27 08:39:49

哥哥代码贴这边?

Su. 发表于 2022-11-27 13:39:46

> [王一之 发表于 2022-11-27 08:39](forum.php?mod=redirect&goto=findpost&pid=43452&ptid=3743)
> > 哥哥代码贴这边?

嗨嗨嗨,来咯,跟哥哥脚本交互帖子里的代码基本一样
```js
// ==UserScript==
// @name         前端任务提交脚本
// @storageName    test
// @version      0.1.0
// @descriptiontry to take over the world!
// @author       You
// @match      https://bbs.tampermonkey.net.cn/
// @grant      GM_setValue
// ==/UserScript==

let btn = document.createElement('button');
btn.innerText = "提交一个任务给后台脚本";

btn.onclick = () => {
    GM_setValue('task', '任务详情...');
    alert('任务提交成功');
}

document.querySelector('#diymsg1').append(btn);
```

```js
// ==UserScript==
// @name         后端任务处理脚本
// @storageName    test
// @version      0.1.0
// @descriptiontry to take over the world!
// @author       You
// @grant      GM_notification
// @grant      GM_addValueChangeListener
// @background
// ==/UserScript==

return new Promise((resolve, reject) => {
    GM_addValueChangeListener("task", (name, oldVal, newVal, remote) => {
      console.log(name, oldVal, newVal, remote);
      GM_notification({
            title: "任务处理",
            text: newVal,
      });
    })
});
```

王一之 发表于 2022-11-27 15:23:47

Su. 发表于 2022-11-27 13:39
> [王一之 发表于 2022-11-27 08:39](forum.php?mod=redirect&goto=findpost&pid=43452&ptid=3743)
> > ...

这代码好像没有改变?我运行起来的期望是正确的

Su. 发表于 2022-11-27 15:24:47

王一之 发表于 2022-11-27 15:23
这代码好像没有改变?我运行起来的期望是正确的

嗯,但我这里不知道为什么,每次点击都会出通知

王一之 发表于 2022-11-27 22:09:13

Su. 发表于 2022-11-27 15:24
嗯,但我这里不知道为什么,每次点击都会出通知

啊?是这样的,每次点击都会的

哥哥想要怎么样的?

Su. 发表于 2022-11-27 22:42:15

王一之 发表于 2022-11-27 22:09
啊?是这样的,每次点击都会的

哥哥想要怎么样的?

啊?好吧,我以为是值改变的时候才会执行
看来是我理解有误

王一之 发表于 2022-11-28 00:12:05

Su. 发表于 2022-11-27 22:42
啊?好吧,我以为是值改变的时候才会执行
看来是我理解有误

哥哥可以触发之后就移除监听 GM_removeValueChangeListener

Su. 发表于 2022-11-28 00:16:28

王一之 发表于 2022-11-28 00:12
哥哥可以触发之后就移除监听 GM_removeValueChangeListener

嗯嗯,我加了个判断,就可以变化再通知了
我一开始一直以为,valuechangelistener是变化才会执行
页: 1 [2]
查看完整版本: 前后台脚本求助