You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么Violentmonkey脚本无法更新GitHub页面DOM?

解决Violentmonkey脚本添加GitHub已审核PR按钮无效的问题

你的脚本在控制台能运行但油猴里没效果,主要是这几个问题导致的:

  • @grant 配置错误:你把许可证MIT写到了@grant字段里,这完全不对。@grant是用来声明脚本需要的GM权限的,这里不需要特殊权限,应该改成@grant none。错误的配置会导致脚本运行环境异常。

  • jQuery对象误用原生DOM方法:你用a.cloneNode(true)克隆元素,但a是jQuery选择器返回的jQuery对象,不是原生DOM元素。得用jQuery的.clone()方法才能正确克隆,后续属性操作也能正常生效。

  • 变量未声明:代码里的a和b没加let/const声明,在油猴脚本的作用域里可能会导致变量泄漏或访问异常,建议补上声明。

修正后的完整脚本:

// ==UserScript==
// @name        GitHub Pull: All review requests button
// @namespace   Violentmonkey Scripts
// @match       https://github.com/pulls
// @grant       none
// @version     1.0
// @author      Reece Hart <reecehart@gmail.com>
// @description Add button to show previously reviewed pull requests
// @require     https://code.jquery.com/jquery-3.6.1.slim.min.js
// @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
// ==/UserScript==

function updateNavBar () {
  console.log("Adding All review requests button")
  const a = $("a[title='Pull Requests requesting your review']")
  const b = a.clone(true)
  b.attr("title", "All review requests")
  b.html("All review requests")
  b.attr("data-selected-links", b.attr("data-selected-links").replace("review-requested", "reviewed-by"))
  b.attr("href", b.attr("href").replace("review-requested", "reviewed-by"))
  a.after(b)
}

waitForKeyElements ("a[title='Pull Requests requesting your review']", updateNavBar);

另外补充:操作jQuery对象的属性时,推荐用.attr()方法替代原生的setAttribute,更符合jQuery的用法逻辑,也能避免跨环境的潜在问题。

内容的提问来源于stack exchange,提问作者Reece

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 18:01:24