Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Pokemon Showdown
  3. // @namespace pokemonshowdown
  4. // @include http://play.pokemonshowdown.com/*
  5. // @include https://play.pokemonshowdown.com/*
  6. // @include http://*.psim.us/*
  7. // @version 1
  8. // @grant unsafeWindow
  9. // ==/UserScript==
  10. try {
  11.  
  12.  
  13.  
  14. function toId(s) {
  15. return s.toLowerCase().replace(/[^a-z0-9]+/g, "");
  16. }
  17.  
  18. var ding = (function () {
  19. function sin(f, t) { return Math.sin(f * 2 * Math.PI * t); }
  20. function waveform(t) {
  21. var b = 880;
  22. return sin(b * 1.25, t) + sin(b * 1.5, t) + sin(b * 2, t);
  23. }
  24.  
  25. function envelope(t) {
  26. var a = 40;
  27. var n = 1.3;
  28. return Math.pow(t, n - 1) / Math.pow(a * Math.pow(t, n) + 1, 2);
  29. }
  30.  
  31. function round(n, by) {
  32. var r = n % by;
  33. if (r <= (by / 2))
  34. return n - r;
  35. else
  36. return n + by - r;
  37. }
  38.  
  39. var sampleRate = 44100;
  40. var length = 2;
  41.  
  42. var context = new AudioContext();
  43. var buffer = context.createBuffer(1, sampleRate * length, sampleRate);
  44. var samples = buffer.getChannelData(0);
  45.  
  46. var maxSample = 0;
  47. for (var t = 0.000001, s = 0; t < length; t += 1 / sampleRate, ++s) {
  48. samples[s] = envelope(t) * waveform(t);
  49. maxSample = Math.max(Math.abs(samples[s]), maxSample);
  50. }
  51. for (var s = 0; s < samples.length; ++s)
  52. samples[s] /= maxSample;
  53.  
  54. return function () {
  55. console.log("ding!");
  56. var node = context.createBufferSource();
  57. node.buffer = buffer;
  58. node.connect(context.destination);
  59. node.start(0);
  60. }
  61. })();
  62. unsafeWindow.ding = ding;
  63.  
  64. function main() {
  65. probeInterval = setInterval(function () {
  66. if (unsafeWindow.app && unsafeWindow.app.socket && unsafeWindow.app.socket.onmessage &&
  67. unsafeWindow.app.socket.onmessage.toString().indexOf("self.receive") >= 0) {
  68. clearInterval(probeInterval);
  69.  
  70. unsafeWindow.Tools.interstice.isWhitelisted = function () { return true; };
  71.  
  72. unsafeWindow.app.socket._onmessage = unsafeWindow.app.socket.onmessage;
  73. unsafeWindow.app.socket.onmessage = function (msg) {
  74. if (unsafeWindow.logMessages)
  75. unsafeWindow.console.log(msg.data);
  76. try {
  77. var data = JSON.parse(msg.data);
  78.  
  79. // console
  80. data = filterLogEntry(data);
  81.  
  82. // init
  83. if (data.log)
  84. data.log = filterLog(data.log);
  85.  
  86. // update
  87. if (/*data.logUpdate || */data.updates)
  88. data.updates = filterLog(data.updates);
  89.  
  90. msg.data = JSON.stringify(data);
  91. } catch (e) {
  92. msg.data = filterLog(msg.data.split("\n")).join("\n");
  93. }
  94. return this._onmessage(msg);
  95. };
  96.  
  97. unsafeWindow.app.rooms['']._addPM = unsafeWindow.app.rooms[''].addPM;
  98. unsafeWindow.app.rooms[''].addPM = function (user, message, pm) {
  99. if (!unsafeWindow.app.focused)
  100. ;
  101. return this._addPM(user, message, pm);
  102. };
  103.  
  104. var botCounter = {n: 0, nSpamCheck: 0, t: 0};
  105. unsafeWindow.ChatRoom.prototype._add = unsafeWindow.ChatRoom.prototype.add;
  106. unsafeWindow.ChatRoom.prototype.add = function (log) {
  107. this._add(log);
  108. if (typeof log === "string")
  109. log = log.split("\n");
  110.  
  111. if (log.length !== 1)
  112. return;
  113.  
  114. var parts = log[0].split("|");
  115. if (parts[1] === "c") {
  116. if (Date.now() > botCounter.t + 5 * 60 * 1000)
  117. botCounter.n = 0;
  118. if (Date.now() > botCounter.t + 30 * 1000)
  119. botCounter.nSpamCheck = 0;
  120.  
  121. var reply = (function(message) {
  122. botCounter.t = Date.now();
  123. this.send(message + new Array(++botCounter.n + 1).join("\u200c"));
  124. ++botCounter.nSpamCheck;
  125. }).bind(this);
  126.  
  127. if (botCounter.nSpamCheck >= 3)
  128. return;
  129. }
  130. }
  131.  
  132. probeInterval = setInterval(function () {
  133. if (unsafeWindow.app.socket.readyState !== 1 || unsafeWindow.app.user.attributes.userid.indexOf("guest") === 0)
  134. return;
  135. clearInterval(probeInterval);
  136.  
  137. if (unsafeWindow.Config.server.id === "showdown" && unsafeWindow.app.user.attributes.userid.indexOf("feliburn") === 0) {
  138. unsafeWindow.app.socket.send("|/join tournaments");
  139. unsafeWindow.app.socket.send("|/join gamecorner");
  140. unsafeWindow.app.socket.send("|/join battlespot");
  141. unsafeWindow.app.socket.send("|/join rarelyused");
  142. unsafeWindow.app.socket.send("|/join challenges");
  143. unsafeWindow.app.socket.send("|/avatar 68");
  144.  
  145. setTimeout(function(){
  146. unsafeWindow.app.socket.send("|/join meteorfalls");
  147. }, 3000);
  148. }
  149. if (unsafeWindow.Config.server.id === "showdown" && unsafeWindow.app.user.attributes.userid.indexOf("feliburn") !== 0 && unsafeWindow.app.user.attributes.userid.indexOf("guest") !== 0) {
  150. unsafeWindow.app.socket.send("|/join tournaments");
  151. unsafeWindow.app.socket.send("|/join gamecorner");
  152. unsafeWindow.app.socket.send("|/join battlespot");
  153. unsafeWindow.app.socket.send("|/join rarelyused");
  154. unsafeWindow.app.socket.send("|/join challenges");
  155. unsafeWindow.app.socket.send("|/avatar 94");
  156.  
  157. setTimeout(function(){
  158. unsafeWindow.app.socket.send("|/join meteorfalls");
  159. }, 3000);
  160. }
  161.  
  162. if (unsafeWindow.app.user.attributes.userid !== avatarNick && nicks[unsafeWindow.app.user.attributes.userid]) {
  163. var oldNick = unsafeWindow.app.user.attributes.userid;
  164. unsafeWindow.app.user.rename(avatarNick);
  165. probeInterval = setInterval(function () {
  166. if (unsafeWindow.app.user.attributes.userid !== avatarNick)
  167. return;
  168. clearInterval(probeInterval);
  169. unsafeWindow.app.user.rename(oldNick);
  170. }, 0);
  171. }
  172. }, 0);
  173. }
  174. }, 0);
  175. }
  176.  
  177. function isContainsJavascript(text) {
  178. return text.toLowerCase().indexOf("<script") >= 0 || text.toLowerCase().match(/<[^>]+on[a-z]+=/);
  179. }
  180.  
  181. function filterLog(log) {
  182. for (var l = 0; l < log.length; ++l)
  183. log[l] = filterLogEntry(log[l]);
  184. return log;
  185. }
  186.  
  187. function filterLogEntry(entry) {
  188. if (typeof entry === "string") {
  189. if (entry[0] === '|' && entry.indexOf("|", 1) >= 0) {
  190. var command = entry.split("|");
  191. if ((command[1] === "raw" && isContainsJavascript(entry)) ||
  192. (command[1] === "c" && command[3].indexOf("/warn") === 0)) {
  193. command[1] = "";
  194. entry = command.join("|");
  195. }
  196. }
  197. if (entry.indexOf("|refresh") == 0 || entry.indexOf("|deinit") == 0 || entry.match(/\|noinit\|?$/))
  198. return "||" + entry;
  199. }
  200.  
  201. if (entry.evalRawMessage) {
  202. entry.message = entry.evalRawMessage;
  203. delete entry.evalRawMessage;
  204. }
  205. if (entry.evalRulesRedirect) {
  206. entry.message = "evalRulesRedirect";
  207. delete entry.evalRulesRedirect;
  208. }
  209. if (entry.rawMessage && isContainsJavascript(entry.rawMessage)) {
  210. entry.message = entry.rawMessage;
  211. delete entry.rawMessage;
  212. }
  213.  
  214. return entry;
  215. }
  216.  
  217. main();
  218.  
  219. } catch (e) { console.log(e); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement