wblinkable

让网页版微博的链接可以在新标签页打开

  1. // ==UserScript==
  2. // @license MIT
  3. // @name wblinkable
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.1
  6. // @description 让网页版微博的链接可以在新标签页打开
  7. // @author xiongchengqing
  8. // @match https://m.weibo.cn/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=weibo.cn
  10. // @run-at document-end
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // 让 github.com 的文本可以点击
  18. function makeGithubTextLinkable(node) {
  19. const REGEX = /(((https?:)?\/?\/\/)?([0-9a-z_\-]+\.)?[0-9a-z_\-]+\.[0-9a-z_\-]+\/[0-9a-z_\-\/\.]*)/ig;
  20. let innerHTMLCopy = node.innerHTML
  21. let res;
  22.  
  23. while((res = REGEX.exec(node.innerHTML)) !== null) {
  24. if (
  25. !res[0].startsWith('https://') &&
  26. !res[0].startsWith('http://') &&
  27. !res[0].startsWith('//') &&
  28. !res[0].startsWith('://')
  29. ) {
  30. innerHTMLCopy = innerHTMLCopy.replace(res[0], `<a href="https://${res[0]}" target="_blank">${res[0]}</a>`);
  31. }
  32. };
  33.  
  34. node.innerHTML = innerHTMLCopy;
  35. node.classList.add('handled');
  36. }
  37.  
  38. // 扫描微博文本并处理
  39. function curryHandleTweet() {
  40. let nodes = null;
  41. let id = null;
  42.  
  43. return function() {
  44. if (id) return;
  45. const promise = new Promise((resolve) => {
  46. id = setInterval(() => {
  47. nodes = document.querySelectorAll('.weibo-text');
  48. if (nodes && nodes.length) {
  49. nodes = Array.from(nodes).filter(node => !node.classList.contains('handled'));
  50. if (nodes.length) {
  51. for (const item of nodes) {
  52. makeGithubTextLinkable(item);
  53. }
  54. }
  55. }
  56.  
  57. if (!nodes.length) resolve();
  58. }, 100);
  59. }).then(() => {
  60. clearInterval(id);
  61. id = null;
  62. });
  63. };
  64. }
  65.  
  66. const handleTweet = curryHandleTweet();
  67.  
  68. window.addEventListener('scroll', handleTweet);
  69. window.addEventListener('pushState', handleTweet);
  70. window.addEventListener('popstate', handleTweet);
  71. handleTweet();
  72. })(window);

QingJ © 2025

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