wuiyang

botz

Jan 13th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Luno Bot
  3. // @namespace wuiyang
  4. // @include https://www.luno.com/*
  5. // @version 1
  6. // @grant none
  7. // ==/UserScript==
  8.  
  9. var hasSetup = false;
  10. var lowerLimit = 0;
  11. var lowerHasNotify = false;
  12. var lowerNotifyCount = 0;
  13. var upperLimit = 0;
  14. var upperHasNotify = false;
  15. var upperNotifyCount = 0;
  16. var spreadLimit = 0;
  17. var spreadHasNotify = false;
  18. var spreadNotifyCount = 0;
  19.  
  20. function beep(frequency, time){
  21. if(frequency == undefined){
  22. frequency = 500;
  23. }
  24. if(time == undefined){
  25. time = 200;
  26. }
  27. var context = new (window.AudioContext || window.webkitAudioContext)();
  28. var o = context.createOscillator();
  29. var gainNode = context.createGain();
  30. o.connect(gainNode);
  31. gainNode.connect(context.destination);
  32. gainNode.gain.value = 0.35;
  33. o.type = 'sine';
  34. o.frequency.value = frequency;
  35.  
  36. // Star the sound
  37. o.start(0);
  38. // Play the sound for a second before stopping it
  39. setTimeout(function() {
  40. o.stop(0);
  41. }, time);
  42. }
  43.  
  44. function beepSeq(freq, count, step, time, wait, i){
  45. if(i == undefined){ i = 0; }
  46. if(count == undefined){ count = 1; }
  47. if(step == undefined){ step = 0; }
  48. if(time == undefined){ time = 200; }
  49. if(wait == undefined){ wait = 0; }
  50. beep(freq+step*i, time);
  51. i++;
  52. if(i<count){
  53. setTimeout(function(){beepSeq(freq, count, step, time, wait, i);}, time+wait);
  54. }
  55. }
  56.  
  57. function checkSpread(){
  58. if(spreadLimit == 0){
  59. return;
  60. }
  61. var spread = parseFloat(document.getElementsByClassName("spread-text")[0].innerHTML.split(" ")[0]);
  62. if(spread >= spreadLimit){
  63. if(!spreadHasNotify){
  64. if(document.hasFocus()){
  65. beepSeq(500, 3, 0, 200, 100);
  66. spreadHasNotify = true;
  67. }else{
  68. if(spreadNotifyCount%8 == 0){
  69. spreadNotifyCount = 0;
  70. beepSeq(500, 3, 0, 200, 100);
  71. }
  72. spreadNotifyCount++;
  73. }
  74. }
  75. }else{
  76. speardHasNotify = false;
  77. }
  78. }
  79.  
  80. function checkLowerPrice(){
  81. var currentPrice = parseFloat(document.getElementById("mCSB_5_container").children[0].children[0].children[0].innerHTML);
  82. if(lowerLimit == 0){
  83. return;
  84. }
  85. if(lowerLimit >= currentPrice){
  86. if(!lowerHasNotify){
  87. if(document.hasFocus()){
  88. beepSeq(700, 3, -100, 100);
  89. lowerHasNotify = true;
  90. }else{
  91. if(lowerNotifyCount%8 == 0){
  92. lowerNotifyCount = 0;
  93. beepSeq(700, 3, -100, 100);
  94. }
  95. lowerNotifyCount++;
  96. }
  97. }
  98. }else{
  99. lowerHasNotify = false;
  100. }
  101. }
  102.  
  103. function checkUpperPrice(){
  104. var temp = document.getElementById("mCSB_4_container").children;
  105. var currentPrice = parseFloat(temp[temp.length-1].children[0].children[0].innerHTML);
  106. if(upperLimit == 0){
  107. return;
  108. }
  109. if(upperLimit <= currentPrice){
  110. if(!upperHasNotify){
  111. if(document.hasFocus()){
  112. beepSeq(500, 3, 100, 100);
  113. upperHasNotify = true;
  114. }else{
  115. if(upperNotifyCount%8 == 0){
  116. upperNotifyCount = 0;
  117. beepSeq(500, 3, 100, 100);
  118. }
  119. upperNotifyCount++;
  120. }
  121. }
  122. }else{
  123. upperHasNotify = false;
  124. }
  125. }
  126.  
  127. function updateBot(){
  128. beep();
  129. var text;
  130. text = document.getElementById("botLower").value;
  131. lowerLimit = text == "" ? 0 : parseFloat(text);
  132. text = document.getElementById("botUpper").value;
  133. upperLimit = text == "" ? 0 : parseFloat(text);
  134. text = document.getElementById("botSpread").value;
  135. spreadLimit = text == "" ? 0 : parseFloat(text);
  136. console.log("Updated Bot Info");
  137. if(!hasSetup){
  138. hasSetup = true;
  139. runner();
  140. console.log("Bot is runnning now");
  141. }
  142. }
  143.  
  144. function runner(){
  145. checkSpread();
  146. checkLowerPrice();
  147. checkUpperPrice();
  148. setTimeout(runner, 1200);
  149. }
  150.  
  151. function setupBotUI(){
  152. var div = document.createElement("div");
  153. var divl = document.createElement("div");
  154. var divu = document.createElement("div");
  155. var divs = document.createElement("div");
  156. var pl = document.createElement("p");
  157. var pu = document.createElement("p");
  158. var ps = document.createElement("p");
  159. var lower = document.createElement("input");
  160. var upper = document.createElement("input");
  161. var spread = document.createElement("input");
  162. var btn = document.createElement("input");
  163.  
  164. //setup input
  165. lower.id = "botLower";
  166. lower.style = "border:1px solid #293235;width:100%;height:30px;";
  167. lower.placeholder = "lower rate";
  168. lower.type = "number";
  169. lower.min = 0.0001;
  170. lower.step = 0.0001;
  171. upper.id = "botUpper";
  172. upper.style = "border:1px solid #293235;width:100%;height:30px;";
  173. upper.placeholder = "higher rate";
  174. upper.type = "number";
  175. upper.min = 0.0001;
  176. upper.step = 0.0001;
  177. spread.id = "botSpread";
  178. spread.style = "border:1px solid #293235;width:100%;height:30px;";
  179. spread.placeholder = "spread amount";
  180. spread.type = "number";
  181. spread.min = 0.0001;
  182. spread.step = 0.0001;
  183.  
  184. //setup div container for input show in one line
  185. pl.innerHTML += "Current buy is lower than:";
  186. pl.style = "margin:0 0 0 10px;";
  187. divl.appendChild(pl);
  188. divl.appendChild(lower);
  189. pu.innerHTML = "Current sell is higher than:";
  190. pu.style = "margin:0 0 0 10px;";
  191. divu.appendChild(pu);
  192. divu.appendChild(upper);
  193. ps.innerHTML += "Current spread is higher than:";
  194. ps.style = "margin:0 0 0 10px;";
  195. divs.appendChild(ps);
  196. divs.appendChild(spread);
  197.  
  198. //button
  199. btn.type = "submit";
  200. btn.value = "Update Luno Notify Bot";
  201. btn.className = "btn btn-primary";
  202. if (btn.addEventListener)
  203. btn.addEventListener("click", updateBot, false);
  204. else if (btn.attachEvent)
  205. btn.attachEvent('onclick', updateBot);
  206.  
  207. //setup div
  208. div.innerHTML = "Create beep noise when:";
  209. div.appendChild(divu);
  210. div.appendChild(divl);
  211. div.appendChild(divs);
  212. div.innerHTML += "<br>";
  213. div.appendChild(btn);
  214.  
  215. document.getElementsByClassName("exchange-trades-panel")[0].innerHTML = '<h2 class="h1" style="font-size:20px;" translate=""><span class="ng-scope">Luno Notify Bot</span></h2>';
  216. document.getElementsByClassName("exchange-trades-panel")[0].appendChild(div);
  217.  
  218. }
  219.  
  220. function firstload(){
  221. if(document.URL.indexOf("https://www.luno.com/trade/ETHXBT") == 0){
  222. var check = document.getElementsByClassName("spread-text");
  223. if(check.length == 1){
  224. console.log("Loaded notify bot");
  225. setupBotUI();
  226. }else{
  227. setTimeout(firstload, 1500);
  228. }
  229. }
  230. }
  231.  
  232. firstload();
Advertisement
Add Comment
Please, Sign In to add comment