Advertisement
Guest User

Untitled

a guest
Jan 1st, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.77 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. // @version 1
  7. // ==/UserScript==
  8.  
  9. (function () {
  10.  
  11. var nicks = {
  12. "username": "password",
  13. "alt name": "password"
  14. };
  15. var avatarNick = "username";
  16.  
  17. var autojoinRooms = [
  18. "BOF",
  19. "dev",
  20. "help",
  21. "francais",
  22. "thp",
  23. "battlespot",
  24. "ou"
  25. ];
  26.  
  27. ////////////////////////////////////////////////////////////////////
  28.  
  29. if (typeof __PAGE_SCOPE_RUN__ === "undefined") {
  30. (function page_scope_runner() {
  31. var my_src = "(" + page_scope_runner.caller.toString() + ")();";
  32. var script = document.createElement("script");
  33. script.setAttribute("type", "application/javascript");
  34. script.textContent = "var __PAGE_SCOPE_RUN__ = true;\n" + my_src;
  35. setTimeout(function () {
  36. document.body.appendChild(script);
  37. document.body.removeChild(script);
  38. }, 0);
  39. })();
  40.  
  41. return;
  42. }
  43.  
  44. function toId(s) {
  45. return s.toLowerCase().replace(/[^\-a-z0-9]+/g, "");
  46. }
  47. avatarNick = toId(avatarNick);
  48.  
  49. for (var n in nicks) {
  50. var id = toId(n);
  51. nicks[id] = {nick: n, pass: nicks[n]};
  52. if (id !== n)
  53. delete nicks[n];
  54. }
  55.  
  56. var ding = (function () {
  57. function sin(f, t) { return Math.sin(f * 2 * Math.PI * t); }
  58. function waveform(t) {
  59. var b = 880;
  60. return sin(b * 1.25, t) + sin(b * 1.5, t) + sin(b * 2, t);
  61. }
  62.  
  63. function envelope(t) {
  64. var a = 40;
  65. var n = 1.3;
  66. return Math.pow(t, n - 1) / Math.pow(a * Math.pow(t, n) + 1, 2);
  67. }
  68.  
  69. var sampleRate = 44100;
  70. var length = 2;
  71.  
  72. var context = new AudioContext();
  73. var buffer = context.createBuffer(1, sampleRate * length, sampleRate);
  74. var samples = buffer.getChannelData(0);
  75.  
  76. var maxSample = 0;
  77. for (var t = 0.000001, s = 0; t < length; t += 1 / sampleRate, ++s) {
  78. samples[s] = envelope(t) * waveform(t);
  79. maxSample = Math.max(Math.abs(samples[s]), maxSample);
  80. }
  81. for (var s = 0; s < samples.length; ++s)
  82. samples[s] /= maxSample;
  83.  
  84. return function () {
  85. console.log("ding!");
  86. var node = context.createBufferSource();
  87. node.buffer = buffer;
  88. node.connect(context.destination);
  89. node.start(0);
  90. };
  91. })();
  92. window.ding = ding;
  93.  
  94. function main() {
  95. probeInterval = setInterval(function () {
  96. if (window.app && window.app.socket && window.app.socket.onmessage &&
  97. window.app.socket.onmessage.toString().indexOf("self.receive") >= 0) {
  98. clearInterval(probeInterval);
  99.  
  100. window.Tools.interstice.isWhitelisted = function () { return true; };
  101.  
  102. window.app.socket._onmessage = window.app.socket.onmessage;
  103. window.app.socket.onmessage = function (msg) {
  104. if (window.logMessages)
  105. window.console.log(msg.data);
  106.  
  107. var lines = filterLog(msg.data.split("\n"));
  108. if (lines[0][0] === ">" && lines[1].indexOf("|init|") !== 0) {
  109. var roomid = toId(lines[0].slice(1));
  110. if (!window.app.rooms[roomid]) {
  111. lines.shift();
  112. lines = lines.map(function (line) {
  113. return "|pm|>" + roomid + "|>" + roomid + "|" + line;
  114. });
  115. }
  116. }
  117.  
  118. return this._onmessage({data: lines.join("\n")});
  119. };
  120.  
  121. window.app.socket._send = window.app.socket.send;
  122. window.app.socket.send = function (msg) {
  123. var cmdIndex = msg.indexOf("|/");
  124. if (msg.indexOf("|/") >= 0) {
  125. var parts = msg.slice(cmdIndex + 2).split(" ");
  126. var room = window.app.rooms[toId(msg.split("|")[0] || "lobby")];
  127. var cmd = parts.shift().toLowerCase().trim();
  128. var arg = parts.join(" ");
  129. switch (cmd) {
  130. case "lb" :
  131. return room.send((arg ? arg + ": " : "") + "Have you tried the \"Look for a battle\" button?");
  132.  
  133. case "katakana" :
  134. return room.send("/me unsheathes katakana ▬▬ι═══════════════════ﺤ");
  135.  
  136. case "lenny" :
  137. return room.send("( ͡° ͜ʖ ͡°)");
  138.  
  139. case "j" :
  140. return this.send("|/join " + arg);
  141.  
  142. case "gods" :
  143. return room.send("/wall Zaterdag om 21:00 is er een Gods Among Us tour, een tour van OU + 1 Ubers Pokémon! De Pokémon die in deze tour toegestaan is: Yveltal! Bereid nu alvast een team voor zodat je dat zaterdag niet meer hoeft te doen!");
  144.  
  145. case "b" :
  146. return this.send(arg);
  147.  
  148. case "whois" :
  149. case "alts" :
  150. var user = toId(arg);
  151. if (window.altList[user])
  152. room.receive("Client recorded alts: " + Object.keys(window.altList[user]).map(function (u) { return window.prettyNames[u]; }).join(", "));
  153. break;
  154.  
  155. case "userlist" :
  156. var ranks = {};
  157. Object.keys(room.users).map(function (u) {
  158. var user = room.users[u];
  159. var rank = user[0];
  160. user = user.slice(1);
  161.  
  162. if (!ranks[rank])
  163. ranks[rank] = [];
  164. ranks[rank].push(user);
  165. });
  166. this.onmessage({data: "|popup|" + Object.keys(ranks).sort().map(function (r) {
  167. return r + ":\n" + ranks[r].sort().join(", ");
  168. }).join("\n\n")});
  169. return;
  170.  
  171. case "pmall" :
  172. if (room.pmAllInterval)
  173. return room.receive("Existing /pmall already in progress. Cancel with /cancelpmall");
  174. var users = Object.keys(room.users);
  175. room.pmAllInterval = setInterval(function () {
  176. if (users.length === 0) {
  177. clearInterval(room.pmAllInterval);
  178. delete room.pmAllInterval;
  179. } else
  180. window.app.socket.send("|/pm " + users.pop() + "," + arg);
  181. }, 1200);
  182. room.receive("Cancel with /cancelpmall");
  183. return;
  184.  
  185. case "cancelpmall" :
  186. clearInterval(room.pmAllInterval);
  187. delete room.pmAllInterval;
  188. room.receive("/pmall cancelled");
  189. return;
  190.  
  191. case "ceval" :
  192. var result;
  193. try {
  194. var s = window.app.socket.send.bind(window.app.socket);
  195. result = eval(arg);
  196. } catch (e) {
  197. result = e;
  198. }
  199. this.onmessage({data: "|pm|>client|>client|" + result});
  200. return;
  201.  
  202. case "trn" :
  203. msg = msg.replace(",0,", ",1,");
  204. break;
  205. }
  206. }
  207.  
  208. window.console.log("sending:", msg);
  209. return this._send(msg);
  210. };
  211.  
  212. window.app.user._rename = window.app.user.rename;
  213. window.app.user.rename = function (nick) {
  214. var nickInfo = nicks[toId(nick)];
  215. if (nickInfo)
  216. this.passwordRename(nickInfo.nick, nickInfo.pass);
  217. else
  218. this._rename(nick);
  219. };
  220.  
  221. window.app.rooms['']._addPM = window.app.rooms[''].addPM;
  222. window.app.rooms[''].addPM = function (user, message, pm) {
  223. if (!window.app.focused)
  224. ding();
  225. return this._addPM(user, message, pm);
  226. };
  227.  
  228. var botCounter = {n: 0, nSpamCheck: 0, t: 0};
  229. window.prettyNames = {};
  230. window.altList = {};
  231. window.ChatRoom.prototype._add = window.ChatRoom.prototype.add;
  232. window.BattleRoom.prototype._add = window.BattleRoom.prototype.add;
  233. window.ChatRoom.prototype.add = window.BattleRoom.prototype.add = function (log) {
  234. var room = this;
  235. if (!room.userRanks)
  236. room.userRanks = {};
  237. if (typeof log === "string")
  238. log = log.split("\n").map(function (line) {
  239. if (window.Config.server.id !== "showdown" || this.id !== "lobby")
  240. line = line.replace(/^\|J\|/, "|j|").replace(/^\|L\|/, "|l|");
  241.  
  242. var cmd = line.toLowerCase().split("|")[1];
  243. if (cmd === "users") {
  244. line.split("|")[2].split(",").slice(1).forEach(function (u) {
  245. room.userRanks[toId(u)] = u[0];
  246. window.prettyNames[toId(u)] = u.slice(1);
  247. });
  248. }
  249. if (cmd in {j: 1, join: 1, l: 1, leave: 1}) {
  250. var name = line.split("|")[2];
  251. if (room.type === "battle")
  252. name = (room.userRanks[toId(name)] || " ") + name;
  253. room.userRanks[toId(name)] = name[0];
  254. window.prettyNames[toId(name)] = name.slice(1);
  255.  
  256. if (room.type === "battle") {
  257. if (!room.users)
  258. room.users = {};
  259. if (cmd in {j: 1, join: 1})
  260. room.users[toId(name)] = name;
  261. else
  262. delete room.users[toId(name)];
  263. }
  264. }
  265. if (cmd === "n") {
  266. var parts = line.split("|");
  267. var from = toId(parts[3]);
  268. var to = parts[2];
  269. var newRank = to[0];
  270. var prettyTo = to.slice(1);
  271. to = toId(to);
  272.  
  273. if (!window.prettyNames[from])
  274. window.prettyNames[from] = from;
  275.  
  276. if (window.altList[from] && window.altList[to] && window.altList[from] !== window.altList[to]) {
  277. for (var u in window.altList[from])
  278. window.altList[to][u] = 1;
  279. window.altList[from] = window.altList[to];
  280. } else {
  281. if (!window.altList[from])
  282. window.altList[from] = window.altList[to] || {};
  283. if (!window.altList[to])
  284. window.altList[to] = window.altList[from];
  285. }
  286. window.altList[to][from] = 1;
  287. window.altList[to][to] = 1;
  288.  
  289. line += "\n|raw|<small><font color=\"#555\">" + window.Tools.escapeHTML((room.userRanks[from] || "") + window.prettyNames[from] + " -> " + newRank + prettyTo) + "</font></small>";
  290.  
  291. window.prettyNames[to] = prettyTo;
  292. room.userRanks[to] = newRank;
  293. }
  294. if (cmd in {c: 1, chat: 1}) {
  295. var name = line.split("|")[2];
  296. room.users[toId(name)] = name;
  297. room.userRanks[toId(name)] = name[0];
  298. window.prettyNames[toId(name)] = name.slice(1);
  299. }
  300. if (cmd === "c:") {
  301. var name = line.split("|")[3];
  302. room.users[toId(name)] = name;
  303. room.userRanks[toId(name)] = name[0];
  304. window.prettyNames[toId(name)] = name.slice(1);
  305. }
  306. return line;
  307. }, this).join("\n");
  308.  
  309. this._add(log);
  310. };
  311.  
  312. probeInterval = setInterval(function () {
  313. if (window.app.socket.readyState !== 1 || window.app.user.attributes.userid.indexOf("guest") === 0)
  314. return;
  315. clearInterval(probeInterval);
  316.  
  317. if (window.Config.server.id === "showdown") {
  318. function doAutojoin() {
  319. window.app.socket.send("|/join " + autojoinRooms.shift());
  320. if (autojoinRooms.length > 0)
  321. setTimeout(doAutojoin, 1000);
  322. }
  323. doAutojoin();
  324. }
  325.  
  326. if (window.app.user.attributes.userid !== avatarNick && nicks[window.app.user.attributes.userid]) {
  327. var oldNick = window.app.user.attributes.userid;
  328. window.app.user.rename(avatarNick);
  329. probeInterval = setInterval(function () {
  330. if (window.app.user.attributes.userid !== avatarNick)
  331. return;
  332. clearInterval(probeInterval);
  333. window.app.user.rename(oldNick);
  334. }, 0);
  335. }
  336. }, 0);
  337. }
  338. }, 0);
  339. }
  340.  
  341. function isContainsJavascript(text) {
  342. return text.toLowerCase().indexOf("<script") >= 0 || text.toLowerCase().match(/<[^>]+on[a-z]+=/);
  343. }
  344.  
  345. function filterLog(log) {
  346. for (var l = 0; l < log.length; ++l)
  347. log[l] = filterLogEntry(log[l]);
  348. return log;
  349. }
  350.  
  351. function filterLogEntry(entry) {
  352. if (typeof entry === "string") {
  353. if (entry[0] === '|' && entry.indexOf("|", 1) >= 0) {
  354. var command = entry.split("|");
  355. if ((command[1] === "raw" && isContainsJavascript(entry)) ||
  356. (command[1] === "c" && command[3].indexOf("/warn") === 0)) {
  357. command[1] = "";
  358. entry = command.join("|");
  359. }
  360. }
  361. if (entry.indexOf("|refresh") == 0 || entry.indexOf("|deinit") == 0 || entry.match(/\|noinit\|?$/)) {
  362. if (!window.app.focused)
  363. ding();
  364. return "||" + entry;
  365. }
  366. }
  367.  
  368. if (entry.evalRawMessage) {
  369. entry.message = entry.evalRawMessage;
  370. delete entry.evalRawMessage;
  371. }
  372. if (entry.evalRulesRedirect) {
  373. entry.message = "evalRulesRedirect";
  374. delete entry.evalRulesRedirect;
  375. }
  376. if (entry.rawMessage && isContainsJavascript(entry.rawMessage)) {
  377. entry.message = entry.rawMessage;
  378. delete entry.rawMessage;
  379. }
  380.  
  381. return entry;
  382. }
  383.  
  384. main();
  385.  
  386. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement