- // ==UserScript==
- // @name Aliexpress URL Cleaner
- // @version 0.4
- // @description Removes unnecessary parameters from Aliexpress URLs
- // @match *://*.aliexpress.com/*
- // @match https://www.3cseller.com/*
- // @namespace https://www.chinaobd2.com
- // @run-at document-start
- // @noframes
- // ==/UserScript==
-
-
- function whenReady() {
- return new Promise((resolve) => {
- function completed() {
- document.removeEventListener('DOMContentLoaded', completed);
- window.removeEventListener('load', completed);
- resolve();
- }
-
- if (document.readyState === 'complete'
- || document.readyState === 'interactive') {
- resolve();
- } else {
- document.addEventListener('DOMContentLoaded', completed);
- window.addEventListener('load', completed);
- }
- });
- }
-
-
- whenReady().then(() => {
-
- let reg = /((?:https?:)?\/\/(?:\w+\.)?aliexpress\.com\/(?:store\/product\/[^\/]+\/[0-9_]+|item\/(?:[^\/]+\/)?[0-9_]+)\.html)(\?[^#\r\n]+)?(#.+)?/i;
-
- function toCanonical(original) {
- let match = original.match(reg);
- if (match) {
- return match[1] + (match[3] || '');
- }
- return null;
- }
-
-
- // For current tab URL.
- let canonical = toCanonical(window.location.href);
- if (!canonical) {
- let link = document.querySelector('head > link[rel=canonical]');
- if (link) {
- canonical = toCanonical(link.href + window.location.hash);
- }
- }
- if (canonical) {
- window.history.replaceState(history.state, document.title, canonical);
- }
-
-
- // For static html links.
- document.querySelectorAll('a').forEach((e) => {
- let canonical = toCanonical(e.href);
- if (canonical) {
- e.href = canonical;
- }
- });
-
-
- // For lazy-loaded links.
- let observer = new MutationObserver(function (mutationsList) {
- for (let i = 0; i < mutationsList.length; i++) {
- const mutation = mutationsList[i];
- const addedNodes = mutation.addedNodes;
- for (let j = 0; j < addedNodes.length; j++) {
- cleanAndTraverse(addedNodes[j]);
- }
- if (mutation.type === 'attributes') {
- cleanNodeHref(mutation.target)
- }
- }
- });
-
- function cleanAndTraverse(root) {
- cleanNodeHref(root);
- let children = root.children;
- if (children) {
- for (let k = 0; k < children.length; k++) {
- cleanAndTraverse(children[k]);
- }
- }
- }
-
- function cleanNodeHref(elem) {
- if (elem.tagName === 'A') {
- const original = elem.href;
- const canonical = toCanonical(elem.href);
- if (canonical && original !== canonical) {
- elem.href = canonical;
- }
- }
- }
-
- observer.observe(document.documentElement, {
- childList: true,
- subtree: true,
- attributes: true,
- attributeFilter: ['href']
- });
- });
posted @
2022-11-15 22:09
hilong911
阅读(
74)
评论()
编辑
收藏
举报
点击右上角即可分享
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?