京东优惠券抢购

京东优惠券刷新测试

  1. // ==UserScript==
  2. // @name 京东优惠券抢购
  3. // @author rjw
  4. // @description 京东优惠券刷新测试
  5. // @namespace com.uestc.rjw
  6. // @icon https://raw.githubusercontent.com/babyrjw/StaticFiles/master/logo_jd.jpg
  7. // @license Apache Licence V2
  8. // @encoding utf-8
  9. // @date 18/06/2017
  10. // @modified 18/06/2017
  11. // @noframes
  12. // @match *://sale.jd.com/act/*
  13. // @require http://code.jquery.com/jquery-2.1.1.min.js
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @grant GM_setClipboard
  17. // @grant GM_xmlhttpRequest
  18. // @grant unsafeWindow
  19. // @run-at document-idle
  20. // @connect jd.com
  21. // @version 1.0.6
  22. // ==/UserScript==
  23.  
  24.  
  25. /*
  26. * === 说明 ===
  27. *@作者:rjw
  28. *@Email:babyrjw@163.com
  29. * */
  30. /*
  31. *优惠券一般是<a>标签内部嵌套一个<img>图片,<a>标签的href是领取优惠券的链接
  32. *
  33. * */
  34. (function add_start(){
  35. function init_page(){
  36. console.log('handle all coupon link :'+unsafeWindow.location);
  37. //移除优惠券已抢完和倒计时的状态
  38. var all_coupon_state = $('a.coup-state');
  39. all_coupon_state.each(function(index, e){
  40. $(this).remove();
  41. });
  42. var all_a = $("a[href*='coupon.jd.com/ilink/']");
  43. console.log('all_a:'+all_a.length);
  44. all_a.each(function(index, e){
  45. //清理<a>标签的 href, target属性
  46. var item = $(this);
  47. var url = item.attr('href');
  48. item.attr('data-url',url);
  49. item.removeAttr('href');
  50. item.removeAttr('target');
  51. var children = $(this).children();
  52. //添加按钮
  53. var item_id = "retrive_coupon_msg"+index;
  54. var item_btn = "retrive_coupon_txt"+index;
  55. var ele_btn;
  56. if(children.length > 0){
  57. //计算加入按钮后<a>标签的高度
  58. //var height = 0;
  59. //for(var i = 0 ; i < children.length; ++i){
  60. // console.log(children[i]+" "+i+" "+children[i].offsetHeight);
  61. // height += children[i].offsetHeight;
  62. //}
  63. //height += 58;
  64. //如果<a>标签的高度或者它的父节点的高度不够,设置高度以保证按钮显示
  65. //var element = $(this);
  66. //console.log('***************');
  67. //console.log(this);
  68. //while(element[0] !== undefined && 'body' != element[0].tagName.toLowerCase()){
  69. // if("static" == element.css('display')){
  70. // element.css('display','inline-block');
  71. // }
  72. //console.log(element);
  73. //console.log(element.height() + " "+element.css('height'));
  74. //element.height(height);
  75. // element.css('height','auto');
  76. // element = $(element.parent());
  77. // console.log(element[0].tagName);
  78. //}
  79. ele_btn = $("<button onclick='on_click_event()' style='opacity:0.9;display:block;width:100%;height:58px;font-size:1.5em;z-index:999'><span id='"+item_btn+"'>获取优惠券</span><br/><span id='"+item_id+"' style='font-size:0.5em'></span></button>");
  80. $(this).prepend(ele_btn);
  81. }else if($(this).attr('class').indexOf('hot-link') >= 0){
  82. console.log(this);
  83. var coupon_item = $(this);
  84. ele_btn = $("<button onclick='on_click_event()' style='opacity:0.9;display:block;position:absolute;left:"+coupon_item.position().left+"px;width:"+coupon_item.width()+"px;height:58px;top:"+(coupon_item.position().top)+"px;font-size:1.5em;z-index:999'><span id='"+item_btn+"'>获取优惠券</span><br/><span id='"+item_id+"' style='font-size:0.5em'></span></button>");
  85. $(this).before(ele_btn);
  86. }else{
  87. ele_btn = $("<button onclick='on_click_event()' style='opacity:0.9;display:block;width:100%;height:58px;font-size:1.5em;z-index:999'><span id='"+item_btn+"'>获取优惠券</span><br/><span id='"+item_id+"' style='font-size:0.5em'></span></button>");
  88. console.log($(this).attr('class'));
  89. }
  90.  
  91. var click_count = 0;
  92. var is_continue = false;
  93. $(this).on('click',on_click_event);
  94. ele_btn.on('click',on_click_event);
  95. //点击按钮后循环请求领取优惠券的链接
  96. function on_click_event(){
  97. click_count = 0;
  98. is_continue = !is_continue;
  99. function get_coupon(){
  100. click_count += 1;
  101. GM_xmlhttpRequest({
  102. url:url,
  103. method:'GET',
  104. onload:function(response){
  105. var msg = '';
  106. if(response.finalUrl.indexOf('passport.jd.com') >= 0){
  107. msg = click_count+':已停止领取,请登录(不可用)后重试';
  108. is_continue = false;
  109. }else{
  110. var result = response.responseText;
  111. var doc = $(result);
  112. var contents = doc.find('.content');
  113. if(contents.length > 0){
  114. var get_coupon_result = contents[0].innerText;
  115. if(get_coupon_result.indexOf('恭喜') >= 0){
  116. is_continue = false;
  117. }
  118. msg = click_count+":"+contents[0].innerText;
  119. }else{
  120. msg = click_count+':领取优惠券出错,请查看console日志';
  121. console.log(response);
  122. }
  123. }
  124. $('#'+item_id).text(msg);
  125. if(is_continue){
  126. setTimeout(function(){
  127. get_coupon();
  128. },0);
  129. }else{
  130. $('#'+item_btn).text('开始获取优惠券');
  131. }
  132. }
  133. });
  134. }
  135. if(is_continue){
  136. if(url.indexOf("//") === 0){
  137. url = unsafeWindow.location.protocol + url;
  138. }
  139. console.log('开始获取优惠券:'+ url);
  140. $('#'+item_btn).text('停止获取优惠券');
  141. get_coupon();
  142. }else{
  143. console.log('停止获取优惠券:'+ url);
  144. $('#'+item_btn).text('开始获取优惠券');
  145. }
  146. }
  147. });
  148. }
  149. //setTimeout(init_page, 100);
  150. var old_head_insertBefore = document.head.insertBefore;
  151. document.head.insertBefore = function(e){
  152. console.log('insertBefore');
  153. console.log(e);
  154. var argu = [];
  155. for(var index = 0 ; index < arguments.length ; ++index){
  156. argu.push(arguments[index]);
  157. }
  158. if(e.src.indexOf('passport') >= 0){
  159. init_page();
  160. }
  161. old_head_insertBefore.apply(document.head, argu);};
  162. })();

QingJ © 2025

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