将手机版网页转换为PC版网页

将京东、B站、淘宝、天猫、微博、知乎、豆瓣手机版网页转换为PC版网页

  1. // ==UserScript==
  2. // @name 将手机版网页转换为PC版网页
  3. // @namespace none
  4. // @version 0.8
  5. // @description 将京东、B站、淘宝、天猫、微博、知乎、豆瓣手机版网页转换为PC版网页
  6. // @author owovo
  7. // @match *://item.m.jd.com/*
  8. // @match *://www.bilibili.com/mobile/video/*
  9. // @match *://detail.m.tmall.com/*
  10. // @match *://h5.m.taobao.com/*
  11. // @match *://shop.m.jd.com/*
  12. // @match *://m.weibo.cn/*
  13. // @match *://zhuanlan.zhihu.com/p/*
  14. // @match *://www.zhihu.com/question/*
  15. // @match *://m.douban.com/*
  16. // @grant none
  17. // ==/UserScript==
  18.  
  19. (function() {
  20. 'use strict';
  21.  
  22. // URL转换规则配置
  23. const urlRules = [
  24. {
  25. // 京东商品详情页
  26. regex: /^https?:\/\/item\.m\.jd\.com\/(?:product\/(\d+)|detail\/(\d+))\.html(?:[\?#].*)?$/,
  27. replace: (match, p1, p2) => `https://item.jd.com/${p1 || p2}.html`,
  28. description: "京东商品详情页转换(product/ 或 detail/ 路径)"
  29. },
  30. {
  31. regex: /^https?:\/\/item\.m\.jd\.com\/ware\/view\.action\?.*wareId=(\d+).*$/,
  32. replace: 'https://item.jd.com/$1.html',
  33. description: "京东商品详情页转换(wareId 参数)"
  34. },
  35. {
  36. // 京东店铺首页
  37. regex: /^https?:\/\/shop\.m\.jd\.com\/shop\/home\/(\w+)\.html$/,
  38. replace: 'https://shop.jd.com/home/popup/shopHome.html?id=$1',
  39. description: "京东店铺首页转换"
  40. },
  41. {
  42. // 哔哩哔哩
  43. regex: /^https?:\/\/www\.bilibili\.com\/mobile\/video\/(av\d+|bv\w+)\.html$/,
  44. replace: 'https://www.bilibili.com/video/$1/',
  45. description: "哔哩哔哩视频页转换(支持 av 号和 bv 号)"
  46. },
  47. {
  48. // 天猫
  49. regex: /^https?:\/\/detail\.m\.tmall\.com\/item\.htm\?(.*)$/,
  50. replace: (match, params) => {
  51. try {
  52. const id = new URLSearchParams(params).get('id');
  53. return id && `https://detail.tmall.com/item.htm?id=${id}`;
  54. } catch (error) {
  55. return null;
  56. }
  57. },
  58. description: "天猫商品详情页转换(使用 URLSearchParams 处理参数)"
  59. },
  60. {
  61. // 淘宝
  62. regex: /^https?:\/\/h5\.m\.taobao\.com\/awp\/core\/detail\.htm\?(.*)$/,
  63. replace: (match, params) => {
  64. try {
  65. const id = new URLSearchParams(params).get('id');
  66. return id && `https://item.taobao.com/item.htm?id=${id}`;
  67. } catch (error) {
  68. return null;
  69. }
  70. },
  71. description: "淘宝商品详情页转换(使用 URLSearchParams 处理参数)"
  72. },
  73. {
  74. // 新浪微博
  75. regex: /^https?:\/\/m\.weibo\.cn\/(.*)$/,
  76. replace: 'https://weibo.com/$1',
  77. description: "新浪微博转换"
  78. },
  79. {
  80. // 知乎(文章)
  81. regex: /^https?:\/\/zhuanlan\.zhihu\.com\/p\/(.*)$/,
  82. replace: 'https://zhuanlan.zhihu.com/p/$1',
  83. description: "知乎文章转换"
  84. },
  85. {
  86. // 知乎(问题)
  87. regex: /^https?:\/\/www\.zhihu\.com\/question\/(\d+)(?:\/.*)?$/,
  88. replace: 'https://www.zhihu.com/question/$1',
  89. description: "知乎问题转换"
  90. },
  91. {
  92. // 豆瓣
  93. regex: /^https?:\/\/m\.douban\.com\/(.*)$/,
  94. replace: 'https://www.douban.com/$1',
  95. description: "豆瓣转换"
  96. }
  97. ];
  98.  
  99. // 通用工具函数
  100. const utils = {
  101. isValidUrl: (url) => {
  102. try {
  103. new URL(url);
  104. return true;
  105. } catch (e) {
  106. return false;
  107. }
  108. },
  109.  
  110. safeReplaceUrl: (url, regex, replace) => {
  111. try {
  112. const newUrl = typeof replace === 'function'
  113. ? url.replace(regex, replace)
  114. : url.replace(regex, replace);
  115. return this.isValidUrl(newUrl) ? newUrl : null;
  116. } catch (e) {
  117. return null;
  118. }
  119. }
  120. };
  121.  
  122. // 主逻辑
  123. const currentUrl = window.location.href;
  124.  
  125. const matchedRule = urlRules.find(rule => {
  126. const { regex, replace } = rule;
  127. const newUrl = utils.safeReplaceUrl(currentUrl, regex, replace);
  128.  
  129. if (newUrl && newUrl !== currentUrl) {
  130. window.location.replace(newUrl);
  131. return true;
  132. }
  133. return false;
  134. });
  135. })();