Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  
  2.  *  Copyright (C) 2013  AJ Ribeiro
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.  
  16. */
  17.  
  18. var rmousedown=false, moved=false, lmousedown=false
  19. var rocker=false, trail=false
  20. var mx,my,nx,ny,lx,ly,phi
  21. var move="", omove=""
  22. var pi =3.14159
  23. var suppress=1
  24. var canvas, myGests, ginv
  25. var link, ls, myColor="red", myWidth=3
  26. var loaded=false
  27. var rocked=false
  28. var link=null
  29.  
  30. function invertHash(hash)
  31. {
  32.     inv = {}
  33.     for(key in hash)
  34.         inv[hash[key]] = key
  35.     return inv
  36. }
  37.  
  38. function createCanvas()
  39. {
  40.     canvas = document.createElement('canvas');
  41.     canvas.id = "gestCanvas"
  42.     canvas.style.width=document.body.scrollWidth
  43.     canvas.style.height=document.body.scrollHeight
  44.     canvas.width=window.document.body.scrollWidth
  45.     canvas.height=window.document.body.scrollHeight    
  46.     canvas.style.left="0px";
  47.     canvas.style.top="0px";
  48.     canvas.style.overflow = 'visible';
  49.     canvas.style.position = 'absolute';
  50.     canvas.style.zIndex="10000"
  51. }
  52. function draw(x,y){
  53.     var ctx = document.getElementById('gestCanvas').getContext('2d');
  54.     ctx.beginPath();
  55.     ctx.strokeStyle = myColor
  56.     ctx.lineWidth = myWidth
  57.     ctx.moveTo(lx,ly);
  58.     ctx.lineTo(x,y);
  59.     ctx.stroke()
  60.     lx=x
  61.     ly=y
  62. }
  63.  
  64. document.onmousedown = function(event){
  65.     // console.log('down'+event.which+rmousedown+lmousedown+rocker)
  66.     if(event.which == 1){
  67.         lmousedown = true
  68.     }
  69.     else if(event.which == 3){
  70.         rmousedown = true
  71.     }
  72.  
  73.     //leftrock
  74.     if(event.which == 1 && rmousedown && suppress && rocker){
  75.         if(! loaded){
  76.             loadOptions()
  77.             loaded=true
  78.         }
  79.         move = 'back'
  80.         rocked = true
  81.         // console.log(rocked)
  82.         exeRock()
  83.     }
  84.  
  85.     // console.log('rmousedown '+suppress)
  86.     //right mouse click
  87.     else if(event.which == 3 && suppress){
  88.         if(! loaded){
  89.             loadOptions()
  90.             loaded=true
  91.         }
  92.         if(lmousedown && rocker){
  93.             if(! loaded){
  94.                 loadOptions()
  95.                 loaded=true
  96.             }
  97.             move = 'forward'
  98.             rocked = true
  99.             // console.log(rocked)
  100.             exeRock()
  101.         }
  102.         else{
  103.             my = event.pageX;
  104.             mx = event.pageY;
  105.             lx = my
  106.             ly = mx
  107.             move = ""
  108.             omove=""
  109.             moved=false
  110.             if(event.target.href){
  111.                 link = event.target.href
  112.             }
  113.             else if(event.target.parentElement.href){
  114.                 link = event.target.parentElement.href
  115.             }
  116.             else{
  117.                 link = null
  118.             }
  119.         }
  120.     }
  121.  
  122. };
  123.  
  124. document.onmousemove = function(event)
  125. {
  126.     // console.log(rmousedown)
  127.     //track the mouse if we are holding the right button
  128.     if(rmousedown)
  129.     {
  130.         ny = event.pageX;
  131.         nx = event.pageY;
  132.         var r = Math.sqrt(Math.pow(nx-mx,2)+Math.pow(ny-my,2))
  133.         if(r > 16)
  134.         {
  135.             phi = Math.atan2(ny-my,nx-mx)
  136.             if(phi < 0) phi += 2.*pi
  137.             if(phi >= pi/4. && phi < 3.*pi/4.)
  138.                 var tmove="R"
  139.             else if(phi >= 3.*pi/4. && phi < 5.*pi/4.)
  140.                 var tmove="U"
  141.             else if(phi >= 5.*pi/4. && phi < 7.*pi/4.)
  142.                 var tmove="L"
  143.             else if(phi >= 7.*pi/4. || phi < pi/4.)
  144.                 var tmove="D"
  145.             if(tmove != omove)
  146.             {
  147.                 move += tmove
  148.                 omove = tmove
  149.             }
  150.             if(moved == false)
  151.             {
  152.                 // console.log('making canvas')
  153.                 createCanvas()
  154.                 document.body.appendChild(canvas);
  155.             }
  156.             moved=true
  157.             // console.log('indraw'+trail)
  158.  
  159.             if(trail){
  160.                 // console.log('draw')
  161.                 draw(ny,nx)
  162.             }
  163.  
  164.             mx = nx
  165.             my = ny
  166.         }
  167.     }
  168. };
  169.  
  170.  
  171. document.onmouseup = function(event)
  172. {
  173.     var x = document.getElementById("gestCanvas");
  174.     if(x)
  175.         x.parentNode.removeChild(x);
  176.  
  177.     // console.log('up'+event.which)
  178.     // console.log('mouse is up '+suppress)
  179.     if(event.which == 1)
  180.         lmousedown = false
  181.  
  182.     //right mouse release
  183.     if(event.which == 3){
  184.         // console.log('suppress is '+suppress)
  185.         rmousedown=false
  186.         // console.log('rmousedown'+rmousedown)
  187.         if(moved){
  188.             exeFunc()
  189.         }
  190.         else if(rocked){
  191.             rocked = false
  192.         }
  193.         else{
  194.             --suppress
  195.             // console.log('no move '+suppress+rmousedown)
  196.             $('#target').rmousedown(which=3);
  197.             $('#target').rmouseup(which=3);
  198.         }
  199.     }
  200. };
  201. function exeRock()
  202. {
  203.     action = move
  204.     if(action == "back"){
  205.         window.history.back()
  206.     }
  207.     else if(action == "forward"){
  208.         window.history.forward()
  209.     }
  210. }
  211.  
  212. function exeFunc()
  213. {
  214.     // console.log('exeFunc '+move)
  215.     if(ginv[move])
  216.     {
  217.         action = ginv[move]
  218.         if(action == "back")
  219.         {
  220.             window.history.back()
  221.         }
  222.         else if(action == "forward")
  223.         {
  224.             window.history.forward()
  225.         }
  226.         else if(action == "newtab")
  227.         {
  228.             if(link == null){
  229.                 chrome.extension.sendMessage({msg: "newtab"},
  230.                     function(response)
  231.                     {
  232.                         if(response != null)
  233.                             console.log(response.resp);
  234.                         else
  235.                         {
  236.                             console.log('problem executing open tab')
  237.                             if(chrome.extension.lastError)
  238.                                 console.log(chrome.extension.lastError.message)
  239.                         }
  240.                     });
  241.             }
  242.             else{
  243.                 window.open(link)
  244.             }
  245.         }
  246.         else if(action == "closetab"){
  247.             chrome.extension.sendMessage({msg: "closetab"});
  248.         }
  249.         else if(action == "lasttab"){
  250.             chrome.extension.sendMessage({msg: "lasttab"});
  251.         }
  252.         else if(action == "reloadall"){
  253.             chrome.extension.sendMessage({msg: "reloadall"});
  254.         }
  255.  
  256.         else if(action == "closeall"){
  257.             chrome.extension.sendMessage({msg: "closeall"});
  258.         }
  259.  
  260.         else if(action == "nexttab") {
  261.             chrome.extension.sendMessage({msg: "nexttab"});
  262.         }
  263.  
  264.         else if(action == "prevtab"){
  265.             chrome.extension.sendMessage({msg: "prevtab"});
  266.         }
  267.  
  268.         else if(action == "closeback"){
  269.             chrome.extension.sendMessage({msg: "closeback"});
  270.         }
  271.  
  272.  
  273.         else if(action == "scrolltop")
  274.             window.scrollTo(0,0)
  275.  
  276.         else if(action == "scrollbottom")
  277.             window.scrollTo(0,document.body.scrollHeight)
  278.  
  279.         else if(action == "reload")
  280.             window.location.reload()
  281.  
  282.         else if(action == "stop")
  283.             window.stop()
  284.  
  285.     }
  286. }
  287.  
  288.  
  289. document.oncontextmenu = function()
  290. {
  291.     // console.log('ctx menu suppress is '+suppress)
  292.     if(suppress)
  293.         return false
  294.     else{
  295.         // console.log("open it");
  296.         suppress++
  297.         var x = document.getElementById("gestCanvas");
  298.         if(x)
  299.             x.parentNode.removeChild(x);
  300.         rmousedown = false
  301.         return true
  302.     }
  303. };
  304.  
  305. function loadOptions(name)
  306. {
  307.     chrome.extension.sendMessage({msg: "colorCode"},
  308.         function(response) {
  309.             if(response){
  310.                 // console.log('color'+response.resp)
  311.                 myColor = response.resp
  312.             }
  313.             // else
  314.             //     console.log('error getting colorCode')
  315.         });
  316.     chrome.extension.sendMessage({msg: "width"},
  317.         function(response) {
  318.             if(response){
  319.                 myWidth = response.resp
  320.                 // console.log('width '+myWidth)
  321.             }
  322.             // else
  323.             //     console.log('error getting width')
  324.         });
  325.     chrome.extension.sendMessage({msg: "gests"},
  326.         function(response)
  327.         {
  328.             if(response)
  329.                 myGests = response.resp
  330.             ginv = invertHash(myGests)
  331.         });
  332.  
  333.     chrome.extension.sendMessage({msg: "rocker"},
  334.         function(response)
  335.         {
  336.             if(response)
  337.                 rocker = response.resp
  338.             if(rocker == 'true') rocker = true
  339.             else rocker = false
  340.         });
  341.  
  342.     chrome.extension.sendMessage({msg: "trail"},
  343.         function(response)
  344.         {
  345.             if(response)
  346.                 trail = response.resp
  347.             if(trail == 'true') trail = true
  348.             else trail = false
  349.         });
  350. }
  351.  
  352. document.addEventListener('DOMContentLoaded', loadOptions);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement