我正在尝试重写一些 Tampermonkey 脚本以使用一些内置 TM/GM 函数,但我无法让它们工作。 GM_Notification 函数就是一个例子。
我正在使用文档中的示例 GM_notification 对其进行测试。这是完整的用户脚本文件内容:
// ==UserScript==
// @name Test Notification
// @namespace http://tampermonkey.net/
// @version 2024-01-06
// @description try to take over the world!
// @author You
// @match https://gist.github.com/EtienneDG/fe32d3213d87f79e0da0f829e73ee4ab
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant GM_notification
// @grant GM.notification
// @grant unsafeWindow
// @sandbox JavaScript
// ==/UserScript==
(function() {
'use strict'
console.log('Loaded test notification')
console.log('About to execute GM_notification...');
GM_notification({
text: "This is the notification message.",
title: "Notification Title",
url: 'https:/google.com/',
onclick: (event) => {
// The userscript is still running, so don't open example.com
event.preventDefault();
// Display an alert message instead
alert('I was clicked!')
},
});
console.log('Executed GM_notification')
})()
当我进入加载此脚本的页面时,我完全没有收到任何通知/警报,甚至没有收到错误。 控制台输出如下:
content: normal start event processing for lr2kbof7.baa (1 to run)
VM3242 content.js:55 env: schedule "Test Notification" for document-idle
VM3242 content.js:53 env: inject "Test Notification" now
VM3242 content.js:73 content: DOMContentLoaded
VM3242 content.js:54 env: run "Test Notification" now (0 requires)
userscript.html?name=Test-Notification.user.js&id=a8f4d506-9af0-45b8-8eed-362488ac963e:21 Loaded test notification
userscript.html?name=Test-Notification.user.js&id=a8f4d506-9af0-45b8-8eed-362488ac963e:23 About to execute GM_notification...
userscript.html?name=Test-Notification.user.js&id=a8f4d506-9af0-45b8-8eed-362488ac963e:37 Executed GM_notification
VM3242 content.js:73 content: load
因此脚本清楚地运行,但我在控制台日志中没有看到任何通知或任何错误。
我也尝试过使用
async GM.notification
:
// ==UserScript==
// @name Test Notification
// @namespace http://tampermonkey.net/
// @version 2024-01-06
// @description try to take over the world!
// @author You
// @match https://gist.github.com/EtienneDG/fe32d3213d87f79e0da0f829e73ee4ab
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant GM_notification
// @grant GM.notification
// @grant unsafeWindow
// @sandbox JavaScript
// ==/UserScript==
(async function() {
'use strict'
console.log('Loaded test notification')
console.log('About to execute await GM.notification...');
const clicked = await GM.notification({ text: "Click me." });
console.log('clicked:',clicked)
console.log('Executed await GM.notification')
})()
但这是同样的事情(没有通知或错误)
我在调试时尝试了一些事情:
@sandbox
值:MAIN_WORLD、ISOLATED_WORLD、USERSCRIPT_WORLD、raw、DOM当地规格:
如有任何帮助,我们将不胜感激。谢谢。