Boxz Price History Image

在网上商店产品页面,自动插入一张从 www.boxz.com 里的价格历史波动图片。

  1. // ==UserScript==
  2. // @name Boxz Price History Image
  3. // @namespace qixinglu.com
  4. // @description 在网上商店产品页面,自动插入一张从 www.boxz.com 里的价格历史波动图片。
  5. // @grant GM_xmlhttpRequest
  6. // @include http://mvd.jd.com/*.html
  7. // @include http://book.jd.com/*.html
  8. // @include http://item.jd.com/*.html
  9. // @include http://www.newegg.com.cn/product/*
  10. // @include http://www.newegg.com.cn/Product/*
  11. // @include http://www.amazon.cn/gp/product/*
  12. // @include http://www.amazon.cn/*/dp/*
  13. // @include http://www.amazon.cn/mn/detailApp*
  14. // @include http://product.dangdang.com/Product.aspx?product_id=*
  15. // @include http://product.dangdang.com/product.aspx?product_id=*
  16. // @include http://item.yixun.com/item-*
  17. // @include http://www.suning.com/emall/prd_10052_10051_-7_*.html*
  18. // @include http://www.suning.com/emall/snupgbpv_10052_10051_*_.html
  19. // @include http://www.suning.com/emall/sngbv_10052_10051_*_.html
  20. // @include http://www.gome.com.cn/ec/homeus/jump/product/*.html*
  21. // @include http://www.lusen.com/Product/ProductInfo.aspx?Id=*
  22. // @include http://www.efeihu.com/Product/*.html*
  23. // @include http://www.tao3c.com/product/*
  24. // @include http://www.coo8.com/product/*.html*
  25. // @include http://www.yihaodian.com/item/*
  26. // @include http://www.1mall.com/item/*
  27. // @include http://www.ouku.com/goods*
  28. // @include http://www.redbaby.com.cn/*/*.html*
  29. // @include http://cn.strawberrynet.com/a/b/c/*/
  30. // @include http://web1.sasa.com/SasaWeb/sch/product/viewProductDetail.jspa?itemno=*
  31. // @include http://www.bookschina.com/*.htm
  32. // @include http://www.wl.cn/*
  33. // @include http://product.china-pub.com/*
  34. // @include http://www.winxuan.com/product/*
  35. // @include http://www.99read.com/product/*
  36. // @include http://www.99read.com/Product/*
  37. // @include http://www.new7.com/product/*
  38. // @include http://detail.bookuu.com/*.html
  39. // @version 0.0.1.20140517140354
  40. // ==/UserScript==
  41.  
  42. // 图书类还是 Google Chart
  43. function google_chart(response) {
  44. var temp_document, img_node, img_node_src, image_node;
  45. temp_document = document.createElement('html');
  46. temp_document.innerHTML = response.responseText;
  47. img_node = temp_document.querySelector('div.fNumber img');
  48. if (img_node === null) {
  49. image_node = document.createElement('p');
  50. image_node.innerHTML = '这个产品貌似没有历史价格数据,<a href="' + response.finalUrl + '">查看链接</a>。';
  51. } else {
  52. // 修改样式
  53. img_node_src = img_node.src.replace('chs=630x180', 'chs=720x240');
  54. img_node_src = img_node_src.replace('chts=FF0000%2c13', 'chts=FF0000%2c14');
  55. img_node_src = img_node_src + '&chdls=,14';
  56. img_node.src = img_node_src;
  57. img_node.width = 720;
  58. img_node.height = 240;
  59. img_node.style.marginTop = '10px';
  60. img_node.style.marginBottom = '10px';
  61. // 加上链接
  62. image_node = document.createElement('a');
  63. image_node.href = response.finalUrl;
  64. image_node.appendChild(img_node);
  65. }
  66. return image_node;
  67. }
  68.  
  69. // 非图书类是自托管图片
  70. function self_host(urls) {
  71. var img_node = document.createElement('img');
  72. var detail_url = urls[0];
  73. var chart_url = urls[1];
  74. img_node.src = chart_url;
  75. img_node.width = 630;
  76. img_node.height = 180;
  77. img_node.alt = '这个产品貌似没有历史价格数据,查看链接。';
  78. img_node.style.marginTop = '10px';
  79. img_node.style.marginBottom = '10px';
  80. // 加上链接
  81. var image_node = document.createElement('a');
  82. image_node.href = detail_url;
  83. image_node.appendChild(img_node);
  84. return image_node;
  85. }
  86.  
  87. // 获得价格历史图片
  88. function create_history_image_node(response) {
  89. var image_node;
  90. if (response.responseText === undefined) {
  91. image_node = self_host(response);
  92. }
  93. else {
  94. image_node = google_chart(response);
  95. }
  96. return image_node;
  97. }
  98.  
  99. function create_product_history_url(prefix, product_uid) {
  100. var detail_url = 'http://www.boxz.com/products/' + prefix + '-' + product_uid + '.shtml';
  101. var chart_url = 'http://www.boxz.com/pic/small/' + prefix + '-' + product_uid + '.png';
  102. return [detail_url, chart_url];
  103. }
  104.  
  105. function create_book_history_url(prefix, product_uid) {
  106. var detail_url = 'http://www.boxz.com/books/' + prefix + '-' + product_uid + '.shtml';
  107. var chart_url = null;
  108. return [detail_url, chart_url];
  109. }
  110.  
  111. function insertAfter(image_node, place_node) {
  112. var parentNode = place_node.parentNode;
  113. if (place_node.nextElementSibling) {
  114. parentNode.insertBefore(image_node, place_node.nextElementSibling);
  115. } else {
  116. parentNode.appendChild(image_node);
  117. }
  118. }
  119.  
  120. var url = window.location.href;
  121.  
  122. /* 网店处理规则 */
  123. var sites = [{
  124. domain : 'jd.com',
  125. get_history_url: function() {
  126. var reg, product_uid, history_url;
  127. if (url.indexOf('http://item.jd.com/') !== -1) {
  128. reg = new RegExp('http://item.jd.com/(\\d+).html');
  129. product_uid = url.match(reg)[1];
  130. history_url = create_product_history_url('360buy', product_uid);
  131. } else {
  132. reg = new RegExp('http://.+\.jd\.com/(\\d+).html');
  133. product_uid = url.match(reg)[1];
  134. history_url = create_book_history_url('360buy', product_uid);
  135. }
  136. return history_url;
  137. },
  138. request_callback: function(response) {
  139. var image_node, place_node;
  140. image_node = create_history_image_node(response);
  141. place_node = document.querySelector('#choose');
  142. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  143. }
  144. }, {
  145. domain : 'newegg.com',
  146. get_history_url: function() {
  147. var reg, product_uid, history_url;
  148. reg = new RegExp('http://www.newegg.com.cn/[Pp]roduct/([^.]+).htm');
  149. product_uid = url.match(reg)[1];
  150. history_url = create_product_history_url('newegg', product_uid);
  151. return history_url;
  152. },
  153. request_callback: function(response) {
  154. var image_node, place_node;
  155. image_node = create_history_image_node(response);
  156. place_node = document.querySelector('.mainInfoArea');
  157. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  158. }
  159. }, {
  160. domain : 'amazon.cn',
  161. get_history_url: function() {
  162. var reg, product_uid, history_url;
  163. if (url.indexOf('/gp/product/') !== -1) {
  164. reg = new RegExp('http://www.amazon.cn/gp/product/([^/]+)/\?');
  165. } else if (url.indexOf('/dp/') !== -1) {
  166. reg = new RegExp('http://www.amazon.cn/[^/]+/dp/([^/]+)/\?');
  167. } else {
  168. reg = new RegExp('http://www.amazon.cn/mn/detailApp.*asin=(\\w+)');
  169. }
  170. product_uid = url.match(reg)[1].toLowerCase();
  171. var category = document.querySelector('.nav-subnav-item.nav-category-button').textContent.trim();
  172. if (category === '图书') {
  173. history_url = create_book_history_url('amazon', product_uid);
  174. } else {
  175. history_url = create_product_history_url('amazon', product_uid);
  176. }
  177. return history_url;
  178. },
  179. request_callback: function(response) {
  180. var image_node, place_node;
  181. image_node = create_history_image_node(response);
  182. place_node = document.querySelector('#handleBuy');
  183. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  184. }
  185. }, {
  186. domain : 'dangdang.com',
  187. get_history_url: function() {
  188. var reg, category, product_uid, history_url;
  189. reg = new RegExp('http://product.dangdang.com/[pP]roduct.aspx\\?product_id=(\\d+)');
  190. product_uid = url.match(reg)[1];
  191. category = document.querySelector('.nav_top li.on a').textContent;
  192. if (category === '图书' || category === '音像') {
  193. history_url = create_book_history_url('dangdang', product_uid);
  194. } else {
  195. history_url = create_product_history_url('dangdang', product_uid);
  196. }
  197. return history_url;
  198. },
  199. request_callback: function(response) {
  200. var image_node, place_node;
  201. image_node = create_history_image_node(response);
  202. place_node = document.querySelector('.show_info');
  203. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  204. }
  205. }, {
  206. domain : 'yixun.com',
  207. get_history_url: function() {
  208. var reg, product_uid, history_url;
  209. reg = new RegExp('http://item.yixun.com/item-([^.]+).html');
  210. product_uid = url.match(reg)[1];
  211. history_url = create_product_history_url('icson', product_uid);
  212. return history_url;
  213. },
  214. request_callback: function(response) {
  215. var image_node, place_node;
  216. image_node = create_history_image_node(response);
  217. place_node = document.querySelector('.xbase_row3');
  218. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  219. }
  220. }, {
  221. domain : 'suning.com',
  222. get_history_url: function() {
  223. var reg, mess, product_uid, history_url;
  224. // 真恶心的url设计
  225. reg = new RegExp('http://www.suning.com/emall/(.+?).html');
  226. mess = url.match(reg)[1].split('_');
  227. if (mess[0] === 'prd') {
  228. product_uid = mess[4];
  229. }
  230. else {
  231. product_uid = mess[3];
  232. }
  233. history_url = create_product_history_url('suning', product_uid);
  234. return history_url;
  235. },
  236. request_callback: function(response) {
  237. var image_node, place_node;
  238. image_node = create_history_image_node(response);
  239. // 真混乱
  240. place_node = document.querySelector('.product_view, .groupViewContent, .show');
  241. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  242. }
  243. }, {
  244. domain : 'gome.com',
  245. get_history_url: function() {
  246. var product_uid, history_url;
  247. product_uid = document.querySelector('#hideSkuid').value;
  248. history_url = create_product_history_url('gome', product_uid);
  249. return history_url;
  250. },
  251. request_callback: function(response) {
  252. var image_node, place_node;
  253. image_node = create_history_image_node(response);
  254. place_node = document.querySelector('#choose');
  255. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  256. }
  257. }, {
  258. domain : 'lusen.com',
  259. get_history_url: function() {
  260. var reg, product_uid, history_url;
  261. reg = new RegExp('http://www.lusen.com/Product/ProductInfo.aspx\\?Id=(\\d+)');
  262. product_uid = url.match(reg)[1];
  263. history_url = create_product_history_url('lusen', product_uid);
  264. return history_url;
  265. },
  266. request_callback: function(response) {
  267. var image_node, place_node;
  268. image_node = create_history_image_node(response);
  269. place_node = document.querySelector('.goodsBox .right');
  270. insertAfter(image_node, place_node);
  271. }
  272. }, {
  273. domain : 'efeihu.com',
  274. get_history_url: function() {
  275. var reg, product_uid, history_url;
  276. reg = new RegExp('http://www.efeihu.com/Product/(\\d+?)\.html');
  277. product_uid = url.match(reg)[1];
  278. history_url = create_product_history_url('efeihu', product_uid);
  279. return history_url;
  280. },
  281. request_callback: function(response) {
  282. var image_node, place_node;
  283. image_node = create_history_image_node(response);
  284. place_node = document.querySelector('.vi_choose');
  285. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  286. }
  287. }, {
  288. domain : 'tao3c.com',
  289. get_history_url: function() {
  290. var reg, product_uid, history_url;
  291. reg = new RegExp('http://www.tao3c.com/product/(\\d+).html');
  292. product_uid = url.match(reg)[1];
  293. history_url = create_product_history_url('tao3c', product_uid);
  294. return history_url;
  295. },
  296. request_callback: function(response) {
  297. var image_node, place_node;
  298. image_node = create_history_image_node(response);
  299. place_node = document.querySelector('.detail_info_rm3');
  300. insertAfter(image_node, place_node);
  301. }
  302. }, {
  303. domain : 'coo8.com',
  304. get_history_url: function() {
  305. var reg, product_uid, history_url;
  306. reg = new RegExp('http://www.coo8.com/product/(\\d\+)\.html');
  307. product_uid = url.match(reg)[1];
  308. history_url = create_product_history_url('coo8', product_uid);
  309. return history_url;
  310. },
  311. request_callback: function(response) {
  312. var image_node, place_node;
  313. image_node = create_history_image_node(response);
  314. place_node = document.querySelector('ul[class="c8-ulbox"]');
  315. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  316. }
  317. }, {
  318. domain : 'yihaodian.com',
  319. get_history_url: function() {
  320. var product_uid, history_url;
  321. product_uid = document.querySelector('#mainProductId').value;
  322. history_url = create_product_history_url('yihaodian', product_uid);
  323. return history_url;
  324. },
  325. request_callback: function(response) {
  326. var image_node, place_node;
  327. image_node = create_history_image_node(response);
  328. place_node = document.querySelector('.produce');
  329. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  330. }
  331. }, {
  332. // 又是照抄上面
  333. domain : '1mall.com',
  334. get_history_url: function() {
  335. var product_uid, history_url;
  336. product_uid = document.querySelector('#mainProductId').value;
  337. history_url = create_product_history_url('yihaodian', product_uid);
  338. return history_url;
  339. },
  340. request_callback: function(response) {
  341. var image_node, place_node;
  342. image_node = create_history_image_node(response);
  343. place_node = document.querySelector('.produce');
  344. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  345. }
  346. }, {
  347. domain : 'ouku.com',
  348. get_history_url: function() {
  349. var reg, product_uid, history_url;
  350. reg = new RegExp('http://www.ouku.com/goods(\\d+)');
  351. product_uid = url.match(reg)[1];
  352. history_url = create_product_history_url('ouku', product_uid);
  353. return history_url;
  354. },
  355. request_callback: function(response) {
  356. var image_node, place_node;
  357. image_node = create_history_image_node(response);
  358. place_node = document.querySelector('.celldetail_contright_xinde1');
  359. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  360. }
  361. }, {
  362. domain : 'redbaby.com',
  363. get_history_url: function() {
  364. var reg, product_uid, history_url;
  365. reg = new RegExp('http://www.redbaby.com.cn/\\w\+/\\d\{7}(\\d+?)\.html');
  366. product_uid = url.match(reg)[1];
  367. history_url = create_product_history_url('redbaby', product_uid);
  368. return history_url;
  369. },
  370. request_callback: function(response) {
  371. var image_node, place_node;
  372. image_node = create_history_image_node(response);
  373. place_node = document.querySelector('.productRightBase');
  374. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  375. }
  376. }, {
  377. domain : 'cn.strawberrynet.com',
  378. get_history_url: function() {
  379. var reg, product_uid, history_url;
  380. reg = new RegExp('http://cn.strawberrynet.com/a/b/c/(\\d+?)/');
  381. product_uid = url.match(reg)[1];
  382. history_url = create_product_history_url('strawberry', product_uid);
  383. return history_url;
  384. },
  385. request_callback: function(response) {
  386. var image_node, place_node;
  387. image_node = create_history_image_node(response);
  388. place_node = document.querySelector('.white_bg.product .fright');
  389. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  390. }
  391. }, {
  392. domain : 'sasa.com',
  393. get_history_url: function() {
  394. var reg, product_uid, history_url;
  395. reg = new RegExp('http://web1.sasa.com/SasaWeb/sch/product/viewProductDetail.jspa\\?itemno=(\\d+)');
  396. product_uid = url.match(reg)[1];
  397. history_url = create_product_history_url('sasa', product_uid);
  398. return history_url;
  399. },
  400. request_callback: function(response) {
  401. var image_node, place_node;
  402. image_node = create_history_image_node(response);
  403. place_node = document.querySelector('table[itemtype]');
  404. insertAfter(image_node, place_node);
  405. }
  406. }, {
  407. domain : 'bookschina.com',
  408. get_history_url: function() {
  409. var reg, product_uid, history_url;
  410. reg = new RegExp('http://www.bookschina.com/(\\d+).htm');
  411. product_uid = url.match(reg)[1];
  412. history_url = create_book_history_url('bookschina', product_uid);
  413. return history_url;
  414. },
  415. request_callback: function(response) {
  416. var image_node, place_node;
  417. image_node = create_history_image_node(response);
  418. place_node = document.querySelectorAll('.float98')[1];
  419. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  420. }
  421. }, {
  422. domain : 'wl.cn',
  423. get_history_url: function() {
  424. var reg, product_uid, history_url;
  425. reg = new RegExp('http://www.wl.cn/(\\d+)/?');
  426. product_uid = url.match(reg)[1];
  427. history_url = create_book_history_url('wl', product_uid);
  428. return history_url;
  429. },
  430. request_callback: function(response) {
  431. var image_node, place_node;
  432. image_node = create_history_image_node(response);
  433. place_node = document.querySelector('.pro.layout.blankbtm');
  434. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  435. }
  436. }, {
  437. domain : 'china-pub.com',
  438. get_history_url: function() {
  439. var reg, product_uid, history_url;
  440. reg = new RegExp('http://product.china-pub.com/(\\d+)');
  441. product_uid = url.match(reg)[1];
  442. history_url = create_book_history_url('chinapub', product_uid);
  443. return history_url;
  444. },
  445. request_callback: function(response) {
  446. var image_node, place_node;
  447. image_node = create_history_image_node(response);
  448. place_node = document.querySelector('.buybook');
  449. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  450. }
  451. }, {
  452. domain : 'winxuan.com',
  453. get_history_url: function() {
  454. var reg, product_uid, history_url;
  455. reg = new RegExp('http://www.winxuan.com/product/(\\d+)');
  456. product_uid = url.match(reg)[1];
  457. history_url = create_book_history_url('wenxuan', product_uid);
  458. return history_url;
  459. },
  460. request_callback: function(response) {
  461. var image_node, place_node;
  462. image_node = create_history_image_node(response);
  463. place_node = document.querySelector('.goods_info');
  464. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  465. }
  466. }, {
  467. domain : '99read.com',
  468. get_history_url: function() {
  469. var reg, product_uid, history_url;
  470. reg = new RegExp('http://www.99read.com/[pP]roduct/(\\d+).aspx');
  471. product_uid = url.match(reg)[1];
  472. history_url = create_book_history_url('99read', product_uid);
  473. return history_url;
  474. },
  475. request_callback: function(response) {
  476. var image_node, place_node;
  477. image_node = create_history_image_node(response);
  478. place_node = document.querySelectorAll('.NeiRongA-box')[1];
  479. place_node.parentNode.insertBefore(image_node, place_node.previousElementSibling);
  480. }
  481. }, {
  482. domain : 'new7.com',
  483. get_history_url: function() {
  484. var reg, product_uid, history_url;
  485. reg = new RegExp('http://www.new7.com/product/(\\d+).html');
  486. product_uid = url.match(reg)[1];
  487. history_url = create_product_history_url('all3c', product_uid);
  488. return history_url;
  489. },
  490. request_callback: function(response) {
  491. var image_node, place_node;
  492. image_node = create_history_image_node(response);
  493. place_node = document.querySelector('.buy');
  494. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  495. }
  496. }, {
  497. domain : 'bookuu.com',
  498. get_history_url: function() {
  499. var reg, product_uid, history_url;
  500. reg = new RegExp('http://detail.bookuu.com/(\\d\+)\.html');
  501. product_uid = url.match(reg)[1];
  502. history_url = create_book_history_url('bookuu', product_uid);
  503. return history_url;
  504. },
  505. request_callback: function(response) {
  506. var image_node, place_node;
  507. image_node = create_history_image_node(response);
  508. place_node = document.querySelector('#rightcontent .desc');
  509. place_node.parentNode.insertBefore(image_node, place_node.nextElementSibling);
  510. }
  511. }];
  512.  
  513. function start_request(site) {
  514. var urls = site.get_history_url();
  515. if (urls[1] === null) {
  516. // 图书类
  517. GM_xmlhttpRequest({
  518. method: 'GET',
  519. url: urls[0],
  520. onload: site.request_callback
  521. });
  522. }
  523. else {
  524. // 非图书类
  525. site.request_callback(urls);
  526. }
  527. }
  528.  
  529. /* 开始处理 */
  530. var i, site;
  531. for (i = 0; i < sites.length; i += 1) {
  532. if (url.indexOf(sites[i].domain) !== -1) {
  533. site = sites[i];
  534. break;
  535. }
  536. }
  537. start_request(site);