代码整理速卖通显示网址Aliexpress URL Cleaner

  1. // ==UserScript==
  2. // @name Aliexpress URL Cleaner
  3. // @version 0.4
  4. // @description Removes unnecessary parameters from Aliexpress URLs
  5. // @match *://*.aliexpress.com/*
  6. // @match https://www.3cseller.com/*
  7. // @namespace https://www.chinaobd2.com
  8. // @run-at document-start
  9. // @noframes
  10. // ==/UserScript==
  11.  
  12.  
  13. function whenReady() {
  14. return new Promise((resolve) => {
  15. function completed() {
  16. document.removeEventListener('DOMContentLoaded', completed);
  17. window.removeEventListener('load', completed);
  18. resolve();
  19. }
  20.  
  21. if (document.readyState === 'complete'
  22. || document.readyState === 'interactive') {
  23. resolve();
  24. } else {
  25. document.addEventListener('DOMContentLoaded', completed);
  26. window.addEventListener('load', completed);
  27. }
  28. });
  29. }
  30.  
  31.  
  32. whenReady().then(() => {
  33.  
  34. let reg = /((?:https?:)?\/\/(?:\w+\.)?aliexpress\.com\/(?:store\/product\/[^\/]+\/[0-9_]+|item\/(?:[^\/]+\/)?[0-9_]+)\.html)(\?[^#\r\n]+)?(#.+)?/i;
  35.  
  36. function toCanonical(original) {
  37. let match = original.match(reg);
  38. if (match) {
  39. return match[1] + (match[3] || '');
  40. }
  41. return null;
  42. }
  43.  
  44.  
  45. // For current tab URL.
  46. let canonical = toCanonical(window.location.href);
  47. if (!canonical) {
  48. let link = document.querySelector('head > link[rel=canonical]');
  49. if (link) {
  50. canonical = toCanonical(link.href + window.location.hash);
  51. }
  52. }
  53. if (canonical) {
  54. window.history.replaceState(history.state, document.title, canonical);
  55. }
  56.  
  57.  
  58. // For static html links.
  59. document.querySelectorAll('a').forEach((e) => {
  60. let canonical = toCanonical(e.href);
  61. if (canonical) {
  62. e.href = canonical;
  63. }
  64. });
  65.  
  66.  
  67. // For lazy-loaded links.
  68. let observer = new MutationObserver(function (mutationsList) {
  69. for (let i = 0; i < mutationsList.length; i++) {
  70. const mutation = mutationsList[i];
  71. const addedNodes = mutation.addedNodes;
  72. for (let j = 0; j < addedNodes.length; j++) {
  73. cleanAndTraverse(addedNodes[j]);
  74. }
  75. if (mutation.type === 'attributes') {
  76. cleanNodeHref(mutation.target)
  77. }
  78. }
  79. });
  80.  
  81. function cleanAndTraverse(root) {
  82. cleanNodeHref(root);
  83. let children = root.children;
  84. if (children) {
  85. for (let k = 0; k < children.length; k++) {
  86. cleanAndTraverse(children[k]);
  87. }
  88. }
  89. }
  90.  
  91. function cleanNodeHref(elem) {
  92. if (elem.tagName === 'A') {
  93. const original = elem.href;
  94. const canonical = toCanonical(elem.href);
  95. if (canonical && original !== canonical) {
  96. elem.href = canonical;
  97. }
  98. }
  99. }
  100.  
  101. observer.observe(document.documentElement, {
  102. childList: true,
  103. subtree: true,
  104. attributes: true,
  105. attributeFilter: ['href']
  106. });
  107. });
posted @ 2022-11-15 22:09  hilong911  阅读(64)  评论(0编辑  收藏  举报