Advertisement
chfoo0

TPP touchscreen input script WIP

Dec 20th, 2015
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         TPP Touchscreen Input Assist (Work in progress)
  3. // @namespace    chfoo/tppinputassist
  4. // @version      0.1
  5. // @description  Touchscreen coordinate tap overlay for inputting into Twitch chat
  6. // @author       Christopher Foo
  7. // @match        http://www.twitch.tv/*
  8. // @grant        none
  9. // ==/UserScript==
  10. /* jshint -W097 */
  11.  
  12.  
  13. (function (console, $global) { "use strict";
  14. function $extend(from, fields) {
  15.     function Inherit() {} Inherit.prototype = from; var proto = new Inherit();
  16.     for (var name in fields) proto[name] = fields[name];
  17.     if( fields.toString !== Object.prototype.toString ) proto.toString = fields.toString;
  18.     return proto;
  19. }
  20. var HxOverrides = function() { };
  21. HxOverrides.__name__ = true;
  22. HxOverrides.cca = function(s,index) {
  23.     var x = s.charCodeAt(index);
  24.     if(x != x) return undefined;
  25.     return x;
  26. };
  27. Math.__name__ = true;
  28. var Reflect = function() { };
  29. Reflect.__name__ = true;
  30. Reflect.hasField = function(o,field) {
  31.     return Object.prototype.hasOwnProperty.call(o,field);
  32. };
  33. Reflect.setField = function(o,field,value) {
  34.     o[field] = value;
  35. };
  36. var Std = function() { };
  37. Std.__name__ = true;
  38. Std.string = function(s) {
  39.     return js_Boot.__string_rec(s,"");
  40. };
  41. Std.parseInt = function(x) {
  42.     var v = parseInt(x,10);
  43.     if(v == 0 && (HxOverrides.cca(x,1) == 120 || HxOverrides.cca(x,1) == 88)) v = parseInt(x);
  44.     if(isNaN(v)) return null;
  45.     return v;
  46. };
  47. var js__$Boot_HaxeError = function(val) {
  48.     Error.call(this);
  49.     this.val = val;
  50.     this.message = String(val);
  51.     if(Error.captureStackTrace) Error.captureStackTrace(this,js__$Boot_HaxeError);
  52. };
  53. js__$Boot_HaxeError.__name__ = true;
  54. js__$Boot_HaxeError.__super__ = Error;
  55. js__$Boot_HaxeError.prototype = $extend(Error.prototype,{
  56.     __class__: js__$Boot_HaxeError
  57. });
  58. var js_Boot = function() { };
  59. js_Boot.__name__ = true;
  60. js_Boot.getClass = function(o) {
  61.     if((o instanceof Array) && o.__enum__ == null) return Array; else {
  62.         var cl = o.__class__;
  63.         if(cl != null) return cl;
  64.         var name = js_Boot.__nativeClassName(o);
  65.         if(name != null) return js_Boot.__resolveNativeClass(name);
  66.         return null;
  67.     }
  68. };
  69. js_Boot.__string_rec = function(o,s) {
  70.     if(o == null) return "null";
  71.     if(s.length >= 5) return "<...>";
  72.     var t = typeof(o);
  73.     if(t == "function" && (o.__name__ || o.__ename__)) t = "object";
  74.     switch(t) {
  75.     case "object":
  76.         if(o instanceof Array) {
  77.             if(o.__enum__) {
  78.                 if(o.length == 2) return o[0];
  79.                 var str2 = o[0] + "(";
  80.                 s += "\t";
  81.                 var _g1 = 2;
  82.                 var _g = o.length;
  83.                 while(_g1 < _g) {
  84.                     var i1 = _g1++;
  85.                     if(i1 != 2) str2 += "," + js_Boot.__string_rec(o[i1],s); else str2 += js_Boot.__string_rec(o[i1],s);
  86.                 }
  87.                 return str2 + ")";
  88.             }
  89.             var l = o.length;
  90.             var i;
  91.             var str1 = "[";
  92.             s += "\t";
  93.             var _g2 = 0;
  94.             while(_g2 < l) {
  95.                 var i2 = _g2++;
  96.                 str1 += (i2 > 0?",":"") + js_Boot.__string_rec(o[i2],s);
  97.             }
  98.             str1 += "]";
  99.             return str1;
  100.         }
  101.         var tostr;
  102.         try {
  103.             tostr = o.toString;
  104.         } catch( e ) {
  105.             if (e instanceof js__$Boot_HaxeError) e = e.val;
  106.             return "???";
  107.         }
  108.         if(tostr != null && tostr != Object.toString && typeof(tostr) == "function") {
  109.             var s2 = o.toString();
  110.             if(s2 != "[object Object]") return s2;
  111.         }
  112.         var k = null;
  113.         var str = "{\n";
  114.         s += "\t";
  115.         var hasp = o.hasOwnProperty != null;
  116.         for( var k in o ) {
  117.         if(hasp && !o.hasOwnProperty(k)) {
  118.             continue;
  119.         }
  120.         if(k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__") {
  121.             continue;
  122.         }
  123.         if(str.length != 2) str += ", \n";
  124.         str += s + k + " : " + js_Boot.__string_rec(o[k],s);
  125.         }
  126.         s = s.substring(1);
  127.         str += "\n" + s + "}";
  128.         return str;
  129.     case "function":
  130.         return "<function>";
  131.     case "string":
  132.         return o;
  133.     default:
  134.         return String(o);
  135.     }
  136. };
  137. js_Boot.__interfLoop = function(cc,cl) {
  138.     if(cc == null) return false;
  139.     if(cc == cl) return true;
  140.     var intf = cc.__interfaces__;
  141.     if(intf != null) {
  142.         var _g1 = 0;
  143.         var _g = intf.length;
  144.         while(_g1 < _g) {
  145.             var i = _g1++;
  146.             var i1 = intf[i];
  147.             if(i1 == cl || js_Boot.__interfLoop(i1,cl)) return true;
  148.         }
  149.     }
  150.     return js_Boot.__interfLoop(cc.__super__,cl);
  151. };
  152. js_Boot.__instanceof = function(o,cl) {
  153.     if(cl == null) return false;
  154.     switch(cl) {
  155.     case Int:
  156.         return (o|0) === o;
  157.     case Float:
  158.         return typeof(o) == "number";
  159.     case Bool:
  160.         return typeof(o) == "boolean";
  161.     case String:
  162.         return typeof(o) == "string";
  163.     case Array:
  164.         return (o instanceof Array) && o.__enum__ == null;
  165.     case Dynamic:
  166.         return true;
  167.     default:
  168.         if(o != null) {
  169.             if(typeof(cl) == "function") {
  170.                 if(o instanceof cl) return true;
  171.                 if(js_Boot.__interfLoop(js_Boot.getClass(o),cl)) return true;
  172.             } else if(typeof(cl) == "object" && js_Boot.__isNativeObj(cl)) {
  173.                 if(o instanceof cl) return true;
  174.             }
  175.         } else return false;
  176.         if(cl == Class && o.__name__ != null) return true;
  177.         if(cl == Enum && o.__ename__ != null) return true;
  178.         return o.__enum__ == cl;
  179.     }
  180. };
  181. js_Boot.__cast = function(o,t) {
  182.     if(js_Boot.__instanceof(o,t)) return o; else throw new js__$Boot_HaxeError("Cannot cast " + Std.string(o) + " to " + Std.string(t));
  183. };
  184. js_Boot.__nativeClassName = function(o) {
  185.     var name = js_Boot.__toStr.call(o).slice(8,-1);
  186.     if(name == "Object" || name == "Function" || name == "Math" || name == "JSON") return null;
  187.     return name;
  188. };
  189. js_Boot.__isNativeObj = function(o) {
  190.     return js_Boot.__nativeClassName(o) != null;
  191. };
  192. js_Boot.__resolveNativeClass = function(name) {
  193.     return $global[name];
  194. };
  195. var tppinputassist_ElementNotFoundError = function() {
  196. };
  197. tppinputassist_ElementNotFoundError.__name__ = true;
  198. tppinputassist_ElementNotFoundError.prototype = {
  199.     __class__: tppinputassist_ElementNotFoundError
  200. };
  201. var tppinputassist_App = function() {
  202.     this.touchscreenHeight = 240;
  203.     this.touchscreenWidth = 320;
  204. };
  205. tppinputassist_App.__name__ = true;
  206. tppinputassist_App.prototype = {
  207.     run: function() {
  208.         var _g = this;
  209.         window.addEventListener("load",function() {
  210.             console.log("Page loaded, trying install script");
  211.             _g.jamJQueryIn();
  212.         });
  213.     }
  214.     ,jamJQueryIn: function() {
  215.         var _g = this;
  216.         js.JQuery.getScript("https://code.jquery.com/ui/1.11.4/jquery-ui.min.js",function() {
  217.             _g.installSettingsButton();
  218.         });
  219.         var div;
  220.         div = js_Boot.__cast(window.document.createElement("div") , HTMLDivElement);
  221.         div.innerHTML = "\n            <link rel='stylesheet' href='https://code.jquery.com/ui/1.11.4/themes/dark-hive/jquery-ui.css' type='text/css'>\n        ";
  222.         window.document.body.appendChild(div);
  223.     }
  224.     ,installSettingsButton: function() {
  225.         var _g = this;
  226.         var buttonContainer = window.document.querySelector(".chat-buttons-container");
  227.         this.throwIfNull(buttonContainer);
  228.         var enableElement;
  229.         enableElement = js_Boot.__cast(window.document.createElement("a") , HTMLAnchorElement);
  230.         enableElement.textContent = "TPPInputAssist";
  231.         enableElement.href = "#";
  232.         enableElement.onclick = function(event) {
  233.             if(_g.settingsPanel == null) _g.install();
  234.             var jq = js.JQuery(_g.settingsPanel);
  235.             jq.dialog();
  236.             return false;
  237.         };
  238.         buttonContainer.appendChild(enableElement);
  239.     }
  240.     ,install: function() {
  241.         var element;
  242.         element = window.document.querySelector(".chat_text_input");
  243.         this.throwIfNull(element);
  244.         this.textarea = js_Boot.__cast(element , HTMLTextAreaElement);
  245.         this.installSettingsPanel();
  246.         this.installTouchscreenOverlay();
  247.     }
  248.     ,throwIfNull: function(element) {
  249.         if(element == null) throw new js__$Boot_HaxeError(new tppinputassist_ElementNotFoundError());
  250.     }
  251.     ,installSettingsPanel: function() {
  252.         var _g = this;
  253.         this.settingsPanel = js_Boot.__cast(window.document.createElement("div") , HTMLDivElement);
  254.         this.settingsPanel.style.display = "none";
  255.         this.settingsPanel.innerHTML = "\n            <input type=checkbox id=tpp_assist_enable_checkbox>\n            <label for=tpp_assist_enable_checkbox>Enable touchscreen</label>\n            <br>\n            Width: <input id=tpp_assist_width_input type=number min=0 value=320 style='width: 3em;'>\n            <br>\n            Height: <input id=tpp_assist_height_input type=number min=0 value=240 style='width: 3em;'>\n        ";
  256.         window.document.body.appendChild(this.settingsPanel);
  257.         var enableCheckbox;
  258.         enableCheckbox = js_Boot.__cast(window.document.getElementById("tpp_assist_enable_checkbox") , HTMLInputElement);
  259.         enableCheckbox.onclick = function(event) {
  260.             _g.showTouchscreenOverlay(enableCheckbox.checked);
  261.         };
  262.         var widthInput;
  263.         widthInput = js_Boot.__cast(window.document.getElementById("tpp_assist_width_input") , HTMLInputElement);
  264.         var heightInput;
  265.         heightInput = js_Boot.__cast(window.document.getElementById("tpp_assist_width_input") , HTMLInputElement);
  266.         widthInput.onchange = heightInput.onchange = function(event1) {
  267.             _g.touchscreenWidth = Std.parseInt(widthInput.value);
  268.             _g.touchscreenHeight = Std.parseInt(heightInput.value);
  269.         };
  270.     }
  271.     ,installTouchscreenOverlay: function() {
  272.         var _g = this;
  273.         this.touchScreenOverlay = js_Boot.__cast(window.document.createElement("div") , HTMLDivElement);
  274.         this.touchScreenOverlay.style.border = "0.1em solid grey";
  275.         this.touchScreenOverlay.style.zIndex = "99";
  276.         this.touchScreenOverlay.style.width = "100px";
  277.         this.touchScreenOverlay.style.height = "100px";
  278.         this.touchScreenOverlay.style.display = "none";
  279.         this.touchScreenOverlay.style.position = "absolute";
  280.         var dragHandle;
  281.         dragHandle = js_Boot.__cast(window.document.createElement("div") , HTMLDivElement);
  282.         this.touchScreenOverlay.appendChild(dragHandle);
  283.         dragHandle.style.border = "0.1em outset grey";
  284.         dragHandle.style.position = "relative";
  285.         dragHandle.style.top = "-0.5em";
  286.         dragHandle.style.left = "-0.5em";
  287.         dragHandle.style.background = "rgba(255, 255, 255, 0.5)";
  288.         dragHandle.style.width = "50%";
  289.         dragHandle.style.height = "1em";
  290.         dragHandle.style.cursor = "move";
  291.         var clickReceiver;
  292.         clickReceiver = js_Boot.__cast(window.document.createElement("div") , HTMLDivElement);
  293.         this.touchScreenOverlay.appendChild(clickReceiver);
  294.         clickReceiver.style.width = "100%";
  295.         clickReceiver.style.height = "100%";
  296.         window.document.body.appendChild(this.touchScreenOverlay);
  297.         js.JQuery(clickReceiver).click(function(event) {
  298.             var offset = js.JQuery(_g.touchScreenOverlay).offset();
  299.             var divWidth = js.JQuery(_g.touchScreenOverlay).width();
  300.             var divHeight = js.JQuery(_g.touchScreenOverlay).height();
  301.             var x = (event.pageX - offset.left) / divWidth * _g.touchscreenWidth | 0;
  302.             var y = (event.pageY - offset.top) / divHeight * _g.touchscreenHeight | 0;
  303.             js.JQuery(_g.textarea).focus().val("" + x + "," + y);
  304.         });
  305.         var jq = js.JQuery(this.touchScreenOverlay);
  306.         jq.draggable({ 'handle' : dragHandle}).resizable();
  307.     }
  308.     ,showTouchscreenOverlay: function(visible) {
  309.         if(visible) {
  310.             this.touchScreenOverlay.style.display = "block";
  311.             this.touchScreenOverlay.style.top = "50px";
  312.             this.touchScreenOverlay.style.left = "50px";
  313.         } else this.touchScreenOverlay.style.display = "none";
  314.     }
  315.     ,__class__: tppinputassist_App
  316. };
  317. var tppinputassist_Main = function() { };
  318. tppinputassist_Main.__name__ = true;
  319. tppinputassist_Main.main = function() {
  320.     if(Reflect.hasField(window.document,tppinputassist_Main.NAMESPACE)) return;
  321.     Reflect.setField(window.document,tppinputassist_Main.NAMESPACE,true);
  322.     var app = new tppinputassist_App();
  323.     app.run();
  324. };
  325. String.prototype.__class__ = String;
  326. String.__name__ = true;
  327. Array.__name__ = true;
  328. var Int = { __name__ : ["Int"]};
  329. var Dynamic = { __name__ : ["Dynamic"]};
  330. var Float = Number;
  331. Float.__name__ = ["Float"];
  332. var Bool = Boolean;
  333. Bool.__ename__ = ["Bool"];
  334. var Class = { __name__ : ["Class"]};
  335. var Enum = { };
  336. var q = window.jQuery;
  337. var js = js || {}
  338. js.JQuery = q;
  339. js_Boot.__toStr = {}.toString;
  340. tppinputassist_Main.NAMESPACE = "tppinputassist";
  341. tppinputassist_Main.main();
  342. })(typeof console != "undefined" ? console : {log:function(){}}, typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement