无法让 GM_notification 函数在 Tampermonkey 用户脚本(chrome、OSX)中工作

问题描述 投票:0回答:1

我正在尝试重写一些 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')
})()

但这是同样的事情(没有通知或错误)

我在调试时尝试了一些事情:

  1. 尝试使用其他
    @sandbox
    值:MAIN_WORLD、ISOLATED_WORLD、USERSCRIPT_WORLD、raw、DOM
  2. TM插件或脚本似乎没有任何权限问题
  3. 在脚本设置中将 GM 功能添加到此窗口设置为
  4. 运行于设置为:文档开始、文档正文、文档结束
  5. 对于位置设置,我尝试了137
  6. 咨询了Google大神,发现了一些类似的线程(例如:Uncaught ReferenceError: GM_notification is not Defined),但最终我还没有找到任何修复它的东西。

当地规格:

  • 操作系统:macOS Big Sur
  • 浏览器:Chrome 版本 120.0.6099.129(官方版本)(x86_64)
  • Tampermonkey版本:v5.0.0

如有任何帮助,我们将不胜感激。谢谢。

javascript google-chrome greasemonkey tampermonkey
1个回答
0
投票

我找到了答案(感谢[this][1])。

这不是 Chrome 中的扩展设置或安全设置,而是 Chrome 的系统安全系统。我转到我的系统偏好设置>通知>查找Chrome并发现它已被禁用: [![Chrome 的系统通知首选项][2]][2]

启用后,通知就可以正常工作。

谢谢! [1]:

https://www.makeuseof.com/google-chrome-notifications-not-working-fixes/

[2]:https://i.stack.imgur.com/bxqUA.png

© www.soinside.com 2019 - 2024. All rights reserved.