Amazon AdId and campaignid Extractor

Extract adId and ASIN from Amazon sponsored products and display them in a table at the top of the page

  1. // ==UserScript==
  2. // @name Amazon AdId and campaignid Extractor
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @author You
  6. // @description Extract adId and ASIN from Amazon sponsored products and display them in a table at the top of the page
  7. // @author Xiafancat and Chatgpt
  8. // @match https://*.amazon.com/s*
  9. // @match https://*.amazon.co.uk/s*
  10. // @match https://*.amazon.de/s*
  11. // @match https://*.amazon.fr/s*
  12. // @match https://*.amazon.it/s*
  13. // @match https://*.amazon.es/s*
  14. // @match https://*.amazon.co.jp/s*
  15. // @grant GM_download
  16. // @license Zlib/Libpng License
  17. // ==/UserScript==
  18.  
  19. (function() {
  20. 'use strict';
  21.  
  22. // 创建一个表格来存储 adId 和 ASIN
  23. const table = document.createElement('table');
  24. table.style.position = 'sticky';
  25. table.style.top = '0';
  26. table.style.background = '#fff';
  27. table.style.zIndex = '1000';
  28. table.border = '1';
  29. const tr = document.createElement('tr');
  30. const th1 = document.createElement('th');
  31. const th2 = document.createElement('th');
  32. const th3 = document.createElement('th');
  33. const th4 = document.createElement('th');
  34.  
  35. th1.textContent = 'Title';
  36. th2.textContent = 'AdId';
  37. th3.textContent = 'ASIN';
  38. th4.textContent = 'CampaignId';
  39. tr.appendChild(th1);
  40. tr.appendChild(th2);
  41. tr.appendChild(th3);
  42. tr.appendChild(th4);
  43. table.appendChild(tr);
  44.  
  45. // 添加下载按钮
  46. const downloadBtn = document.createElement('button');
  47. downloadBtn.textContent = 'Download as CSV';
  48. downloadBtn.style.position = 'sticky';
  49. downloadBtn.style.top = '0';
  50. downloadBtn.style.marginLeft = '10px';
  51. downloadBtn.style.zIndex = '1000';
  52. downloadBtn.addEventListener('click', function() {
  53. downloadTableAsCSV(table, 'Amazon_AdId_Extractor');
  54. });
  55.  
  56. // 下载表格为CSV文件的函数
  57. function downloadTableAsCSV(table, filename) {
  58. let csvContent = '';
  59. const rows = table.querySelectorAll('tr');
  60.  
  61. rows.forEach(row => {
  62. const cols = row.querySelectorAll('td, th');
  63. cols.forEach((col, index) => {
  64. let cellText = col.textContent;
  65. if (cellText.includes('"')) {
  66. cellText = cellText.replace(/"/g, '""');
  67. }
  68. if (cellText.includes(',') || cellText.includes('"')) {
  69. cellText = '"' + cellText + '"';
  70. }
  71. csvContent += cellText;
  72. if (index < cols.length - 1) {
  73. csvContent += ',';
  74. }
  75. });
  76. csvContent += '\r\n';
  77. });
  78.  
  79. const csvBlob = new Blob([csvContent], {type: 'text/csv;charset=utf-8'});
  80. const csvURL = URL.createObjectURL(csvBlob);
  81.  
  82. GM_download({
  83. url: csvURL,
  84. name: filename + '.csv',
  85. onload: function() {
  86. URL.revokeObjectURL(csvURL);
  87. }
  88. });
  89. }
  90.  
  91. // 获取所有 sponsored product 的父元素
  92. const sponsoredProducts = document.querySelectorAll('.s-result-item[data-component-type="s-search-result"]');
  93.  
  94. // 遍历每个 sponsored product,并添加 adId 和 ASIN
  95. sponsoredProducts.forEach((product, index) => {
  96. console.log('Processing product #' + (index + 1));
  97.  
  98. const adAsinParent = product.querySelector('.a-popover-preload .a-declarative');
  99. if (adAsinParent && adAsinParent.dataset && adAsinParent.dataset.sSafeAjaxModalTrigger) {
  100. const data = adAsinParent.dataset.sSafeAjaxModalTrigger;
  101. console.log('data:', data);
  102.  
  103. try {
  104. const jsonData = JSON.parse(data);
  105. if (jsonData && jsonData.ajaxUrl) {
  106. const decodedData = decodeURIComponent(jsonData.ajaxUrl);
  107. const dataJsonString = decodedData.match(/{.+}/);
  108. if (dataJsonString) {
  109. try {
  110. const dataJsonObject = JSON.parse(dataJsonString[0]);
  111.  
  112. if (dataJsonObject && dataJsonObject.adCreativeMetaData && dataJsonObject.adCreativeMetaData.adCreativeDetails && dataJsonObject.adCreativeMetaData.adCreativeDetails.length > 0) {
  113. const adId = dataJsonObject.adCreativeMetaData.adCreativeDetails[0].adId;
  114. const asin = dataJsonObject.adCreativeMetaData.adCreativeDetails[0].asin;
  115. const campaignId = dataJsonObject.adCreativeMetaData.adCreativeDetails[0].campaignId;
  116.  
  117.  
  118. const titleElement = product.querySelector(`h2 > a[href*="${asin}"]`);
  119. if (titleElement) {
  120. const title = titleElement.textContent.trim();
  121. console.log('title:', title);
  122.  
  123. const tr = document.createElement('tr');
  124. const td1 = document.createElement('td');
  125. const td2 = document.createElement('td');
  126. const td3 = document.createElement('td');
  127. const td4 = document.createElement('td');
  128. td1.textContent = title;
  129. td2.textContent = adId;
  130. td3.textContent = asin;
  131. td4.textContent = campaignId;
  132.  
  133. tr.appendChild(td1);
  134. tr.appendChild(td2);
  135. tr.appendChild(td3);
  136. tr.appendChild(td4);
  137. table.appendChild(tr);
  138. } else {
  139. console.log('Title element not found');
  140. }
  141. } else {
  142. console.log('AdId or ASIN not found in jsonData.ajaxUrl');
  143. }
  144. } catch (error) {
  145. console.log('Error parsing JSON:', error);
  146. }
  147. } else {
  148. console.log('jsonData is not defined or jsonData.ajaxUrl is not found');
  149. }
  150. }
  151. } catch (error) {
  152. console.log('Error parsing JSON:', error);
  153. }
  154. } else {
  155. console.log('AdAsinParent not found');
  156. }
  157. });
  158.  
  159.  
  160.  
  161.  
  162. // 插入表格和下载按钮到页面中
  163. const sgColInner = document.querySelector('.sg-col-inner .s-main-slot');
  164. if (sgColInner) {
  165. sgColInner.insertBefore(table, sgColInner.firstChild);
  166. sgColInner.insertBefore(downloadBtn, table.nextSibling);
  167. }
  168. })();
  169.