Wikipedia Open Specific Links in New Window (Including Homepage)

在维基百科中点击特定蓝色链接时自动弹出新窗口,排除图片和媒体文件链接,并确保在首页也生效。

  1. // ==UserScript==
  2. // @name Wikipedia Open Specific Links in New Window (Including Homepage)
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description 在维基百科中点击特定蓝色链接时自动弹出新窗口,排除图片和媒体文件链接,并确保在首页也生效。
  6. // @author You
  7. // @match https://zh.wikipedia.org/zh-cn/*
  8. // @match https://zh.wikipedia.org/wiki/Wikipedia:%E9%A6%96%E9%A1%B5
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. // 获取所有蓝色链接
  16. const links = document.querySelectorAll('a');
  17.  
  18. links.forEach(link => {
  19. // 判断链接是否在正文部分,并排除图片和媒体文件链接
  20. if (link.closest('#bodyContent') && !link.closest('.thumb') && !link.href.includes('/wiki/File:')) {
  21. link.addEventListener('click', function(event) {
  22. window.open(link.href, '_blank');
  23. event.preventDefault(); // 阻止默认行为,避免当前页面跳转
  24. });
  25. }
  26. });
  27. })();