Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var form = null;
  2. var position;
  3. var cursor;
  4. var currentURL = "";
  5. var isListening = 0;
  6. check();
  7.  
  8. window.setInterval("check();", 250);
  9.  
  10. chrome.extension.onConnect.addListener( function(port)
  11. {
  12.     port.onMessage.addListener(function (string)
  13.     {
  14.         if(string != "")
  15.         {
  16.             form.value = form.value.substring(0, position+1) + string + form.value.substring(cursor+1,form.value.length);
  17.             form.setSelectionRange(position+1+string.length,position+1+string.length);
  18.         }
  19.     });
  20. });
  21.  
  22. function keydown(event)
  23. {
  24.     if(event.which ==  32 && event.ctrlKey)
  25.         parse(form.value);
  26. }
  27.  
  28. function check()
  29. {
  30.     if(form == null)
  31.     {
  32.         form = document.getElementById("tickets_reply");
  33.         if(form != null)
  34.         {
  35.             if(isListening)
  36.                 form.removeEventListener("keypress",keydown);
  37.             form.addEventListener("keypress",keydown);
  38.             isListening = 1;
  39.         }
  40.         currentURL = window.location.href;
  41.     }
  42.     if(window.location.href != currentURL)
  43.     {
  44.         form = null;
  45.         isListening = 0;
  46.     }
  47. }
  48.  
  49. function parse(string)
  50. {
  51.     cursor = form.selectionStart;
  52.     string = string.substring(0,cursor);
  53.     position = Math.max(
  54.     string.lastIndexOf("."),
  55.     string.lastIndexOf("\n"),
  56.     string.lastIndexOf("?"),
  57.     string.lastIndexOf("!"));
  58.     while(string[position+1] == ' ')
  59.         position++;
  60.     string = string.substring(position + 1,string.length);
  61.     var port = chrome.runtime.connect();
  62.     port.postMessage(string);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement