SwagTv script testing

this script will automatically fetch and go to the next video after the meter goes up.

当前为 2014-05-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name SwagTv script testing
  3. // @namespace swagtv
  4. // @description this script will automatically fetch and go to the next video after the meter goes up.
  5. // @version 0.3
  6. // @include http://video.swagbucks.com/*
  7. // @include http://www.swagbucks.com/?cmd=cp-get-captcha-image*
  8. // @require https://greasyfork.org/scripts/1706-gocr-library/code/GOCR%20Library.js?version=4235
  9. // ==/UserScript==
  10. //
  11. // --------------------------------------------------------------------
  12.  
  13.  
  14. if (window.opener != null && window.location.href.indexOf("mumbojumbo") > -1) {
  15. console.log("in");
  16. console.log(window.opener);
  17. window.opener.postMessage("Child Frame Loaded", "*");
  18. var pic = document.getElementsByTagName("img")[0];
  19. var image = getBase64Image(pic);
  20. var ocrString = GOCR(image);
  21. console.log(ocrString.toUpperCase());
  22. window.opener.postMessage({magicword: "mumbojumbo", string: ocrString}, "*");
  23. }
  24.  
  25. else{
  26. var iLoops = 0;
  27. window.setInterval(reloadTimer,1000);
  28. var doc = document.getElementById("feed-ajax-div");
  29. var notes = false;
  30. var _class = document.getElementsByClassName("thumb-container");
  31. /*for (var i = 0; i < _class.length; i++) {
  32. if (_class[i].className == "thumb-container active") {
  33. var k = i;
  34. break;
  35. }
  36. } */
  37. var k = getK();
  38. var video = document.getElementById("video-title");
  39. video.parentNode.removeChild(video);
  40. var comment = document.getElementById("commentsCont");
  41. comment.parentNode.removeChild(comment);
  42. var randomNum = 0;
  43. var strPercentStart = null;
  44. var link = null;
  45. var div = null;
  46. }
  47.  
  48. function getK(){
  49. for (var i = 0; i < _class.length; i++) {
  50. if (_class[i].className == "thumb-container active") {
  51. var k = i;
  52. break;
  53. }
  54. }
  55. return k
  56. }
  57.  
  58. function reloadTimer(){
  59. iLoops++;
  60. if (iLoops%5==0)
  61. console.log(iLoops);
  62. if (iLoops == 5) {
  63. console.log("k: "+k);
  64. strPercentStart = document.getElementById("meterNumber").innerHTML;
  65. div = document.getElementById("meterDuplicateVideo");
  66. if (div.style.display == 'none') {
  67. randomNum = Math.floor(Math.random()*15 + 50);
  68. }
  69. else {
  70. randomNum = 8;
  71. };
  72. if (k == null) {
  73. console.log("In K=3");
  74. //var clickEvent = document.createEvent('MouseEvents');
  75. //clickEvent.initEvent('click', true, true);
  76. //document.getElementsByClassName("feed-ajax-next")[0].dispatchEvent(clickEvent);
  77. if (!document.getElementsByClassName("feed-ajax-next")[0])
  78. notes = true;
  79. else
  80. document.getElementsByClassName("feed-ajax-next")[0].click();
  81. k = -1;
  82. }
  83. };
  84. if (iLoops == randomNum){
  85. //console.log(k);
  86. if (document.getElementById("sbvd_capText")) {
  87. //alert('Captcha');
  88. pic = document.getElementById("sbvdcapimg");
  89. //pic.crossOrigin = 'anonymous';
  90. var string = getCaptchaText(pic);
  91. }
  92. else {
  93. if (notes)
  94. alert("Pick a new category");
  95. else{
  96. link = document.getElementsByClassName("thumb-link")[k+1].href;
  97. window.location.href = link;
  98. }
  99. }
  100. }
  101. };
  102. var childWindow = null;
  103. function getCaptchaText(pic){
  104. var pic = document.getElementById("sbvdcapimg");
  105. childWindow = window.open(pic.src+"&magicword=mumbojumbo");
  106. }
  107.  
  108. if (window.opener != null && window.location.href.indexOf("mumbojumbo") > -1) {
  109. console.log("in");
  110. window.opener.postMessage("Child Frame Loaded", "*");
  111. var pic = document.getElementsByTagName("img")[0];
  112. console.log(pic);
  113. ocrString = ""
  114. if (pic){
  115. setTimeout(function(){
  116. var image = getBase64Image(pic);
  117. console.log(image);
  118. var imageData = image[0];
  119. var ocrString = GOCR(imageData);
  120. console.log(ocrString.toUpperCase());
  121. },500);
  122. }
  123. window.opener.postMessage({magicword: "mumbojumbo", string: ocrString}, "*");
  124. window.opener=window;
  125. window.close();
  126. top.window.close();
  127. }
  128.  
  129. window.addEventListener("message", function(e) {
  130. if (e.data.magicword === "mumbojumbo") {
  131. console.log("Message Received");
  132. console.log(e.data);
  133. var ocrString = e.data.string.toUpperCase();
  134. console.log("string");
  135. console.log(ocrString);
  136. console.log("string");
  137. if (childWindow)
  138. childWindow.close();
  139. if (ocrString.indexOf('_') === -1){
  140. if (document.getElementById("sbvd_capText")){
  141. document.getElementById("sbvd_capText").value = ocrString;
  142. document.getElementsByClassName("btnClaim")[0].click();
  143. setTimeout(function() {
  144. if (!document.getElementById("sbvd_capText")){
  145. k=getK();
  146. if (k == null)
  147. k=-1;
  148. link = document.getElementsByClassName("thumb-link")[k+1].href;
  149. //alert("NEXT");
  150. window.location.href = link;
  151. }
  152. else{
  153. //alert("Opened 1");
  154. var pic = document.getElementById("sbvdcapimg");
  155. childWindow = window.open(pic.src+"&magicword=mumbojumbo");
  156. }
  157. }, 1000);
  158. }
  159. }
  160. else{
  161. //alert("Opened 2");
  162. var pic = document.getElementById("sbvdcapimg");
  163. childWindow = window.open(pic.src+"&magicword=mumbojumbo");
  164. }
  165. }
  166. else{
  167. console.log("Also message received");
  168. console.log(e.data);
  169. }
  170. }, false);
  171.  
  172. function getBase64Image(img) {
  173. /* // Create an empty canvas element
  174. var pic = document.createElement("img");
  175. pic.src = img;
  176. var canvas = document.createElement("canvas");
  177. canvas.width = img.width;
  178. canvas.height = img.height;
  179.  
  180. // Copy the image contents to the canvas
  181. var ctx = canvas.getContext("2d");
  182. ctx.drawImage(pic, 0, 0);
  183. var pic2 = document.createElement("img");
  184. pic2.src=canvas.toDataURL("image/png");
  185. console.log(canvas.toDataURL("image/png"));
  186. ctx.drawImage(pic2,0,0);
  187.  
  188. // Get the data-URL formatted image
  189. // Firefox supports PNG and JPEG. You could check img.src to
  190. // guess the original format, but be aware the using "image/jpg"
  191. // will re-encode the image.
  192. var dataURL = ctx.getImageData(0,0,canvas.width,canvas.height);
  193.  
  194. return dataURL;//.replace(/^data:image\/(png|jpg);base64,/, "");*/
  195. // Create an empty canvas element
  196. var canvas = document.createElement("canvas");
  197. canvas.width = img.width;
  198. canvas.height = img.height;
  199.  
  200. // Copy the image contents to the canvas
  201. var ctx = canvas.getContext("2d");
  202. ctx.drawImage(img, 0, 0);
  203.  
  204. // Get the data-URL formatted image
  205. // Firefox supports PNG and JPEG. You could check img.src to
  206. // guess the original format, but be aware the using "image/jpg"
  207. // will re-encode the image.
  208. var dataURL = ctx.getImageData(0,0,canvas.width,canvas.height);
  209.  
  210. return dataURL;//.replace(/^data:image\/(png|jpg);base64,/, "");
  211. }