京东JD商品item信息提取

在PC端的京东商品详情页,一键复制店铺Id、店铺名称、SKUId、SKU标题、产品归属类目等信息

  1. // ==UserScript==
  2. // @name 京东JD商品item信息提取
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2.4
  5. // @description 在PC端的京东商品详情页,一键复制店铺Id、店铺名称、SKUId、SKU标题、产品归属类目等信息
  6. // @author .XX的青春 Wechat:gz08091011
  7. // @match https://item.jd.com/*.html*
  8. // @match https://item.m.jd.com/product/*.html*
  9. // @icon https://www.jd.com/favicon.ico
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_addStyle
  13. // @grant GM_registerMenuCommand
  14. // @license Copyright .XX的青春 Wechat:gz08091011
  15. // ==/UserScript==
  16. (function() {
  17. 'use strict';
  18. console.clear();
  19. const isMobile = /AndroId|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
  20. var catName = "",venderId = "",shopId = "",shopName = "",mainSkuId = "",skuIdGroup = "",spuName = "",outPutText = "",reportSkuId = "",reportSkuName = "",reportFull = "";
  21. var imageUrls = [];
  22. // 检查是否已经存在设置,如果没有则初始化
  23. let customText = GM_getValue('customText', '#');
  24. let separator = customText;
  25. // 创建弹出设置界面的函数
  26. function showSettings() {
  27. const settingsDiv = document.createElement('div');
  28. settingsDiv.style.cssText = `
  29. position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
  30. background-color: #d9eeee; border: 10px solid #ccc; padding: 20px;
  31. z-index: 9999; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  32. `;
  33. const hint = document.createElement('p');
  34. hint.textContent = '设置复制结果参数间分隔符';
  35. hint.style.cssText = 'color: #000; font-size: 14px; margin-bottom: 10px;';
  36. settingsDiv.appendChild(hint);
  37. const input = document.createElement('input');
  38. input.value = customText;
  39. input.style.cssText = `
  40. width: 200px; padding: 8px; border: 1px solid #ccc;
  41. border-radius: 5px; font-size: 14px;
  42. `;
  43. settingsDiv.appendChild(input);
  44. const dynamicHint = document.createElement('p');
  45. dynamicHint.textContent = `复制内容为:\n商家ID${separator}店铺名${separator}skuid${separator}商品名${separator}spuId${separator}所属类别`;
  46. dynamicHint.style.cssText = 'color: #000; font-size: 14px; margin-top: 10px;';
  47. settingsDiv.appendChild(dynamicHint);
  48. function updateDynamicHint() {
  49. const newSeparator = input.value;
  50. dynamicHint.textContent = `复制内容为:\n商家ID${newSeparator}店铺名${newSeparator}skuid${newSeparator}商品名${newSeparator}spuId${newSeparator}所属类别`;
  51. }
  52. input.addEventListener('input', updateDynamicHint);
  53. // 按钮样式
  54. const buttonStyle = `
  55. border: none; padding: 10px 15px; border-radius: 5px; margin: 5px; cursor: pointer;
  56. `;
  57. const saveButton = document.createElement('button');
  58. saveButton.textContent = '保存';
  59. saveButton.style.cssText = `background-color: #4CAF50; color: white; ${buttonStyle}`;
  60. saveButton.addEventListener('click', () => {
  61. customText = input.value;
  62. GM_setValue('customText', customText);
  63. separator = customText;
  64. updateDynamicHint();
  65. settingsDiv.remove();
  66. });
  67. settingsDiv.appendChild(saveButton);
  68. const closeButton = document.createElement('button');
  69. closeButton.textContent = '关闭';
  70. closeButton.style.cssText = `background-color: #f44336; color: white; ${buttonStyle}`;
  71. closeButton.addEventListener('click', () => settingsDiv.remove());
  72. settingsDiv.appendChild(closeButton);
  73. document.body.appendChild(settingsDiv);
  74. }
  75. // 注册(不可用)菜单设置项
  76. GM_registerMenuCommand('自定义复制分隔符', showSettings);
  77. if (isMobile) {
  78. try {
  79. const skuJson = window._itemInfo;
  80. shopName = skuJson.stock.D.shopName;
  81. reportSkuId = skuJson.item.skuId;
  82. reportSkuName = skuJson.skuChooseArr;
  83. spuName = skuJson.item.skuName.replace(reportSkuName, "");
  84. catName = skuJson.product.category;
  85. skuJson.item.ColorSize.forEach(item => {
  86. outPutText += `${skuJson.item.venderId}${separator}${shopName}${separator}${item.skuId}${separator}${spuName}${separator}${item.color}${separator}${skuJson.product.productId}${separator}${catName}\n`;
  87. skuIdGroup += `${item.skuId}\r\n`;
  88. });
  89. if (!skuJson.item.ColorSize.length) {
  90. outPutText += `${skuJson.item.venderId}${separator}${shopName}${separator}${skuJson.product.skuId}${separator}${skuJson.item.skuName}${separator}${skuJson.product.productId}${separator}${catName}\n`;
  91. skuIdGroup += `${skuJson.product.skuId}\r\n`;
  92. }
  93. toolBar();
  94. } catch (e) {
  95. console.log("解析商品信息失败~");
  96. }
  97. } else {
  98. shopName = $("div .name a")[0].innerText;
  99. try {
  100. const skuJson = JSON.parse(JSON.stringify(pageConfig));
  101. const skuLength = skuJson.product.colorSize.length;
  102. catName = skuJson.product.catName.join(">");
  103. shopId = skuJson.product.shopId;
  104. venderId = skuJson.product.venderId;
  105. reportSkuId = skuJson.product.skuid;
  106. reportSkuName = skuJson.product.name;
  107. mainSkuId = skuJson.product.mainSkuId;
  108. reportFull = `${venderId}${separator}${shopName}${separator}${reportSkuId}${separator}${reportSkuName}${separator}${mainSkuId}${separator}${catName}`;
  109. imageUrls = skuJson.product.imageList.map(url => 'http://img30.360buyimg.com/imgzone/' + url);
  110. if (skuLength > 0) {
  111. spuName = findAndExtractLast(skuJson.product.name, " ");
  112. skuJson.product.colorSize.forEach(item => {
  113. const color = item[Object.keys(item)[1]];
  114. outPutText += `${venderId}${separator}${shopName}${separator}${item.skuId}${separator}${spuName} ${color}${separator}${mainSkuId}${separator}${catName}\n`;
  115. skuIdGroup += `${item.skuId}\n`;
  116. });
  117. } else {
  118. outPutText += `${reportFull}\n`;
  119. skuIdGroup += `${reportSkuId}\n`;
  120. }
  121. toolBar();
  122. } catch (e) {
  123. console.log("解析商品信息失败!");
  124. }
  125. }
  126. function toolBar() {
  127. // 创建工具栏容器
  128. const toolbar = document.createElement('div');
  129. toolbar.style.cssText = 'background-color: #ecf2ff; padding: 5px; display: flex; position: relative;';
  130. function createSubMenu(buttons) {
  131. const submenu = document.createElement('div');
  132. submenu.style.cssText = 'position: absolute; top: 100%; left: 0; background-color: #ecf2ff; display: none; flex-direction: column; border: 1px solid #ccc; z-index: 9999;';
  133. buttons.forEach(({ text, id, title, action, specialColor }) => {
  134. const button = document.createElement('div');
  135. button.textContent = text;
  136. button.style.cssText = `cursor: pointer; padding: 5px 10px; background-color: #ecf2ff; color: ${specialColor || '#000'}; font-size: 12px; font-weight: bold; white-space: nowrap;`;
  137. button.id = id;
  138. button.title = title;
  139. button.addEventListener('mouseover', () => {
  140. button.style.cssText += 'border-radius: 10px; background-color: #aabbf2; color: #fff;';
  141. });
  142. button.addEventListener('mouseout', () => {
  143. button.style.cssText += `border-radius: 0; background-color: #ecf2ff; color: ${specialColor || '#000'};`;
  144. });
  145. button.addEventListener('click', () => {
  146. if (action) {
  147. if (typeof action === 'function') {
  148. action();
  149. } else {
  150. copyToClip(action);
  151. showTip("复制成功!", event.target);
  152. }
  153. } else {
  154. alert('获取信息失败,请刷新网页后重试!');
  155. }
  156. });
  157. submenu.appendChild(button);
  158. });
  159. return submenu;
  160. }
  161. function createMenuButton(text, submenu) {
  162. const menuButton = document.createElement('div');
  163. menuButton.textContent = text;
  164. menuButton.style.cssText = 'cursor: pointer; padding: 5px 10px; background-color: #ecf2ff; color: #000; font-size: 12px; font-weight: bold; position: relative;';
  165. menuButton.addEventListener('mouseover', () => submenu.style.display = 'flex');
  166. menuButton.addEventListener('mouseout', () => submenu.style.display = 'none');
  167. menuButton.appendChild(submenu);
  168. return menuButton;
  169. }
  170. const storeInfoSubMenu = createSubMenu([
  171. { text: '店铺 Id', id: 'button1', title: '复制店铺 Id', action: shopId },
  172. { text: '商家Id', id: 'button2', title: '复制商家 Id', action: venderId },
  173. { text: '店铺名', id: 'button3', title: '复制店铺名称', action: shopName }
  174. ]);
  175. const skuInfoSubMenu = createSubMenu([
  176. { text: 'SkuId', id: 'button4', title: '复制当前SkuId', action: reportSkuId },
  177. { text: '商品名', id: 'button5', title: '复制商品名称', action: reportSkuName },
  178. { text: '所有SkuId', id: 'button6', title: '复制该商品全部 SkuId', action: skuIdGroup },
  179. { text: 'Sku全信息', id: 'button8', title: '复制当前Sku完整信息', action: reportFull, specialColor: '#fa2c19' }
  180. ]);
  181. const spuInfoSubMenu = createSubMenu([
  182. { text: 'spuId', id: 'button9', title: '复制商品spuId', action: mainSkuId },
  183. { text: 'SPU 信息', id: 'button7', title: '复制该商品完整 SPU 信息', action: outPutText }
  184. ]);
  185. const mainImageSubMenu = createSubMenu([
  186. { text: '复制主图Url', id: 'downloadMainImage', title: '复制所有主图链接', action: imageUrls.join('\n') }
  187. ]);
  188. const feedbackButton = document.createElement('div');
  189. feedbackButton.textContent = '工具反馈';
  190. feedbackButton.style.cssText = 'cursor: pointer; padding: 5px 10px; background-color: #ecf2ff; color: #000; font-size: 12px; font-weight: bold;';
  191. feedbackButton.addEventListener('mouseover', () => {
  192. feedbackButton.style.cssText += 'border-radius: 10px; background-color: #aabbf2; color: #fff;';
  193. });
  194. feedbackButton.addEventListener('mouseout', () => {
  195. feedbackButton.style.cssText += 'border-radius: 0; background-color: #ecf2ff; color: #000;';
  196. });
  197. feedbackButton.addEventListener('click', () => {
  198. const feedbackDiv = document.createElement('div');
  199. feedbackDiv.style.cssText = `
  200. position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
  201. width: 310px; height: 460px; background-color: #f9f9f9; border: 2px solid #ccc; padding: 10px;
  202. display: flex; flex-direction: column; align-items: center; justify-content: center;
  203. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); z-index: 1000;
  204. `;
  205. const feedbackImage = document.createElement('img');
  206. feedbackImage.src = 'https://wwimages.s3.cn-north-1.jdcloud-oss.com/7.9kudi.jpg';
  207. feedbackImage.style.cssText = 'width: 300px; height: 380px; border-radius: 10px; ';
  208. feedbackDiv.appendChild(feedbackImage);
  209. const feedbackText = document.createElement('p');
  210. feedbackText.textContent = '反馈问题 ==> 微信:gz08091011';
  211. feedbackText.href = 'weixin://';
  212. feedbackText.style.cssText = 'color: #000; font-size: 14px; margin-top: 10px;';
  213. feedbackDiv.appendChild(feedbackText);
  214. const closeButton = document.createElement('button');
  215. closeButton.textContent = '关闭';
  216. closeButton.style.cssText = 'margin-top: 10px; padding: 5px 10px; background-color: #f44336; color: #fff; border: none; border-radius: 5px; cursor: pointer;';
  217. closeButton.addEventListener('click', () => {
  218. document.body.removeChild(feedbackDiv);
  219. });
  220. feedbackDiv.appendChild(closeButton);
  221. document.body.appendChild(feedbackDiv);
  222. });
  223. toolbar.appendChild(createMenuButton('店铺信息', storeInfoSubMenu));
  224. toolbar.appendChild(createMenuButton('Sku信息', skuInfoSubMenu));
  225. toolbar.appendChild(createMenuButton('Spu信息', spuInfoSubMenu));
  226. toolbar.appendChild(createMenuButton('主图&视频', mainImageSubMenu));
  227. toolbar.appendChild(feedbackButton);
  228. const targetDiv = document.querySelector('.sku-name');
  229. if (targetDiv) {
  230. targetDiv.insertAdjacentElement('afterend', toolbar);
  231. }
  232. }
  233. function copyToClip(message) {
  234. const content = document.createElement("textarea");
  235. content.value = message;
  236. document.body.appendChild(content);
  237. content.select();
  238. document.execCommand("copy");
  239. document.body.removeChild(content);
  240. }
  241. function showTip(text, targetElement) {
  242. // 创建提示框
  243. const tip = document.createElement('div');
  244. tip.textContent = text;
  245. tip.style.cssText = `
  246. position: absolute;
  247. padding: 5px 10px;
  248. background-color: rgba(0, 0, 0, 0.75);
  249. color: #fff;
  250. border-radius: 5px;
  251. z-index: 9999;
  252. font-size: 14px;
  253. pointer-events: none;
  254. white-space: nowrap;
  255. `;
  256. const rect = targetElement.getBoundingClientRect();
  257. tip.style.left = `${rect.right + 10}px`;
  258. tip.style.top = `${rect.top + window.scrollY}px`;
  259. document.body.appendChild(tip);
  260. setTimeout(() => {
  261. document.body.removeChild(tip);
  262. }, 1500);
  263. }
  264. function findAndExtractLast(text, search) {
  265. var index = text.lastIndexOf(search);
  266. if (index !== -1) {
  267. return text.substring(0,index);
  268. }
  269. return '';
  270. }
  271. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址