Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.21 KB | None | 0 0
  1. // CHANGELOG
  2.  
  3. // 1.2.2:
  4. // changed if (timing) to if (secs > 0), and sophisticated in_game update in mutationObserver. hopefully this fixes the problem of sometimes timer being inaccessible
  5. // brief test: still weird behavior if you press spacebar right after starting the game maybe because mutationobserver is still firing but no worries
  6.  
  7. // 1.2.1:
  8. // selective page action instead of browser action
  9.  
  10. // 1.2.0:
  11. // popup with quick instructions and link to options
  12. // 7-w baby!
  13.  
  14. // 1.1.2:
  15. // backspace doesn't leave page
  16. // various logistics: check for in-game, check for timer-running; can't enter mass before spacebar, etc.
  17. // print mass below timer for reference
  18. // changed name and description
  19. // layout options page
  20.  
  21. // 1.1.1:
  22. // 'show mass' works for new users
  23.  
  24. // 1.1.0:
  25. // black/white countdown text, reset timer, buffer timer
  26.  
  27. // from manifest:
  28.  
  29. // A dynamic timer to inform your tactics in the crucial moments between split and merge.
  30. var secs = 0;
  31. // is it mass before split or after split?
  32. var k = 7 / 300;
  33.  
  34. var mass_input = "";
  35. var buffer = "";
  36. var buffered = false;
  37. var reset = false;
  38. var in_game = false;
  39. var timing;
  40.  
  41. // var x = document.getElementById("overlays").createElement("CANVAS");
  42. // x.id = "fuckled";
  43. // console.log(x);
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. // http://stackoverflow.com/questions/11816431/how-to-add-a-html5-canvas-within-a-div
  52. function loadCanvas(id) {
  53. var canvas = document.createElement('canvas');
  54. div = document.getElementById(id);
  55. canvas.id = "CursorLayer";
  56. canvas.width = .2 * window.innerWidth;
  57. canvas.height = .1 * window.innerHeight;
  58. canvas.style.zIndex = 8;
  59. canvas.style.position = "absolute";
  60. // change border here for debugging
  61. canvas.style.border = "0px solid";
  62. div.appendChild(canvas)
  63. }
  64.  
  65. function loadCanvas_2(id) {
  66. var canvas = document.createElement('canvas');
  67. div = document.getElementById(id);
  68. canvas.id = "CursorLayer_2";
  69. canvas.width = .2 * window.innerWidth;
  70. canvas.height = .25 * window.innerHeight;
  71. canvas.style.zIndex = 8;
  72. canvas.style.top = "50px";
  73. canvas.style.position = "relative";
  74. // change border here for debugging
  75. canvas.style.border = "0px solid";
  76. div.appendChild(canvas)
  77. }
  78.  
  79. var canvas;
  80. var ctx;
  81.  
  82. var canvas_2;
  83. var ctx_2;
  84.  
  85.  
  86.  
  87. window.onload = function() {
  88. // http://stackoverflow.com/questions/1495219/how-can-i-prevent-the-backspace-key-from-navigating-back
  89. // Prevent the backspace key from navigating back.
  90. $(document).unbind('keydown').bind('keydown', function (event) {
  91. var doPrevent = false;
  92. if (event.keyCode === 8) {
  93. var d = event.srcElement || event.target;
  94. if ((d.tagName.toUpperCase() === 'INPUT' &&
  95. (
  96. d.type.toUpperCase() === 'TEXT' ||
  97. d.type.toUpperCase() === 'PASSWORD' ||
  98. d.type.toUpperCase() === 'FILE' ||
  99. d.type.toUpperCase() === 'SEARCH' ||
  100. d.type.toUpperCase() === 'EMAIL' ||
  101. d.type.toUpperCase() === 'NUMBER' ||
  102. d.type.toUpperCase() === 'DATE' )
  103. ) ||
  104. d.tagName.toUpperCase() === 'TEXTAREA') {
  105. doPrevent = d.readOnly || d.disabled;
  106. }
  107. else {
  108. doPrevent = true;
  109. }
  110. }
  111.  
  112. if (doPrevent) {
  113. event.preventDefault();
  114. }
  115. });
  116.  
  117.  
  118.  
  119.  
  120.  
  121. var divs = d3.selectAll("div")[0];
  122. // DANGER: some temperamental behavior with getting the correct div. be very cautious with anything that relies on his page architecture being a particular way.
  123. var myDiv;
  124. divs.forEach(function(d) {
  125. // just make this as descriptive as possible, then pray
  126. if (d.style.length == 1 && d.style[0] == "font-family") {
  127. myDiv = d;
  128. }
  129. });
  130. // myDiv = divs[divs.length];
  131. myDiv.id = "myDiv";
  132. // myDiv.innerText = "fuckled!";
  133. loadCanvas("myDiv");
  134.  
  135. loadCanvas_2("myDiv");
  136.  
  137.  
  138. canvas = document.getElementById("CursorLayer");
  139. ctx = canvas.getContext("2d");
  140.  
  141. canvas_2 = document.getElementById("CursorLayer_2");
  142. ctx_2 = canvas_2.getContext("2d");
  143.  
  144. var x = document.getElementsByTagName("INPUT");
  145. var y = [];
  146. var cnt2 = 0;
  147. for (var cnt = 0; cnt < x.length; cnt++) {
  148. if (x[cnt].type == "checkbox") y.push(x[cnt]);
  149. }
  150. if (y[4].checked) {
  151. ctx.fillStyle = "white";
  152. ctx_2.fillStyle = "white";
  153. }
  154. ctx.font = "30px Arial";
  155. ctx_2.font = "30px Arial";
  156. }
  157.  
  158. // probably shouldn't just do this 3 times
  159. var x = document.getElementsByTagName("INPUT");
  160. var y = [];
  161. var cnt2 = 0;
  162. for (var cnt = 0; cnt < x.length; cnt++) {
  163. if (x[cnt].type == "checkbox") y.push(x[cnt]);
  164. }
  165.  
  166. y[4].id = "dark_mode_evan";
  167. y[4].onclick = function() {
  168. if (y[4].checked) {
  169. ctx.fillStyle = "white";
  170. ctx_2.fillStyle = "white";
  171. } else {
  172. ctx.fillStyle = "black";
  173. ctx_2.fillStyle = "black";
  174. }
  175. }
  176.  
  177. // document.onload = function() {
  178. // $('input[type=checkbox]').change(
  179. // function(){
  180. // console.log("f");
  181. // if (this.checked && this.id == "dark_mode_evan") {
  182. // console.log("white");
  183. // ctx.fillStyle = "white";
  184. // } else if (this.id == "dark_mode_evan") {
  185. // console.log("black");
  186. // ctx.fillStyle = "black";
  187. // }
  188. // });
  189. // }
  190.  
  191.  
  192. // document.body.innerHTML +='<svg width="100" height="100"></svg>';
  193. // document.getElementById("canvas").style.position = "absolute";
  194.  
  195. // document.getElementById("canvas").style.zIndex = "0";
  196.  
  197. window.onkeyup = function(e) {
  198. // console.log(e);
  199. var adder = 0;
  200. if (e.keyCode == 32 /*spacebar*/) {
  201. // $("#overlays").after("<div id='fuckle'></div>");
  202. // document.body.innerHTML +='<div id="fuckle" style="display:inline-block;"></div>';
  203.  
  204. // console.log(divs[0][72]);
  205.  
  206. // var myWindow = window.open("", "", "top=0, status='no', titlebar='no', left=0, width=200, height=100");
  207.  
  208.  
  209. // document.body.innerHTML +='<iframe id="injectedCanvas" style="position:absolute;width:100px;height:100px;opacity:0.3;z-index:-11111100;background:#000;"></iframe>';
  210.  
  211.  
  212.  
  213.  
  214. // ctx.fillText("Hello World",10,50);
  215.  
  216. // var canvas = document.getElementById("canvas");
  217. // var ctx = canvas.getContext("2d");
  218. // ctx.font = "30px Arial";
  219. // ctx.fillText("Hello World",10,50);
  220.  
  221. // var canvas = document.getElementById("layer2");
  222. // var ctx = canvas.getContext("2d");
  223. // ctx.font = "30px Arial";
  224. // ctx.fillText("Hello World",10,50);
  225. // var svg = d3.select("body").append("svg")
  226. // .attr("width", 100)
  227. // .attr("height", 100)
  228. // .attr("id","fuckle");
  229. // // .attr("z-index",5000000000000000);
  230.  
  231.  
  232. // svg.append("rect")
  233. // .attr("x", 10)
  234. // .attr("y", 10)
  235. // .attr("width", 500)
  236. // .attr("height", 500);
  237.  
  238. overlay_style = $("#overlays").css("display");
  239. // put in console logs for every bool everywhere so users can help you debug (and so you can debug). something's off; sometimes the timer doesn't play at all.
  240. if (secs <= 0 && /*overlay_style == "none"*/in_game) {
  241. secs = 30;
  242. timing = true;
  243. countDown();
  244. }
  245.  
  246. } else if (e.keyCode >= 48 && e.keyCode <= 57/*any number*/) {
  247. if (in_game && secs > 0) {
  248. mass_input += (parseInt(e.keyCode) - 48);
  249. }
  250. } else if (e.keyCode == 13 /*enter*/) {
  251. if (in_game && secs > 0) {
  252. if (buffered) {
  253. secs -= k * parseInt(buffer);
  254. secs += k * parseInt(mass_input);
  255. } else {
  256. secs += k * parseInt(mass_input);
  257. }
  258. buffer = mass_input;
  259. buffered = true;
  260. // write buffer to canvas
  261.  
  262. ctx_2.clearRect(0, 0, canvas_2.width, canvas_2.height);
  263.  
  264. // if (/*secs > 0*/timing) {
  265. ctx_2.fillText(parseInt(mass_input), 10, 50);
  266. // }
  267. // ctx_2.translate(0,100);
  268. mass_input = "";
  269.  
  270. // var margin = {top: 50, bottom: 10, left: 300, right: 40};
  271. // var width = 1650 - margin.left - margin.right;
  272. // var height = 400 - margin.top - margin.bottom;
  273.  
  274. // var timer_box = d3.select("body").append("svg")
  275. // .attr("id", "box")
  276. // .attr("width", width)
  277. // .attr("height", height);
  278.  
  279. // timer_box.append("rect")
  280. // .attr("x", 1000)
  281. // .attr("y", 200)
  282. // .attr("width", 200)
  283. // .attr("height", 200)
  284. // .attr("fill", "black")
  285. // .attr("fill-opacity", .75);
  286. }
  287. } else if (e.keyCode == 83 /*s*/ ) {
  288. for (i = 0; i < 8; i++) {
  289.  
  290. setTimeout(function(){triggerKeyEvent(87);
  291. triggerKeyEvent(87, "keyup");
  292. }, i*100);
  293.  
  294. }
  295. function triggerKeyEvent(charCode, eventName) {
  296. eventName = eventName || "keydown";
  297. var s = document.createElement("script");
  298. s.textContent = "window.agarioTriggerKey(" + charCode + ", \"" + eventName + "\");";
  299. (document.head || document.documentElement).appendChild(s);
  300. s.parentNode.removeChild(s);
  301. }
  302. function addTriggerKeyFunction() {
  303. var s = document.createElement("script");
  304. s.textContent = "window.agarioTriggerKey = " + function(charCode, eventName) {
  305. var event = document.createEvent("KeyboardEvents");
  306. event.initKeyboardEvent(
  307. eventName
  308. );
  309. var getterCode = {
  310. get: function() {
  311. return charCode;
  312. }
  313. };
  314. Object.defineProperties(event, {
  315. keyCode: getterCode
  316. });
  317.  
  318. window.dispatchEvent(event);
  319. };
  320. (document.head || document.documentElement).appendChild(s);
  321. s.parentNode.removeChild(s);
  322. }
  323. addTriggerKeyFunction();
  324. /*
  325. console.log('s');
  326. var oEvent = document.createEvent('KeyboardEvent');
  327. var k = 87;
  328. // Chromium Hack
  329. Object.defineProperty(oEvent, 'keyCode', {
  330. get: function() {
  331. return this.keyCodeVal;
  332. }
  333. });
  334. Object.defineProperty(oEvent, 'which', {
  335. get: function() {
  336. return this.keyCodeVal;
  337. }
  338. });
  339.  
  340. if (oEvent.initKeyboardEvent) {
  341. oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, false, false, false, false, k, k);
  342. } else {
  343. oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, k, 0);
  344. }
  345.  
  346. oEvent.keyCodeVal = k;
  347.  
  348. if (oEvent.keyCode !== k) {
  349. console.log("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
  350. }
  351. console.log(oEvent);
  352. document.dispatchEvent(oEvent);
  353.  
  354. var oEvent = document.createEvent('KeyboardEvent');
  355. // Chromium Hack
  356. Object.defineProperty(oEvent, 'keyCode', {
  357. get: function() {
  358. return this.keyCodeVal;
  359. }
  360. });
  361. Object.defineProperty(oEvent, 'which', {
  362. get: function() {
  363. return this.keyCodeVal;
  364. }
  365. });
  366.  
  367. if (oEvent.initKeyboardEvent) {
  368. oEvent.initKeyboardEvent("keyup", true, true, document.defaultView, false, false, false, false, k, k);
  369. } else {
  370. oEvent.initKeyEvent("keyup", true, true, document.defaultView, false, false, false, false, k, 0);
  371. }
  372.  
  373. oEvent.keyCodeVal = k;
  374.  
  375. if (oEvent.keyCode !== k) {
  376. console.log("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
  377. }
  378. console.log(oEvent);
  379. document.dispatchEvent(oEvent);
  380. */
  381. } else if (e.keyCode == 82 /*r*/) {
  382.  
  383. if (in_game) {
  384. secs = 0;
  385. reset = true;
  386. timing = false;
  387. buffered = false;
  388. }
  389. }
  390. }
  391.  
  392.  
  393.  
  394.  
  395. var x = document.getElementsByTagName("INPUT");
  396. var y = [];
  397. var cnt2 = 0;
  398. for (var cnt = 0; cnt < x.length; cnt++) {
  399. if (x[cnt].type == "checkbox") y.push(x[cnt]);
  400. }
  401.  
  402. var modes = {
  403. "FFA": "",
  404. "Teams": ":teams",
  405. "Experimental": ":experimental",
  406. "Party": ":party"
  407. }
  408.  
  409. chrome.storage.sync.get(['skins',
  410. 'names',
  411. 'colors',
  412. 'mass',
  413. 'theme',
  414. 'stats', 'nick'//, 'region', 'mode'
  415. ], function(response) {
  416. if (response.skins) {
  417. y[0].click();
  418. }
  419. if (response.names) {
  420. y[1].click();
  421. }
  422. if (response.colors) {
  423. y[2].click();
  424. }
  425. // != false in case it doesn't exist, for new user
  426. // don't be a dummy m8. you had the extension running twice
  427. if (response.mass != false) {
  428.  
  429. y[3].click();
  430. }
  431. if (response.theme) {
  432. y[4].click();
  433. }
  434. if (response.stats) {
  435. y[5].click();
  436. }
  437.  
  438. // "kill" hotkey could be useful
  439. // toggle extension is probably good practice
  440.  
  441. // textbox and dropdowns
  442. // I don't think mode or region are working
  443. if (response.nick /*!= "undefined"*/ ) {
  444. document.getElementById('nick').value = response.nick;
  445. }
  446. if (response.region /*!= "undefined"*/ && response.region != "Auto") {
  447. document.getElementById('region').value = response.region;
  448. }
  449. if (response.mode /*!= "undefined"*/ ) {
  450. document.getElementById('gamemode').value = modes[response.mode];
  451. // console.log(document.getElementById('gamemode'));
  452. // console.log(document.getElementById("helloContainer"));
  453. $("#helloContainer").attr("data-gamemode", modes[response.mode]);
  454. // console.log(document.getElementById("helloContainer"));
  455.  
  456. }
  457. });
  458.  
  459.  
  460.  
  461. // y[3].dispatchEvent(new Event('change', { 'bubbles': true }))
  462. // console.log("fail");
  463.  
  464. // console.log(y[3]);
  465.  
  466. // console.log(y[3].checked);
  467. // y[3].checked = true;
  468. // y[3].checked = false;
  469. // y[3].checked = true;
  470. // console.log(y[3].checked);
  471.  
  472.  
  473. // y[3].onchange();
  474.  
  475. // var iframe = document.createElement('iframe');
  476. // // change border here for debugging
  477. // iframe.frameBorder = 0;
  478. // iframe.width = "500px";
  479. // iframe.height = "250px";
  480. // iframe.id = "iframe";
  481.  
  482. function rounded(s) {
  483. return Math.round(s * 100) / 100
  484. }
  485.  
  486.  
  487. // console.log(rounded(0));
  488. // console.log(rounded(1));
  489. // console.log(rounded(11));
  490. // console.log(rounded(111));
  491. // console.log(rounded(1.2345888));
  492. // console.log(rounded(12.345888));
  493. // console.log(rounded(123.456888));
  494. // console.log(rounded(1234.5678888));
  495.  
  496. var observer = new MutationObserver(function(mutations) {
  497. mutations.forEach(function(mutationRecord) {
  498. // console.log(mutationRecord);
  499. // check status of overlay div or something because t/f changes a million times when game ends so idk if that's robust
  500. secs = 0;
  501. buffered = false;
  502. // timing = false;
  503. ctx.clearRect(0, 0, canvas.width, canvas.height);
  504. ctx_2.clearRect(0, 0, canvas_2.width, canvas_2.height);
  505. overlay_style = $("#overlays").css("display");
  506. if (overlay_style != "none") {
  507. in_game = false;
  508. } else {
  509. in_game = true;
  510. }
  511. // console.log(in_game);
  512. });
  513. });
  514.  
  515. var target = document.getElementById('overlays');
  516. observer.observe(target, {
  517. attributes: true,
  518. attributeFilter: ['style']
  519. });
  520.  
  521. function countDown() {
  522.  
  523. // var canvas = document.getElementById("CursorLayer");
  524. // var ctx = canvas.getContext("2d");
  525. // ctx.font = "30px Arial";
  526.  
  527. // unexpected behavior if you start the game before the page has finished loading
  528.  
  529. // erase canvas
  530. ctx.clearRect(0, 0, canvas.width, canvas.height);
  531. // ctx_2.clearRect(0, 0, canvas_2.width, canvas_2.height);
  532.  
  533.  
  534. if (secs > 0) {
  535. ctx.fillText(rounded(secs), 10, 50);
  536. } else {
  537. if (!reset && in_game) {
  538. ctx.fillText("MERGE", 10, 50);
  539. setTimeout(function() {
  540. ctx.clearRect(0, 0, canvas.width, canvas.height);
  541. ctx_2.clearRect(0, 0, canvas_2.width, canvas_2.height);
  542.  
  543. }, 3000)
  544. } else {
  545. ctx.clearRect(0, 0, canvas.width, canvas.height);
  546. ctx_2.clearRect(0, 0, canvas_2.width, canvas_2.height);
  547.  
  548.  
  549. }
  550. buffered = false;
  551. timing = false;
  552. reset = false;
  553. }
  554. if (secs >= 1) {
  555. setTimeout(function() {
  556. secs--;
  557. countDown(secs);
  558. }, 1000)
  559. } else if (secs > 0) {
  560. setTimeout(function() {
  561. secs = 0;
  562. countDown(secs);
  563. }, secs * 1000)
  564. }
  565.  
  566. }
  567.  
  568.  
  569.  
  570.  
  571. // create svg canvas
  572. // window.onload = function() {
  573. // console.log("loaded");
  574.  
  575. // }
  576.  
  577. // chrome.runtime.onConnect.addListener(function(port) {
  578. // port.onMessage.addListener(function(msg) {
  579. // console.log(msg);
  580. // port.postMessage({counter: msg.counter+1});
  581. // });
  582. // });
  583.  
  584. // chrome.extension.onRequest.addListener(
  585. // function(request, sender, sendResponse) {
  586. // sendResponse({counter: request.counter+1});
  587. // });
  588.  
  589.  
  590.  
  591.  
  592. // http://pastie.org/10199526
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement