Aliexpress short link share button

Adds short link share button on Aliexpress without annoying query params. Check it out near item raiting & order counter! Old & new design supported!

< 脚本 Aliexpress short link share button 的反馈

评价:差评 - 脚本失效或无法使用

§
发布于:2025-09-30
编辑于:2025-09-30

Fixed the code to work. use this (creator - please update the code):

// ==UserScript==
// @name Aliexpress short link share button
// @namespace opravdin.aliexpress
// @version 1.2
// @description Adds a button to copy a clean product link on Aliexpress. Waits for dynamic content to load.
// @homepageURL https://github.com/opravdin/aliexpress-share-userscript
// @author Oleg Pravdin aka opravdin (updated 2025)
// @match *://*.aliexpress.com/item/*
// @match *://aliexpress.com/item/*
// @grant none
// ==/UserScript==

(function() {
'use strict';

const BUTTON_TARGET_SELECTOR = '[data-pl="product-reviewer"] > div > div';

/**
* Waits for an element to appear in the DOM using a MutationObserver.
* @param {string} selector - The CSS selector for the element to find.
* @param {function} callback - Function to execute once the element is found.
*/
const onElementReady = (selector, callback) => {
const observer = new MutationObserver(mutations => {
const element = document.querySelector(selector);
if (element) {
observer.disconnect();
callback(element);
}
});

observer.observe(document.body, {
childList: true,
subtree: true
});

// Fallback timeout in case the element never appears
setTimeout(() => {
observer.disconnect();
}, 15000);
};

/**
* Creates and adds the button to the page.
* @param {HTMLElement} targetContainer - The DOM element to which the button will be added.
*/
const initializeButton = (targetContainer) => {
// --- Settings ---
const hostname = 'aliexpress.com';
const buttonText = `Copy Link`;
const buttonOnCopyText = `Copied!`;

// --- URL Parsing ---
const idRegex = /\/item\/(\d+)\.html/;
const match = window.location.href.match(idRegex);

if (!match || !match[1]) {
console.error("Aliexpress share userscript: Could not extract product ID from URL.");
return;
}

const productId = match[1];
const cleanLink = `https://${hostname}/item/${productId}.html`;

// --- CSS Styling ---
const css = document.createElement("style");
css.innerHTML = `
.ali-copy-link-button {
float: right;
border: 1px solid #e9e9e9;
background-color: #f7f7f7;
padding: 0 10px;
margin: 0 10px;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
font-weight: 500;
color: #666;
height: 22px;
line-height: 20px;
}
.ali-copy-link-button:hover {
background-color: #e0e0e0;
}
`;
document.head.appendChild(css);

// --- Button Creation ---
const copyButton = document.createElement("span");
copyButton.className = "ali-copy-link-button";
copyButton.textContent = buttonText;

// --- Append Button ---
targetContainer.appendChild(copyButton);

// --- Click Handler ---
copyButton.addEventListener("click", () => {
navigator.clipboard.writeText(cleanLink).then(() => {
copyButton.textContent = buttonOnCopyText;
setTimeout(() => {
copyButton.textContent = buttonText;
}, 1500);
}).catch(err => {
console.error('Aliexpress share script: Failed to copy link to clipboard.', err);
alert('Failed to copy link.');
});
});
};

// --- Script Entry Point ---
onElementReady(BUTTON_TARGET_SELECTOR, initializeButton);

})();

发布留言

登录以发布留言。