// ==UserScript== // @name 亚马逊搜索结果添加序号和广告ID // @namespace https://noobbei.top // @version 1.1.1 // @description 为亚马逊搜索结果页面上的广告和自然搜索结果添加序号, 并为广告结果添加广告ID和活动ID // @author noobbei // @match https://www.amazon.com/* // @match https://www.amazon.co.uk/* // @match https://www.amazon.de/* // @match https://www.amazon.it/* // @match https://www.amazon.fr/* // @match https://www.amazon.es/* // @match https://www.amazon.se/* // @match https://www.amazon.com.mx/* // @match https://www.amazon.co.jp/* // @match https://www.amazon.ca/* // @icon https://www.amazon.com/favicon.ico // @license MIT // @grant none // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 封装逻辑于一个函数中 const applyLabelsAndIds = () => { // 计数器重置 let adCounter = 1; let searchResultCounter = 1; // 获取所有搜索结果的元素 let searchResults = document.querySelectorAll('div[data-component-type="s-search-result"], .sbv-video-single-product'); searchResults.forEach(result => { let label; let adIdElement; let campaignIdElement; // 检查是否已添加过标签 if(result.querySelector('.ad-counter-label, .search-result-counter-label')) { return; // 如果已添加标签,跳过这个元素 } // 检查是否是广告 if (result.classList.contains('AdHolder') || result.classList.contains('sbv-video-single-product')) { // 创建序号标签 label = createLabel(`${adCounter}`, 'gold'); label.classList.add('ad-counter-label'); // 添加自定义类以避免重复添加 // 从data-s-safe-ajax-modal-trigger属性获取广告ID let adDataAttribute = result.querySelector('[data-s-safe-ajax-modal-trigger]'); // console.log('adData'); // console.log(adDataAttribute); let adId = null; let campaignId = null; if (adDataAttribute) { // 解码HTML实体,然后解析JSON const adData = JSON.parse(adDataAttribute.getAttribute('data-s-safe-ajax-modal-trigger')); let ajaxUrl = adData.ajaxUrl; adId = decodeURIComponent(ajaxUrl).match(/"adId":"([^"]*)"/)[1]; campaignId = decodeURIComponent(ajaxUrl).match(/"campaignId":"([^"]*)"/)[1]; } // 如果找到广告ID,则创建并添加一个包含广告ID的标签 if (adId) { adIdElement = createIdElement(`广告Id: ${adId}`); } if (campaignId) { campaignIdElement = createIdElement(`活动Id: ${campaignId}`); } // 增加广告计数器 adCounter++; } else { // 创建序号标签 label = createLabel(`${searchResultCounter}`, 'green'); label.classList.add('search-result-counter-label'); // 添加自定义类以避免重复添加 // 增加搜索结果计数器 searchResultCounter++; } // 将序号标签预置到搜索结果元素的顶部 result.insertBefore(label, result.firstChild); if(adIdElement){ // 将广告ID标签插入到广告序号标签之后 result.insertBefore(adIdElement, label.nextSibling); } if(campaignIdElement){ // 将活动ID标签插入到广告序号标签之后 result.insertBefore(campaignIdElement, adIdElement.nextSibling); } }); }; // 创建标签的函数,避免重复代码 function createLabel(text, backgroundColor) { let label = document.createElement('span'); label.textContent = text; // 样式设置 label.style.backgroundColor = backgroundColor; label.style.borderRadius = '50%'; label.style.color = '#000'; label.style.display = 'inline-table'; label.style.width = '25px'; label.style.height = '25px'; label.style.textAlign = 'center'; label.style.marginLeft = '10px'; label.style.marginRight = '5px'; label.style.lineHeight = '25px'; label.style.verticalAlign = 'middle'; return label; } // 创建广告id或活动id元素的函数,避免重复代码 function createIdElement(text) { let idElement = document.createElement('span'); idElement = document.createElement('span'); idElement .textContent = text; idElement .style.backgroundColor = '#DAA520'; // 金色背景色 idElement .style.color = '#FFFFFF'; // 白色字体 idElement .style.fontWeight = 'bold'; // 字体加粗 idElement .style.padding = '3px 6px'; // 内边距 idElement .style.marginLeft = '10px'; // 左边距 idElement .style.borderRadius = '4px'; // 边框圆角 idElement .style.boxShadow = '0 1px 3px rgba(0, 0, 0, 0.3)'; // 简单阴影效果 idElement .style.fontSize = '0.75rem'; // 字体大小 idElement .style.textAlign = 'center'; // 文本居中 idElement .style.verticalAlign = 'middle'; // 垂直居中 idElement .style.display = 'inline-block'; // 使用inline-block以便应用宽高、边距等 idElement .style.minWidth = '80px'; // 最小宽度,保证布局的一致性 idElement .style.height = '20px'; // 元素高度 idElement .style.lineHeight = '20px'; // 行高与元素高度一致以垂直居中文本 return idElement; } // 使用MutationObserver来监视DOM的变化 let observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { if (mutation.addedNodes.length) { applyLabelsAndIds(); } }); }); // 监视整个body的变化 observer.observe(document.body, {childList: true, subtree: true}); // 初次调用 applyLabelsAndIds(); })();