sasurugaya

record the surugaya item you buy

当前为 2024-10-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name sasurugaya
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description record the surugaya item you buy
  6. // @author longzai
  7. // @match https://www.suruga-ya.jp/search*
  8. // @match https://www.suruga-ya.jp/pcmypage/action_sell_search/detail*
  9. // @match https://www.suruga-ya.jp/product/detail/*
  10. // @match https://www.suruga-ya.jp/feature/timesale/*
  11. // @icon https://www.google.com/s2/favicons?sz=64&domain=suruga-ya.jp
  12. // @grant unsafeWindow
  13. // @grant GM_xmlhttpRequest
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @grant GM_deleteValue
  17. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js
  18. // @require https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
  19. // @connect *
  20. // @license MIT
  21. // ==/UserScript==
  22.  
  23. (function() {
  24. 'use strict';
  25.  
  26.  
  27. unsafeWindow.GM_getValue=GM_getValue;
  28. unsafeWindow.GM_setValue=GM_setValue;
  29.  
  30.  
  31. var bought_items=GM_getValue('bought_items',{});
  32. //var bought_items2=new Set()
  33. var bought_items2=new Set(GM_getValue('bought_items2',[]));
  34. var bought_pages=GM_getValue('bought_pages',{});
  35. var tenpo_branch=GM_getValue('tenpo_branch',{'':'0001'});
  36. var item_data=GM_getValue('item_data',{});
  37.  
  38. unsafeWindow.bought_items=bought_items;
  39. unsafeWindow.bought_items2=bought_items2;
  40. unsafeWindow.item_data=item_data;
  41.  
  42. console.log(tenpo_branch);
  43. let div_item=$("div.item_box div.item");
  44. console.log(Object.keys(bought_items).length,bought_items2.size);
  45. console.log(div_item.length);
  46. let all_a_tag=$("a");
  47. all_a_tag.each((n,item)=>{
  48. //console.log(item,n);
  49. let h=item.getAttribute('href')
  50. if(h && h.includes('search_word=&')){
  51. item.setAttribute('href',h.replace('search_word=&','search_word=%E3%83%9E%E3%83%AA%E3%82%A2%E6%A7%98%E3%81%8C%E3%81%BF%E3%81%A6%E3%82%8B&'))
  52. }
  53. })
  54.  
  55. $("div.search_option").append(`<button onclick="{
  56. var lnk = document.createElement ('input');
  57. lnk.type='file';
  58. lnk.dispatchEvent (new MouseEvent ('click'));
  59. lnk.addEventListener('change',function(){
  60. var reader=new FileReader();
  61. reader.readAsText(lnk.files[0],'UTF-8')
  62. reader.onload=function(e){var item_list=JSON.parse(e.target.result);
  63. bought_items2=new Set()
  64. for(var item of item_list){bought_items2.add(item);}
  65. GM_setValue('bought_items2',Array.from(bought_items2));
  66. console.log(bought_items2.size);}
  67. }
  68. )
  69. }" >上传文件修改bought_items2(是否购买) </button>`);
  70.  
  71.  
  72. $("div.search_option").append(`<button onclick="{
  73. var lnk = document.createElement ('input');
  74. lnk.type='file';
  75. lnk.dispatchEvent (new MouseEvent ('click'));
  76. lnk.addEventListener('change',function(){
  77. var reader=new FileReader();
  78. reader.readAsText(lnk.files[0],'UTF-8')
  79. reader.onload=function(e){var json_data=JSON.parse(e.target.result);
  80. item_data={}
  81. for(var key in json_data){item_data[key]=json_data[key]['date_price'];}
  82. GM_setValue('item_data',item_data);
  83. }
  84. }
  85. )
  86. }">上传文件修改item_data(爬虫文件,价格) </button>`);
  87. $("div.search_option").append(`<button onclick="
  88. {
  89. var lnk = document.createElement ('input');
  90. lnk.type='file';
  91. lnk.dispatchEvent (new MouseEvent ('click'));
  92. lnk.addEventListener('change',function(){
  93. var reader=new FileReader();
  94. reader.readAsText(lnk.files[0],'UTF-8')
  95. reader.onload=function(e){var json_data=JSON.parse(e.target.result);
  96. bought_items=json_data['bought_items'];
  97. GM_setValue('bought_items',bought_items);
  98. bought_items2=Array.from(json_data['bought_items2']);
  99. GM_setValue('bought_items2',json_data['bought_items2']);
  100. item_data=json_data['item_data'];
  101. GM_setValue('item_data',item_data);
  102. }
  103. }
  104. )
  105. }">一次三个(bought_item,bought_items2,item_data) </button>`);
  106.  
  107. /*
  108. const blob = new Blob([JSON.stringify({'bought_items':bought_items,'bought_items2':Array.from(bought_items2),'item_data':item_data}, null, 2)], { type: 'application/json' });
  109.  
  110. // 创建一个链接,指向这个Blob对象
  111. const url = URL.createObjectURL(blob);
  112. const a = document.createElement('a');
  113. a.href = url;
  114. a.download = 'a.json';
  115. a.click();
  116.  
  117. // 释放URL对象
  118. URL.revokeObjectURL(url);
  119. */
  120.  
  121.  
  122.  
  123. div_item.append((n)=>{var a=div_item[n].querySelector('div.title a');
  124.  
  125. var u=new URL(a.href);
  126. var price_text='';
  127. var b=u.pathname.split('/').pop();
  128. console.log(b);
  129. if (b in bought_items){price_text+=`<div style="color:red;">surugaya_bought_value:${bought_items[b]}</div> `}
  130. if (bought_items2.has(b)){price_text+=`<div style="color:green;">outside_bought_value</div> `}
  131. if (b in item_data){
  132. for(var date in item_data[b]){
  133. price_text+=`<div style="color:blue;"> ${date}</div>`;
  134. var date_value=item_data[b][date]
  135. for(var key in date_value){
  136. price_text+=`<div style="color:black;">${key}:${date_value[key]}</div>`;
  137. }
  138. }
  139. }
  140. if(a.href.includes('/product-other/')){return price_text;
  141. }
  142. else if(a.href.includes('/product/other/')){return price_text;
  143. }
  144. else if (! a.href.includes('/product/detail/')){throw(a.href)}
  145. var tenpo_cd=u.searchParams.get('tenpo_cd') || "";
  146. //console.log(`<input type="checkbox" ${((b in bought_items) || bought_items2.has(b))?"":"checked"} class="addcartcheck" value1="${a.text}" value2="${a.href}" value3="${b}" tenpo_cd="${tenpo_cd}">`+price_text);
  147. return `<input type="checkbox" ${((b in bought_items) || bought_items2.has(b))?"":"checked"} class="addcartcheck" value1="${a.text}" value2="${a.href}" value3="${b}" tenpo_cd="${tenpo_cd}">`+price_text;
  148. }
  149. );
  150. let addcartcheck=$(".addcartcheck");
  151. let trade_code=new URLSearchParams(window.location.search).get('trade_code');
  152. //addcartcheck.each((n)=>{addcartcheck[n].checked=true;});
  153. $('div.item div p a').on('click', function(event) {
  154. if (event.shiftKey) {
  155. event.preventDefault();
  156. console.log($(this).attr('href'),$(this).attr('href').split('?')[0]);
  157. window.open($(this).attr('href').split('?')[0], '_blank');
  158. }
  159. });
  160.  
  161. let x=$('table.mgnT15 tbody tr');
  162. x.each((n)=>{if(n==0){return};var item1=x[n].children[0].textContent;x[n].children[0].innerHTML=`<a href="/product/detail/${item1}">${item1}</a>`});
  163.  
  164. if (window.location.pathname.includes('product/detail/')){
  165. $("h1.h1_title_product").append((n)=>{
  166. var price_text='';
  167. var b=window.location.pathname.split('/').pop();
  168. if (b in bought_items){price_text+=`<div style="color:red;">surugaya_bought_value:${bought_items[b]}</div> `}
  169. if (bought_items2.has(b)){price_text+=`<div style="color:green;">outside_bought_value</div> `}
  170. if (b in item_data){
  171. for(var date in item_data[b]){
  172. price_text+=`<div style="color:blue;"> ${date}</div>`;
  173. var date_value=item_data[b][date]
  174. for(var key in date_value){
  175. price_text+=`<div style="color:black;">${key}:${date_value[key]}</div>`;
  176. }
  177. }
  178. }
  179. return price_text;
  180. }
  181. )
  182. }
  183. async function kd(e){//q:添加到购物车,y:从购买页保存数据修改bought_items,i:从商品详情页设置branch_num,z:上传文件修改bought_items2(是否购买),x:取消全部选中,b:清除bought_items和bought_items2,m:上传文件修改item_data(爬虫文件,价格)
  184. let keycode=(window.event) ? e.which : e.keyCode;
  185. //console.log(keycode,String.fromCharCode(keycode).toLowerCase(),String.fromCharCode(keycode))
  186. if (e.ctrlKey && String.fromCharCode(keycode).toLowerCase()=='q')
  187. {
  188. var a1=0,a2=[],a3=[],a4=[];
  189. var p=[]
  190. addcartcheck.each((n)=>{
  191. var e=addcartcheck[n];
  192. console.log(`rem=${e.getAttribute("value3")}&tenpo_cd=${e.getAttribute("tenpo_cd")}&amount=1`);
  193. let tenpo_cd=e.getAttribute("tenpo_cd");
  194. if(! (tenpo_cd in tenpo_branch)){
  195. a2.push(['未添加店铺',e.getAttribute("tenpo_cd"),e.getAttribute("value3")]);
  196. return;
  197. }
  198. if (e.checked){
  199. p.push( new Promise((resolve, reject) => {GM_xmlhttpRequest({
  200. method: "POST",
  201. url: "https://www.suruga-ya.jp/cargo/ajax_add",
  202. data: `rem=${e.getAttribute("value3")}&branch_number=${tenpo_branch[tenpo_cd]}&tenpo_cd=${tenpo_cd}&amount=1`,
  203. headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8","X-Requested-With":"XMLHttpRequest"},
  204. onload: function (response) {
  205. var s=JSON.parse(response.responseText);
  206. if(s.status=='1') {a1+=1;e.checked=false;}
  207. else{a2.push([e.getAttribute("value1"),e.getAttribute("value2"),s.data]);}
  208. resolve();
  209. },
  210. onerror: function (){
  211. a3.push([e.getAttribute("value1"),e.getAttribute("value2")]);
  212. resolve();
  213. }
  214. })
  215. }
  216. )
  217. );
  218. }
  219. else{
  220. a4.push([e.getAttribute("value1"),e.getAttribute("value2")]);
  221. }
  222. }
  223. )
  224. await Promise.all(p);
  225. console.log(`未选中${a4.length}个商品,选中${a1+a2.length+a3.length}个商品,其中成功添加${a1}个,添加失败${a2.length}个,未成功处理${a3.length}个`)
  226. console.log("添加失败的有:")
  227. a2.forEach((value)=>{console.log(value);});
  228. console.log("未成功处理的有:")
  229. a3.forEach((value)=>{console.log(value);});
  230. console.log("未选中的有:")
  231. a4.forEach((value)=>{console.log(value);});
  232. }
  233. else if (e.ctrlKey && String.fromCharCode(keycode).toLowerCase()=='y')
  234. {
  235. if(trade_code){bought_pages[trade_code]='y'};
  236. let x=$('table.mgnT15 tbody tr');
  237. if(x[0].children[0].textContent!='品番' || x[0].children[5].textContent!='数量' || x[0].children[8].textContent!='備考'){throw(x[0])}
  238. console.log(x.length-1,Object.keys(bought_items).length);
  239. var count1=0
  240. var count2=0
  241. var count3=0
  242. var count4=0
  243. var count5=0
  244. var p=[]
  245. x.each((n)=>{if(n==0) {return};
  246. if(x[n].children.length!=9){return}
  247. var item1=x[n].children[0].textContent;
  248. p.push( new Promise((resolve, reject) => {GM_xmlhttpRequest({
  249. method: "GET",
  250. url: "https://www.suruga-ya.jp/product/detail/"+item1,
  251. onload: function (response) {
  252. var item=response.finalUrl
  253. if(item1!=item && item1 in bought_items){delete bought_items[item]}
  254. var u=new URL(response.finalUrl);
  255. item=u.pathname.split('/').pop();
  256. if(item1!=item && item1 in bought_items){delete bought_items[item1]}
  257. if(x[n].children[5].textContent==0)
  258. {
  259. count1+=1
  260. if(x[n].children[8].textContent.includes('キャンセル') || x[n].children[8].textContent.includes('在庫なし'))
  261. {
  262. count2+=1
  263. if (item in bought_items && bought_items[item]!=null){count3+=1;delete bought_items[item]}
  264. }
  265. else{
  266. throw(x[n],x[n].children[8].textContent);
  267. }
  268. }
  269. else{
  270. count4+=1
  271. if (!(item in bought_items)){count5+=1;bought_items[item]=x[n].children[4].textContent}
  272. }
  273. resolve();
  274. },
  275. onerror: function (){
  276. console.log("reject: ",item1)
  277. reject();
  278. }
  279. })
  280. }
  281. )
  282. );
  283. }
  284. );
  285. await Promise.all(p);
  286. console.log(count1,count2,count3,count4,count5)
  287. console.log(Object.keys(bought_items).length);
  288. GM_setValue('bought_items',bought_items);
  289. console.log(bought_items)
  290. }
  291. else if (e.ctrlKey && String.fromCharCode(keycode).toLowerCase()=='i'){
  292. if(window.location.pathname.includes('/product/detail/')){
  293. let tenpo_cd=$('input[name="tenpo_cd"]').attr('value');
  294. let branch_num=$('input[name="branch_number"]').attr('value');
  295. tenpo_branch[tenpo_cd]=branch_num;
  296. console.log(tenpo_branch);
  297. GM_setValue('tenpo_branch',tenpo_branch);
  298. }
  299. }
  300. /*
  301. else if (e.ctrlKey && String.fromCharCode(keycode).toLowerCase()=='z')
  302. {
  303. console.log('awegfaw');
  304. var lnk = document.createElement ('input');
  305. lnk.type="file"
  306. lnk.dispatchEvent (new MouseEvent ("click"));
  307. lnk.addEventListener('change',function(){
  308. console.log(lnk.files[0].name)
  309. var reader=new FileReader()
  310. reader.readAsText(lnk.files[0],'UTF-8')
  311. reader.onload=function(e){var item_list=JSON.parse(e.target.result);
  312. bought_items2=new Set()
  313. for(var item of item_list){bought_items2.add(item);}
  314. GM_setValue('bought_items2',Array.from(bought_items2));
  315. console.log(bought_items2.size);}
  316. }
  317. )
  318.  
  319. }
  320. */
  321. else if (e.ctrlKey && String.fromCharCode(keycode).toLowerCase()=='x')
  322. {
  323. addcartcheck.each((n)=>{addcartcheck[n].checked=false;})
  324. }
  325. else if (e.ctrlKey && String.fromCharCode(keycode).toLowerCase()=='b')
  326. {
  327. bought_items={};
  328. GM_setValue('bought_items',bought_items);
  329. bought_items2=new Set();
  330. GM_setValue('bought_items2',Array.from(bought_items2));
  331. console.log('clear success');
  332. }
  333. /*
  334. else if (e.ctrlKey && String.fromCharCode(keycode).toLowerCase()=='m')
  335. {
  336. console.log('awegfaw');
  337. var lnk = document.createElement ('input');
  338. lnk.type="file"
  339. lnk.dispatchEvent (new MouseEvent ("click"));
  340. lnk.addEventListener('change',function(){
  341. console.log(lnk.files[0].name)
  342. var reader=new FileReader()
  343. reader.readAsText(lnk.files[0],'UTF-8')
  344. reader.onload=function(e){var json_data=JSON.parse(e.target.result);
  345. item_data={}
  346. for(var key in json_data){item_data[key]=json_data[key]['date_price'];}
  347. GM_setValue('item_data',item_data);
  348. }
  349. }
  350. )
  351. }
  352. */
  353. else if (e.ctrlKey && String.fromCharCode(keycode).toLowerCase()=='¿')
  354. {
  355. alert("ctrl+q:添加到购物车,y:从购买页保存数据修改bought_items,i:从商品详情页设置branch_num,z:上传文件修改bought_items2(是否购买),x:取消全部选中,b:清除bought_items和bought_items2,m:上传文件修改item_data(爬虫文件,价格)");
  356. }
  357. }
  358. document.addEventListener("keydown", kd);
  359. console.log("abc");
  360. })();