Amazon Search Result Enhancer

Enhance Amazon search results by adding numbers and advertisement IDs.

当前为 2024-05-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Amazon Search Result Enhancer
  3. // @name:zh 亚马逊搜索结果添加序号和广告ID
  4. // @name:de Amazon Suchergebnis Verbesserer
  5. // @name:ja Amazon 検索結果エンハンサー
  6. // @name:fr Amazon Amélioration des résultats de recherche
  7. // @name:it Amazon Miglioratore di risultati di ricerca
  8. // @name:nl Amazon Zoekresultaten Verbeteraar
  9. // @name:es Amazon Mejorador de Resultados de Búsqueda
  10. // @namespace https://noobbei.top
  11. // @version 1.3.1
  12. // @description Enhance Amazon search results by adding numbers and advertisement IDs.
  13. // @description:de Verbessern Sie Amazon-Suchergebnisse durch Hinzufügen von Nummern und Werbe-IDs.
  14. // @description:ja 広告IDと番号を追加してAmazon検索結果を向上させます。
  15. // @description:fr Améliorez les résultats de recherche Amazon en ajoutant des numéros et des identifiants de publicité.
  16. // @description:it Migliora i risultati di ricerca di Amazon aggiungendo numeri e ID pubblicitari.
  17. // @description:nl Verbeter Amazon zoekresultaten door nummers en advertentie-ID's toe te voegen.
  18. // @description:es Mejora los resultados de búsqueda de Amazon añadiendo números e ID de anuncios.
  19. // @description:zh 为亚马逊搜索结果页面上的广告和自然搜索结果添加序号, 并为广告结果添加广告ID和活动ID。
  20. // @author noobbei
  21. // @match https://www.amazon.com/*
  22. // @match https://www.amazon.co.uk/*
  23. // @match https://www.amazon.de/*
  24. // @match https://www.amazon.it/*
  25. // @match https://www.amazon.fr/*
  26. // @match https://www.amazon.es/*
  27. // @match https://www.amazon.se/*
  28. // @match https://www.amazon.com.mx/*
  29. // @match https://www.amazon.co.jp/*
  30. // @match https://www.amazon.ca/*
  31. // @icon https://www.amazon.com/favicon.ico
  32. // @license MIT
  33. // @grant none
  34. // ==/UserScript==
  35.  
  36.  
  37. (function () {
  38. 'use strict';
  39.  
  40. const translations = {
  41. 'zh': {'AdID': '广告ID', 'CampaignID': '活动ID'},
  42. 'en': {'AdID': 'Ad ID', 'CampaignID': 'Campaign ID'},
  43. 'de': {'AdID': 'Werbe-ID', 'CampaignID': 'Kampagnen-ID'},
  44. 'fr': {'AdID': 'ID Publicité', 'CampaignID': 'ID Campagne'},
  45. 'es': {'AdID': 'ID Anuncio', 'CampaignID': 'ID Campaña'},
  46. 'it': {'AdID': 'ID Annuncio', 'CampaignID': 'ID Campagna'},
  47. 'nl': {'AdID': 'Advertentie-ID', 'CampaignID': 'Campagne-ID'},
  48. 'ca': {'AdID': 'ID Anunci', 'CampaignID': 'ID Campanya'},
  49. 'ko': {'AdID': '광고ID', 'CampaignID': '캠페인ID'},
  50. 'ja': {'AdID': '広告ID', 'CampaignID': 'キャンペーンID'}
  51. };
  52.  
  53. // 动态检测用户的浏览器或网站语言
  54. let lang = navigator.language || navigator.userLanguage;
  55. console.log(lang);
  56. lang = lang.split('-')[0]; // 处理类似'en-US'的语言代码
  57.  
  58. let adID_k = translations[lang].AdID;
  59. let campaignID_k = translations[lang].CampaignID;
  60.  
  61. // 改进的 applyLabelsAndIds 函数
  62. const applyLabelsAndIds = () => {
  63. // 计数器重置
  64. let adCounter = 1;
  65. let searchResultCounter = 1;
  66.  
  67. // 获取所有搜索结果的元素
  68. let searchResults = document.querySelectorAll('div[data-component-type="s-search-result"], .sbv-video-single-product');
  69.  
  70. searchResults.forEach(result => {
  71. let label;
  72. let adIdElement;
  73. let campaignIdElement;
  74.  
  75. // 检查是否已添加过标签
  76. if (result.querySelector('.ad-counter-label, .search-result-counter-label')) {
  77. return; // 如果已添加标签,跳过这个元素
  78. }
  79.  
  80. // 检查是否是广告
  81. if (result.classList.contains('AdHolder') || result.classList.contains('sbv-video-single-product')) {
  82.  
  83. // 创建序号标签
  84. label = createLabel(`${adCounter}`, 'gold', '#000');
  85. label.classList.add('ad-counter-label'); // 添加自定义类以避免重复添加
  86.  
  87.  
  88. // 从data-s-safe-ajax-modal-trigger属性获取广告ID
  89. let adDataAttribute = result.querySelector('[data-s-safe-ajax-modal-trigger]');
  90. // console.log('adData');
  91. // console.log(adDataAttribute);
  92. let adId = null;
  93. let campaignId = null;
  94. if (adDataAttribute) {
  95. // 解码HTML实体,然后解析JSON
  96. const adData = JSON.parse(adDataAttribute.getAttribute('data-s-safe-ajax-modal-trigger'));
  97. let ajaxUrl = adData.ajaxUrl;
  98. adId = decodeURIComponent(ajaxUrl).match(/"adId":"([^"]*)"/)[1];
  99. campaignId = decodeURIComponent(ajaxUrl).match(/"campaignId":"([^"]*)"/)[1];
  100.  
  101. }
  102.  
  103. // 如果找到广告ID,则创建并添加一个包含广告ID的标签
  104. if (adId) {
  105. adIdElement = createIdElement(`${adID_k}: ${adId}`);
  106.  
  107. }
  108.  
  109. if (campaignId) {
  110. campaignIdElement = createIdElement(`${campaignID_k}: ${campaignId}`);
  111.  
  112. }
  113.  
  114.  
  115. // 增加广告计数器
  116. adCounter++;
  117.  
  118. } else {
  119. // 创建序号标签
  120. label = createLabel(`${searchResultCounter}`, 'green', '#FFF');
  121. label.classList.add('search-result-counter-label'); // 添加自定义类以避免重复添加
  122.  
  123. // 增加搜索结果计数器
  124. searchResultCounter++;
  125. }
  126.  
  127. // 将序号标签预置到搜索结果元素的标题下方
  128. const titleElement = result.querySelector('div[data-cy="title-recipe"]'); // 标题元素的选择器可能依网页而异
  129. if (titleElement) {
  130. let infoContainer = createInfoContainer();
  131. infoContainer.appendChild(label); // 序号标签
  132. if (adIdElement) {
  133. // 广告ID标签
  134. let adIdContainer = document.createElement('span'); // 创建一个新的容器
  135. adIdContainer.appendChild(adIdElement); // 将广告ID元素加入新容器
  136. infoContainer.appendChild(adIdContainer); // 将新容器加入信息容器
  137. }
  138.  
  139. if (campaignIdElement) {
  140. // 活动ID标签
  141. let campaignIdContainer = document.createElement('div'); // 创建一个新的容器
  142. campaignIdContainer.appendChild(campaignIdElement); // 将活动ID元素加入新容器
  143. infoContainer.appendChild(campaignIdContainer); // 将新容器加入信息容器
  144. }
  145.  
  146. // 将信息容器插入到标题元素的下一行
  147. titleElement.parentNode.insertBefore(infoContainer, titleElement.nextSibling);
  148. }
  149. });
  150. };
  151.  
  152. // 创建信息容器的函数,将标签和ID聚合在一起
  153. function createInfoContainer() {
  154. let container = document.createElement('div');
  155. container.style.display = 'block';
  156. container.style.flexDirection = 'row';
  157. container.style.alignItems = 'center';
  158. container.style.marginTop = '5px'; // 在标题下方添加一些间距
  159. return container;
  160. }
  161.  
  162. // 创建标签的函数,避免重复代码
  163. function createLabel(text, backgroundColor, foregroundColor) {
  164. let label = document.createElement('span');
  165. label.textContent = text;
  166. // 样式设置
  167. label.style.backgroundColor = backgroundColor;
  168. label.style.borderRadius = '50%';
  169. label.style.color = foregroundColor;
  170. label.style.display = 'inline-table';
  171. label.style.width = '25px';
  172. label.style.height = '25px';
  173. label.style.textAlign = 'center';
  174. label.style.marginLeft = '10px';
  175. label.style.marginRight = '5px';
  176. label.style.lineHeight = '25px';
  177. label.style.verticalAlign = 'middle';
  178. return label;
  179. }
  180.  
  181. // 创建广告id或活动id元素的函数,避免重复代码
  182. function createIdElement(text) {
  183. let idElement;
  184.  
  185. idElement = document.createElement('div');
  186. idElement.textContent = text;
  187. idElement.style.backgroundColor = '#DAA520'; // 金色背景色
  188. idElement.style.color = '#FFFFFF'; // 白色字体
  189. idElement.style.fontWeight = 'bold'; // 字体加粗
  190. idElement.style.padding = '3px 6px'; // 内边距
  191. idElement.style.marginLeft = '10px'; // 左边距
  192. idElement.style.borderRadius = '4px'; // 边框圆角
  193. idElement.style.boxShadow = '0 1px 3px rgba(0, 0, 0, 0.3)'; // 简单阴影效果
  194. idElement.style.fontSize = '0.75rem'; // 字体大小
  195. idElement.style.textAlign = 'center'; // 文本居中
  196. idElement.style.verticalAlign = 'middle'; // 垂直居中
  197. idElement.style.display = 'inline-block'; // 使用inline-block以便应用宽高、边距等
  198. idElement.style.minWidth = '80px'; // 最小宽度,保证布局的一致性
  199. idElement.style.height = '20px'; // 元素高度
  200. idElement.style.lineHeight = '20px'; // 行高与元素高度一致以垂直居中文本
  201. return idElement;
  202.  
  203.  
  204. }
  205.  
  206. // 使用MutationObserver来监视DOM的变化
  207. let observer = new MutationObserver((mutations) => {
  208. mutations.forEach((mutation) => {
  209. if (mutation.addedNodes.length) {
  210. applyLabelsAndIds();
  211. }
  212. });
  213. });
  214.  
  215. // 监视整个body的变化
  216. observer.observe(document.body, {childList: true, subtree: true});
  217.  
  218. // 初次调用
  219. applyLabelsAndIds();
  220. })();