github-search-helper

在新标签页打开github搜索,explore和trending结果

  1. // ==UserScript==
  2. // @name github-search-helper
  3. // @namespace github.com/leychan
  4. // @version 0.3
  5. // @description 在新标签页打开github搜索,explore和trending结果
  6. // @author leychan
  7. // @match https://github.com/trending
  8. // @match https://github.com/explore
  9. // @match https://github.com/search*
  10. // @icon https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png
  11. // @run-at document-end
  12. // @grant none
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. // 修改所有 <a> 标签的 target 属性
  20. function setTargetBlank() {
  21. const allATags = document.getElementsByTagName('a');
  22. for (let i = 0; i < allATags.length; i++) {
  23. allATags[i].setAttribute('target', '_blank');
  24. }
  25. }
  26.  
  27. // 初始化 MutationObserver
  28. const observer = new MutationObserver((mutationsList, observer) => {
  29. // 检查是否有新的节点被添加到 DOM 中
  30. mutationsList.forEach(mutation => {
  31. if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
  32. // 如果新增的节点中包含 <a> 标签,则修改其 target 属性
  33. mutation.addedNodes.forEach(node => {
  34. if (node.nodeType === 1 && node.tagName === 'A') { // 检查是否是元素节点且是 <a> 标签
  35. node.setAttribute('target', '_blank');
  36. } else if (node.nodeType === 1 && node.querySelectorAll) { // 检查是否是元素节点且包含子节点
  37. const aTags = node.querySelectorAll('a');
  38. aTags.forEach(aTag => aTag.setAttribute('target', '_blank'));
  39. }
  40. });
  41. }
  42. });
  43. });
  44.  
  45. // 启动 MutationObserver
  46. observer.observe(document.body, { childList: true, subtree: true });
  47.  
  48. // 初始修改页面中所有的 <a> 标签
  49. setTargetBlank();
  50. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址