Greasy Fork

Greasy Fork is available in English.

亚马逊搜索结果添加序号和广告ID

为亚马逊搜索结果页面上的广告和自然搜索结果添加序号, 并为广告结果添加广告ID

目前为 2024-05-10 提交的版本。查看 最新版本

// ==UserScript==
// @name         亚马逊搜索结果添加序号和广告ID
// @namespace    https://noobbei.top
// @version      0.1
// @description  为亚马逊搜索结果页面上的广告和自然搜索结果添加序号, 并为广告结果添加广告ID
// @author       noobbei
// @match        https://www.amazon.com/s?k=*
// @match        https://www.amazon.co.uk/s?k=*
// @match        https://www.amazon.de/s?k=*
// @match        https://www.amazon.it/s?k=*
// @match        https://www.amazon.fr/s?k=*
// @match        https://www.amazon.es/s?k=*
// @match        https://www.amazon.se/s?k=*
// @match        https://www.amazon.com.mx/s?k=*
// @match        https://www.amazon.co.jp/s?k=*
// @match        https://www.amazon.ca/s?k=*
// @icon         https://www.amazon.com/favicon.ico
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 计数器
    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;

        // 检查是否是广告
        if (result.classList.contains('AdHolder') || result.classList.contains('sbv-video-single-product')) {

            // 创建序号标签
            label = document.createElement('span');
            label.textContent = `${adCounter}`;
            
            label.style.backgroundColor = 'gold';
            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';

            // 从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;
            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];

            }

            // 如果找到广告ID,则创建并添加一个包含广告ID的标签
            if (adId) {
                adIdElement = document.createElement('span');
                adIdElement.textContent = `广告ID: ${adId}`;
                adIdElement.style.backgroundColor = '#DAA520'; // 金色背景色
                adIdElement.style.color = '#FFFFFF'; // 白色字体
                adIdElement.style.fontWeight = 'bold'; // 字体加粗
                adIdElement.style.padding = '3px 6px'; // 内边距
                adIdElement.style.marginLeft = '10px'; // 左边距
                adIdElement.style.borderRadius = '4px'; // 边框圆角
                adIdElement.style.boxShadow = '0 1px 3px rgba(0, 0, 0, 0.3)'; // 简单阴影效果
                adIdElement.style.fontSize = '0.75rem'; // 字体大小
                adIdElement.style.textAlign = 'center'; // 文本居中
                adIdElement.style.verticalAlign = 'middle'; // 垂直居中
                adIdElement.style.display = 'inline-block'; // 使用inline-block以便应用宽高、边距等
                adIdElement.style.minWidth = '80px'; // 最小宽度,保证布局的一致性
                adIdElement.style.height = '20px'; // 元素高度
                adIdElement.style.lineHeight = '20px'; // 行高与元素高度一致以垂直居中文本

            }
            // 增加广告计数器
            adCounter++;

        } else {
            // 创建序号标签
            label = document.createElement('div');
            label.textContent = `${searchResultCounter}`;

            label.style.backgroundColor = 'green';
            label.style.borderRadius = '50%';
            label.style.color = 'white';

            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';

            // 增加搜索结果计数器
            searchResultCounter++;
        }

        // 将序号标签预置到搜索结果元素的顶部
        result.insertBefore(label, result.firstChild);
        if(adIdElement){
            // 将广告ID标签插入到广告序号标签之后
            result.insertBefore(adIdElement, label.nextSibling);
        }
    });
})();