Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Chrome Gestures
  2. // This Script based on Gomita & Arc Cosine 's Script.
  3. // cf:
  4. //    http://www.xuldev.org/misc/script/MouseGestures.uc.js
  5. //    http://github.com/ArcCosine/userscript/raw/master/simple_guestures.user.js#
  6. // license http://0-oo.net/pryn/MIT_license.txt (The MIT license)
  7.  
  8. (function () {
  9.   if (this.ChromeGesture) return;
  10.  
  11.   var connection = {
  12.     postMessage: function (message, response) {
  13.       if (response) {
  14.         chrome.extension.sendMessage(message, response);
  15.       } else {
  16.         chrome.extension.sendMessage(message);
  17.       }
  18.     }
  19.   }
  20.   var Root, NotRoot;
  21.   var LOG_SEC = Math.log(1000);
  22.  
  23.   var ACTION = {
  24.     "back": function () {
  25.       history.back();
  26.     },
  27.     "fastback": function () {
  28.       history.go(-history.length + 1);
  29.     },
  30.     "forward": function () {
  31.       history.forward();
  32.     },
  33.     "reload": function () {
  34.       location.reload();
  35.     },
  36.     "cacheless reload": function () {
  37.       location.reload(true);
  38.     },
  39.     "reload all tabs": function () {
  40.       connection.postMessage('reload_all_tabs');
  41.     },
  42.     "stop": function () {
  43.       window.stop();
  44.     },
  45.     "go to parent dir": function () {
  46.       if (location.hash) {
  47.         location.href = location.pathname + (location.search ? '?' + location.search : '');
  48.       } else {
  49.         var paths = location.pathname.split('/');
  50.         var path = paths.pop();
  51.         if (!location.search && path === '') paths.pop();
  52.         location.href = paths.join('/') + '/';
  53.       }
  54.     },
  55.     "open new tab": function () {
  56.       connection.postMessage('open_tab');
  57.     },
  58.     "open new tab background": function () {
  59.       connection.postMessage('open_tab_background');
  60.     },
  61.     "open blank tab": function () {
  62.       connection.postMessage('open_blank_tab');
  63.     },
  64.     "open blank tab background": function () {
  65.       connection.postMessage('open_blank_tab_background');
  66.     },
  67.     "close this tab": function () {
  68.       connection.postMessage('close_tab');
  69.     },
  70.     "open new window": function () {
  71.       connection.postMessage('open_window');
  72.     },
  73.     "close this window": function () {
  74.       connection.postMessage('close_window');
  75.     },
  76.     "select right tab": function () {
  77.       connection.postMessage('right_tab');
  78.     },
  79.     "select left tab": function () {
  80.       connection.postMessage('left_tab');
  81.     },
  82.     "select last tab": function () {
  83.       connection.postMessage('last_tab');
  84.     },
  85.     "select first tab": function () {
  86.       connection.postMessage('first_tab');
  87.     },
  88.     "re-open closed tab": function () {
  89.       connection.postMessage('closed_tab');
  90.     },
  91.     "clone tab": function () {
  92.       connection.postMessage('clone_tab');
  93.     },
  94.     "close other tabs": function () {
  95.       connection.postMessage('close_other_tabs');
  96.     },
  97.     "close right tabs": function () {
  98.       connection.postMessage('close_right_tabs');
  99.     },
  100.     "close left tabs": function () {
  101.       connection.postMessage('close_left_tabs');
  102.     },
  103.     "pin this tab": function () {
  104.       connection.postMessage('pin_tab');
  105.     },
  106.     "unpin this tab": function () {
  107.       connection.postMessage('unpin_tab');
  108.     },
  109.     "toggle pin tab": function () {
  110.       connection.postMessage('toggle_pin_tab');
  111.     },
  112.     "scroll down": function (config) {
  113.       if (config.smooth_scroll) {
  114.         SmoothScroll(0, 100, 100);
  115.       } else {
  116.         window.scrollBy(0, 100);
  117.       }
  118.     },
  119.     "scroll up": function (config) {
  120.       if (config.smooth_scroll) {
  121.         SmoothScroll(0, -100, 100);
  122.       } else {
  123.         window.scrollBy(0, -100);
  124.       }
  125.     },
  126.     "scroll right": function (config) {
  127.       if (config.smooth_scroll) {
  128.         SmoothScroll(50, 0, 100);
  129.       } else {
  130.         window.scrollBy(50, 0);
  131.       }
  132.     },
  133.     "scroll left": function (config) {
  134.       if (config.smooth_scroll) {
  135.         SmoothScroll(-50, 0, 100);
  136.       } else {
  137.         window.scrollBy(-50, 0);
  138.       }
  139.     },
  140.     "scroll down half page": function (config) {
  141.       if (config.smooth_scroll) {
  142.         SmoothScroll(0, window.innerHeight / 2);
  143.       } else {
  144.         window.scrollBy(0, window.innerHeight / 2);
  145.       }
  146.     },
  147.     "scroll up half page": function (config) {
  148.       if (config.smooth_scroll) {
  149.         SmoothScroll(0, -window.innerHeight / 2);
  150.       } else {
  151.         window.scrollBy(0, -window.innerHeight / 2);
  152.       }
  153.     },
  154.     "scroll down full page": function (config) {
  155.       if (config.smooth_scroll) {
  156.         SmoothScroll(0, window.innerHeight * 0.9);
  157.       } else {
  158.         window.scrollBy(0, window.innerHeight * 0.9);
  159.       }
  160.     },
  161.     "scroll up full page": function (config) {
  162.       if (config.smooth_scroll) {
  163.         SmoothScroll(0, -window.innerHeight * 0.9);
  164.       } else {
  165.         window.scrollBy(0, -window.innerHeight * 0.9);
  166.       }
  167.     },
  168.     "scroll to top": function (config) {
  169.       if (config.smooth_scroll) {
  170.         SmoothScroll(0, -1 * Root.scrollHeight);
  171.       } else {
  172.         window.scrollBy(0, -1 * Root.scrollHeight);
  173.       }
  174.     },
  175.     "scroll to bottom": function (config) {
  176.       if (config.smooth_scroll) {
  177.         SmoothScroll(0, Root.scrollHeight);
  178.       } else {
  179.         window.scrollBy(0, Root.scrollHeight);
  180.       }
  181.     },
  182.     "open #1 in new tab": function (arg) {
  183.       if (arg.action && arg.action.args && arg.action.args[0]) {
  184.         connection.postMessage({action: 'open_tab', 'link': arg.action.args[0], foreground: true});
  185.       }
  186.     },
  187.     "open #1 in new tab background": function (arg) {
  188.       if (arg.action && arg.action.args && arg.action.args[0]) {
  189.         connection.postMessage({action: 'open_tab', 'link': arg.action.args[0]});
  190.       }
  191.     },
  192.     "go to #1": function (arg) {
  193.       if (arg.action && arg.action.args && arg.action.args[0]) {
  194.         var url = arg.action.args[0];
  195.         if (url.indexOf('javascript:') === 0) {
  196.           location.href = url;
  197.         } else {
  198.           connection.postMessage({action: 'goto', 'link': url});
  199.         }
  200.       }
  201.     },
  202.     "copy url": function (arg) {
  203.       connection.postMessage({action: 'copy', 'message': location.href});
  204.     },
  205.     "copy url and title": function (arg) {
  206.       connection.postMessage({action: 'copy', 'message': document.title + ' ' + location.href});
  207.     },
  208.     "copy url and title as html": function (arg) {
  209.       connection.postMessage({action: 'copy', 'message': '<a href="' + location.href + '">' + document.title + '</a>'});
  210.     },
  211.     "copy url and title by custom tag #1": function (arg) {
  212.       if (arg.action && arg.action.args && arg.action.args[0]) {
  213.         var format = arg.action.args[0];
  214.         var data = {
  215.           'URL': location.href,
  216.           'TITLE': document.title
  217.         };
  218.         var message = format.replace(/%(\w+)%/g, function (_, _1) {
  219.           return data[_1] || '';
  220.         });
  221.         connection.postMessage({
  222.           action: 'copy',
  223.           'message': message
  224.         });
  225.       }
  226.     },
  227.     "run script #1": function (arg) {
  228.       if (arg.action && arg.action.args && arg.action.args[0]) {
  229.         var script = arg.action.args[0];
  230.         try {
  231.           eval(script);
  232.         } catch (e) {
  233.           alert(e);
  234.         }
  235.       }
  236.     },
  237.     "open link in new tab": function (arg) {
  238.       var link = $X('ancestor-or-self::a', GM.target)[0] || false;
  239.       if (link && link.href && link.href.indexOf('javascript:') !== 0) {
  240.         connection.postMessage({action: 'open_tab', 'link': link.href, foreground: true});
  241.       }
  242.     },
  243.     "open link in new tab background": function (arg) {
  244.       var link = $X('ancestor-or-self::a', GM.target)[0] || false;
  245.       if (link && link.href && link.href.indexOf('javascript:') !== 0) {
  246.         connection.postMessage({action: 'open_tab', 'link': link.href});
  247.       }
  248.     },
  249.     "config": function () {
  250.       connection.postMessage('config');
  251.     }
  252.   };
  253.   var LINK_ACTION = {
  254.     "no action": function () {
  255.     },
  256.     "open in new tab": function (arg) {
  257.       if (arg.target) {
  258.         connection.postMessage({action: 'open_tab', 'link': arg.target.href, foreground: true});
  259.       }
  260.     },
  261.     "open in background tab": function (arg) {
  262.       if (arg.target) {
  263.         connection.postMessage({action: 'open_tab', 'link': arg.target.href});
  264.       }
  265.     },
  266.     "open in new window": function (arg) {
  267.       if (arg.target) {
  268.         connection.postMessage({action: 'open_window', 'link': arg.target.href});
  269.       }
  270.     },
  271.     "copy text": function (arg) {
  272.       if (arg.target) {
  273.         connection.postMessage({action: 'copy', 'message': arg.target.textContent.trim()});
  274.       }
  275.     },
  276.     "copy url": function (arg) {
  277.       if (arg.target) {
  278.         connection.postMessage({action: 'copy', 'message': arg.target.href});
  279.       }
  280.     },
  281.     "copy url and text": function (arg) {
  282.       if (arg.target) {
  283.         connection.postMessage({action: 'copy', 'message': arg.target.textContent.trim() + ' ' + arg.target.href});
  284.       }
  285.     },
  286.     "copy url and text as html": function (arg) {
  287.       if (arg.target) {
  288.         connection.postMessage({action: 'copy', 'message': '<a href="' + arg.target.href + '">' + arg.target.textContent.trim() + '</a>'});
  289.       }
  290.     }
  291.   };
  292.   var TEXT_ACTION = {
  293.     "no action": function () {
  294.     },
  295.     "search with #1 in new tab": function (arg) {
  296.       if (arg.action && arg.action.args && arg.action.args[0]) {
  297.         var url = arg.action.args[0].replace('%s', encodeURIComponent(String(getSelection()).trim()));
  298.         connection.postMessage({action: 'open_tab', 'link': url, foreground: true});
  299.       }
  300.     },
  301.     "search with #1 in current tab": function (arg) {
  302.       if (arg.action && arg.action.args && arg.action.args[0]) {
  303.         var url = arg.action.args[0].replace('%s', String(getSelection()).trim());
  304.         connection.postMessage({action: 'goto', 'link': url, foreground: true});
  305.       }
  306.     },
  307.     "search with #1 in background tab": function (arg) {
  308.       if (arg.action && arg.action.args && arg.action.args[0]) {
  309.         var url = arg.action.args[0].replace('%s', String(getSelection()).trim());
  310.         connection.postMessage({action: 'open_tab', 'link': url});
  311.       }
  312.     },
  313.     "copy text": function (arg) {
  314.       connection.postMessage({action: 'copy', 'message': String(getSelection()).trim()});
  315.     },
  316.     "copy text by custom tag #1": function (arg) {
  317.       if (arg.action && arg.action.args && arg.action.args[0]) {
  318.         var format = arg.action.args[0];
  319.         var data = {
  320.           'URL': location.href,
  321.           'TITLE': document.title,
  322.           'SELECTION-STRING': String(getSelection()),
  323.           'SELECTION-HTML': (new XMLSerializer).serializeToString(window.getSelection().getRangeAt(0).cloneContents())
  324.         };
  325.         var message = format.replace(/%([-\w]+)%/g, function (_, _1) {
  326.           return data[_1] || '';
  327.         });
  328.         connection.postMessage({
  329.           action: 'copy',
  330.           'message': message
  331.         });
  332.       }
  333.     },
  334.     "copy html": function (arg) {
  335.       connection.postMessage({
  336.         action: 'copy',
  337.         'message': (new XMLSerializer).serializeToString(window.getSelection().getRangeAt(0).cloneContents())
  338.       });
  339.     },
  340.     "copy html by custom tag #1": function (arg) {
  341.       if (arg.action && arg.action.args && arg.action.args[0]) {
  342.         var format = arg.action.args[0];
  343.         var data = {
  344.           'URL': location.href,
  345.           'TITLE': document.title,
  346.           'SELECTION-STRING': String(getSelection()),
  347.           'SELECTION-HTML': (new XMLSerializer).serializeToString(window.getSelection().getRangeAt(0).cloneContents())
  348.         };
  349.         var message = format.replace(/%([-\w]+)%/g, function (_, _1) {
  350.           return data[_1] || '';
  351.         });
  352.         connection.postMessage({
  353.           action: 'copy',
  354.           'message': message
  355.         });
  356.       }
  357.     },
  358.     "copy escaped html": function (arg) {
  359.       connection.postMessage({
  360.         action: 'copy',
  361.         'message': (new XMLSerializer).serializeToString(window.getSelection().getRangeAt(0).cloneContents()).replace(/&<>/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;')
  362.       });
  363.     },
  364.     "copy escaped html by custom tag #1": function (arg) {
  365.       if (arg.action && arg.action.args && arg.action.args[0]) {
  366.         var format = arg.action.args[0];
  367.         var data = {
  368.           'URL': location.href,
  369.           'TITLE': document.title,
  370.           'SELECTION-STRING': String(getSelection()),
  371.           'SELECTION-HTML': (new XMLSerializer).serializeToString(window.getSelection().getRangeAt(0).cloneContents()).replace(/&<>/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;')
  372.         };
  373.         var message = format.replace(/%([-\w]+)%/g, function (_, _1) {
  374.           return data[_1] || '';
  375.         });
  376.         connection.postMessage({
  377.           action: 'copy',
  378.           'message': message
  379.         });
  380.       }
  381.     }
  382.   };
  383.  
  384.   function easeOutCubic(t, b, c, d) {
  385.     return c * ((t = t / d - 1) * t * t + 1) + b;
  386.   }
  387.  
  388.   function easeOutQuart(t, b, c, d) {
  389.     return -c * ((t = t / d - 1) * t * t * t - 1) + b;
  390.   }
  391.  
  392.   function SmoothScroll(_x, _y, _duration) {
  393.     if (SmoothScroll.timer) {
  394.       _x += SmoothScroll.X - window.pageXOffset;
  395.       _y += SmoothScroll.Y - window.pageYOffset;
  396.       SmoothScroll.fin();
  397.     }
  398.     SmoothScroll.X = _x + window.pageXOffset;
  399.     SmoothScroll.Y = _y + window.pageYOffset;
  400.     var from_x = window.pageXOffset;
  401.     var from_y = window.pageYOffset;
  402.     var duration = _duration || 400;
  403.     var easing = easeOutQuart;
  404.     var begin = Date.now();
  405.     SmoothScroll.fin = function () {
  406.       clearInterval(SmoothScroll.timer);
  407.       SmoothScroll.timer = void 0;
  408.     };
  409.     SmoothScroll.timer = setInterval(scroll, 10);
  410.     function scroll() {
  411.       var now = Date.now();
  412.       var time = now - begin;
  413.       var prog_x = easing(time, from_x, _x, duration);
  414.       var prog_y = easing(time, from_y, _y, duration);
  415.       window.scrollTo(prog_x, prog_y);
  416.       if (time > duration) {
  417.         SmoothScroll.fin();
  418.         window.scrollTo(from_x + _x, from_y + _y);
  419.       }
  420.     }
  421.   }
  422.  
  423.   function SmoothScrollByElement(target) {
  424.     this.target = target;
  425.     this._target = target === document.documentElement ? document.body : target;
  426.   }
  427.  
  428.   SmoothScrollByElement.noSmooth = function () {
  429.     SmoothScrollByElement.prototype.scroll = function (_x, _y) {
  430.       var self = this, target = this._target;
  431.       target.scrollLeft += _x;
  432.       target.scrollTop += _y;
  433.     };
  434.   };
  435.   SmoothScrollByElement.prototype = {
  436.     scroll: function (_x, _y, _duration) {
  437.       var self = this, target = this.target, _target = this._target, isDown = _y > 0;
  438.       if (self.timer >= 0) {
  439.         _x += self.X - _target.scrollLeft;
  440.         _y += self.Y - _target.scrollTop;
  441.         self.fin();
  442.       }
  443.       var x = _target.scrollLeft;
  444.       var y = _target.scrollTop;
  445.       self.X = _x + x;
  446.       self.Y = _y + y;
  447.       var duration = _duration || 400;
  448.       var easing = easeOutQuart;
  449.       var begin = Date.now();
  450.       self.fin = function () {
  451.         clearInterval(self.timer);
  452.         self.timer = void 0;
  453.       };
  454.       self.timer = setInterval(scroll, 10);
  455.       function scroll() {
  456.         var now = Date.now();
  457.         var time = now - begin;
  458.         if (time > duration || (!isDown && _target.scrollTop === 0) || (isDown && (_target.scrollTop + target.clientHeight + 16 >= target.scrollHeight))) {
  459.           self.fin();
  460.           _target.scrollLeft = x + _x;
  461.           _target.scrollTop = y + _y;
  462.           return;
  463.         }
  464.         var prog_x = easing(time, x, _x, duration);
  465.         var prog_y = easing(time, y, _y, duration);
  466.         _target.scrollLeft = prog_x;
  467.         _target.scrollTop = prog_y;
  468.       }
  469.     },
  470.     isScrollable: function (dir) {
  471.       var self = this, target = this.target, _target = this._target;
  472.       if (target.clientHeight <= target.scrollHeight) {
  473.         if (dir === 'down') {
  474.           if ((_target.scrollTop + target.clientHeight) < target.scrollHeight) {
  475.             return true;
  476.           }
  477.         } else if (dir === 'up' && _target.scrollTop > 0) {
  478.           return true;
  479.         }
  480.       }
  481.       return false;
  482.     }
  483.   };
  484.  
  485.   var ARROW_ICON = {
  486.     U: chrome.extension.getURL('up.png'),
  487.     R: chrome.extension.getURL('right.png'),
  488.     D: chrome.extension.getURL('down.png'),
  489.     L: chrome.extension.getURL('left.png')
  490.   };
  491.   var FIELD_ID = 'chrome-gestures-helper-field';
  492.   var GM = {
  493.     _lastX: 0,
  494.     _lastY: 0,
  495.     _directionChain: "",
  496.     _isMousedown: false,
  497.     _isLeftMousedown: false,
  498.     _scroll_targets: [],
  499.     init: function (config) {
  500.       GM.config = config;
  501.       GM.mouse_track = config.mouse_track;
  502.       GM.normal_actions = config.normal_actions;
  503.       GM.linkdrag_actions = config.linkdrag_actions;
  504.       GM.textdrag_actions = config.textdrag_actions;
  505.       GM.visualized_arrow = GM.config.visualized_arrow;
  506.       //GM.action_config = config.actions;
  507.       window.addEventListener("mousedown", GM, false);
  508.       window.addEventListener("mousemove", GM, false);
  509.       window.addEventListener("mouseup", GM, false);
  510.       if (config.superdrag) {
  511.         window.addEventListener("dragstart", GM, false);
  512.         window.addEventListener("drag", GM, false);
  513.         window.addEventListener("drop", GM, false);
  514.         window.addEventListener("dragenter", GM, false);
  515.         window.addEventListener("dragover", GM, false);
  516.         window.addEventListener("dragend", GM, false);
  517.       }
  518.       if (config.useMousewheel) {
  519.         window.addEventListener("mousewheel", GM, false);
  520.         if (!config.useSmoothScroll) {
  521.           SmoothScrollByElement.noSmooth();
  522.         }
  523.         GM.config.AccelerationValue || (GM.config.AccelerationValue = 5);
  524.         GM.config.ScrollSpeedValue || (GM.config.ScrollSpeedValue = 0.1);
  525.       }
  526.       //GM.isLeft = true;
  527.       document.addEventListener("contextmenu", GM, false);
  528.     },
  529.     handleEvent: function (e) {
  530.       switch (e.type) {
  531.         case "mousedown":
  532.           if (e.button === 2 && !GM._isLeftMousedown) {
  533.             if (window.getSelection().toString().length > 0) {
  534.               return;
  535.             }
  536.             GM._isMousedown = true;
  537.             GM._startGuesture(e);
  538.           } else if (e.button === 0 && GM._isMousedown && !GM._isMousemove) {
  539.             GM.flip_case = '#FlipBack';
  540.           } else if (e.button === 2 && GM._isLeftMousedown && !GM._isMousemove) {
  541.             GM.flip_case = '#FlipForward';
  542.           } else if (e.button === 0 && GM.isLeft && !e.target.draggable) {
  543.             GM._isLeftMousedown = true;
  544.             GM.notdrag = true;
  545.             //console.log(e.target , );
  546.             GM._startGuesture(e);
  547.           } else if (e.button === 0) {
  548.             GM._isLeftMousedown = true;
  549.           }
  550.           break;
  551.         case "mousemove":
  552.           if (!GM.isLeft && GM._isMousedown && !GM.wheel_action) {
  553.             GM._isMousemove = true;
  554.             GM._progressGesture(e);
  555.           } else if (GM._isLeftMousedown && GM.notdrag) {
  556.             GM._isMousemove = true;
  557.             GM._progressGesture(e);
  558.             window.getSelection().removeAllRanges();
  559.           }
  560.           break;
  561.         case "dragstart":
  562.           var link = $X('ancestor-or-self::a', e.target)[0] || false;
  563.           GM.linkdrag = link && link.href && link.href.indexOf('javascript:') !== 0;
  564.           GM.dragging = true;
  565.           GM._startGuesture(e);
  566.           break;
  567.         case "drag":
  568.           if (GM.dragging) {
  569.             GM._progressGesture(e);
  570.           }
  571.           break;
  572.         case "dragend":
  573.           if (e.clientY > 0) {
  574.             GM.superdrag_action(e);
  575.           }
  576.           GM.dragging = false;
  577.           if (GM.field && GM.field.parentNode) {
  578.             GM.field.parentNode.removeChild(GM.field);
  579.             GM.mouse_track_start = false;
  580.           }
  581.           break;
  582.         case "mouseup":
  583.           var r;
  584.           if (GM._isMousemove && GM.isLeft && (r = GM._stopGuesture(e))) {
  585.             e.preventDefault();
  586.           }
  587.           if (GM._isMousedown || (GM.isLeft && GM._isLeftMousedown)) {
  588.             if (GM.field && GM.field.parentNode) {
  589.               GM.field.parentNode.removeChild(GM.field);
  590.               GM.mouse_track_start = false;
  591.             }
  592.           }
  593.           GM._isMousedown = GM._isMousemove = GM._isLeftMousedown = GM.notdrag = false;
  594.           break;
  595.         case "mousewheel":
  596.           if (GM._isMousedown && GM.config.useTabList) {
  597.             var dir = e.wheelDeltaY > 0 ? -1 : 1;
  598.             if (GM.titled) {
  599.               GM.title_change(dir);
  600.             } else {
  601.               connection.postMessage('get_title_list', function (message) {
  602.                 GM.title_list(message.title);
  603.               });
  604.             }
  605.             GM.wheel_action = true;
  606.             e.preventDefault();
  607.           } else {
  608.             var target = e.target, targets = GM._scroll_targets, scroll_object;
  609.             var dir = e.wheelDeltaY > 0 ? 'up' : 'down';
  610.             if (document.TEXT_NODE === target.nodeType) {
  611.               target = target.parentElement;
  612.             }
  613.             do {
  614.               if (!targets.some(function (_so) {
  615.                 if (_so.target === target) {
  616.                   scroll_object = _so;
  617.                   return true;
  618.                 }
  619.               })) {
  620.                 if (target.clientHeight > 0 && (target.scrollHeight - target.clientHeight) > 16 && target !== NotRoot) {
  621.                   var overflow = getComputedStyle(target, "").getPropertyValue("overflow");
  622.                   if (overflow === 'scroll' || overflow === 'auto' || (target.tagName === Root.tagName && overflow !== 'hidden')) {
  623.                     scroll_object = new SmoothScrollByElement(target);
  624.                     targets.push(scroll_object);
  625.                   }
  626.                 }
  627.               }
  628.               if (scroll_object && scroll_object.isScrollable(dir)) {
  629.                 var x = -e.wheelDeltaX, y = -e.wheelDeltaY;
  630.                 if (GM.config.useScrollAcceleration) {
  631.                   var AccelerationValue = GM.config.AccelerationValue;
  632.                   var prev = GM.prev_scroll_time || 0;
  633.                   var now = GM.prev_scroll_time = Date.now();
  634.                   var accele = (1 - Math.min(Math.log(now - prev + 1), LOG_SEC) / LOG_SEC) * AccelerationValue + 1;
  635.                   x *= accele;
  636.                   y *= accele;
  637.                 }
  638.                 var ax = Math.abs(x), ay = Math.abs(y);
  639.                 scroll_object.scroll(x, y, Math.log(Math.max(ax, ay)) * GM.config.ScrollSpeedValue * 100);
  640.                 e.preventDefault();
  641.                 return;
  642.               }
  643.             } while (target = target.parentElement);
  644.           }
  645.           break;
  646.         case "contextmenu":
  647.           if (GM.wheel_action) {
  648.             GM.wheel_action = false;
  649.             e.preventDefault();
  650.             GM.title_end();
  651.           }
  652.           var r;
  653.           if (r = GM._stopGuesture(e)) {
  654.             e.preventDefault();
  655.           }
  656.           break;
  657.       }
  658.     },
  659.     superdrag_action: function (e) {
  660.       var act, action;
  661.       var link = $X('ancestor-or-self::a', e.target)[0] || false;
  662.       if (link && link.href && link.href.indexOf('javascript:') !== 0) {
  663.         act = GM.linkdrag_actions[GM._directionChain];
  664.         action = act && LINK_ACTION[act.name];
  665.       } else {
  666.         act = GM.textdrag_actions[GM._directionChain];
  667.         action = act && TEXT_ACTION[act.name];
  668.       }
  669.       if (act && !action) {
  670.         var ev = document.createEvent('Event');
  671.         ev.initEvent(act.name, true, false);
  672.         var target = link || e.target;
  673.         if (target.dispatchEvent) {
  674.           target.dispatchEvent(ev);
  675.         } else {
  676.           document.dispatchEvent(ev);
  677.         }
  678.         return true;
  679.       }
  680.       if (action) {
  681.         action({config: GM.config, key: GM._directionChain, action: act, target: link || e.target, event: e});
  682.         return true;
  683.       }
  684.     },
  685.     title_list: function (titles) {
  686.       var title_list = document.createElement('ul');
  687.       title_list.id = 'chrome_gestures_title_list';
  688.       title_list.setAttribute('style', 'position:fixed;width:40%;top:30%;left:30%;background:#fff;list-style-type:none;margin:0;padding:0;display:block;');
  689.       titles.forEach(function (title) {
  690.         var li = document.createElement('li');
  691.         li.textContent = title.text;
  692.         li.setAttribute('style', 'background:#fff;border:none;margin:3px;padding:4px;display:block;font-size:12pt;');
  693.         if (title.selected) {
  694.           li.style.background = '-webkit-gradient(linear, left top, left bottom, from(#aaa), to(#eee))';
  695.         }
  696.         title_list.appendChild(li);
  697.       });
  698.       (document.body || document.documentElement).appendChild(title_list);
  699.       GM.titled = true;
  700.       GM.title_end = function () {
  701.         GM.titled = false;
  702.         title_list.parentNode.removeChild(title_list);
  703.         titles.some(function (title) {
  704.           if (title.selected) {
  705.             connection.postMessage({tabid: title.id});
  706.             return true;
  707.           }
  708.         });
  709.       }
  710.       GM.title_change = function (dir) {
  711.         title_list.parentNode.removeChild(title_list);
  712.         var index = 0;
  713.         titles.some(function (title, i) {
  714.           index = i;
  715.           if (title.selected) {
  716.             title.selected = false;
  717.             return true;
  718.           }
  719.         });
  720.         index += dir;
  721.         if (index >= titles.length) {
  722.           index = 0;
  723.         } else if (index < 0) {
  724.           index = titles.length - 1;
  725.         }
  726.         titles[index].selected = true;
  727.         GM.title_list(titles);
  728.       }
  729.     },
  730.     _startGuesture: function (e) {
  731.       GM._lastX = e.clientX;
  732.       GM._lastY = e.clientY;
  733.       GM._directionChain = '';
  734.       GM.target = e.target;
  735.     },
  736.     _progressGesture: function (e) {
  737.       var x = e.clientX;
  738.       var y = e.clientY;
  739.       if (x === 0 && y === 0) {
  740.         GM.dragging = GM._isMousedown = GM._isMousemove = false;
  741.         if (GM.field && GM.field.parentNode) {
  742.           GM.field.parentNode.removeChild(GM.field);
  743.           GM.mouse_track_start = false;
  744.         }
  745.         return;
  746.       }
  747.       var dx = Math.abs(x - GM._lastX);
  748.       var dy = Math.abs(y - GM._lastY);
  749.       if (dx < GM.config.minimumUnit / 2 && dy < GM.config.minimumUnit / 2) return;
  750.       if (GM.mouse_track) {
  751.         if (!GM.mouse_track_start) {
  752.           var field = GM.field = document.createElement("div");
  753.           field.id = FIELD_ID;
  754.           var style = document.createElement("style");
  755.           style.textContent = '#' + FIELD_ID + ' *:after {display:none;}';
  756.           field.appendChild(style);
  757.           var SVG = 'http://www.w3.org/2000/svg';
  758.           var svg = GM.svg = document.createElementNS(SVG, "svg");
  759.           svg.style.position = "absolute";
  760.           field.style.position = "fixed";
  761.           field.addEventListener('click', function (_e) {
  762.             GM.mouse_track_start = false;
  763.             if (field.parentNode) field.parentNode.removeChild(field);
  764.           }, false);
  765.           var polyline = document.createElementNS(SVG, 'polyline');
  766.           polyline.setAttribute('stroke', 'rgba(18,89,199,0.8)');
  767.           polyline.setAttribute('stroke-width', '2');
  768.           polyline.setAttribute('fill', 'none');
  769.           GM.polyline = polyline;
  770.           field.appendChild(svg);
  771.           (document.body || document.documentElement).appendChild(field);
  772.           field.style.left = "0px";
  773.           field.style.top = "0px";
  774.           field.style.display = 'block';
  775.           field.style.zIndex = '1000000';
  776.           field.style.textAlign = 'left';
  777.           field.style.width = Root.clientWidth + 'px';
  778.           field.style.height = Root.clientHeight + 'px';
  779.           if (GM.visualized_arrow) {
  780.             var pop = GM.pop = document.createElement("p");
  781.             var label = GM.label = document.createElement("span");
  782.             var arrows = GM.arrows = document.createElement("span");
  783.             pop.setAttribute('style', 'display:block;background:transparent;position:absolute;top:45%;width:100%;text-align:center;min-height:4em;margin:0px;padding:0px;');
  784.             label.setAttribute('style', 'font-size:large;font-weight:bold;color:white;display:inline-block;background:rgba(0,0,0,0.5);margin:0px;padding:10px;');
  785.             arrows.setAttribute('style', 'display:inline-block;background:rgba(0,0,0,0.5);margin:10px;padding:10px;');
  786.             pop.appendChild(arrows);
  787.             pop.appendChild(document.createElement('br'));
  788.             pop.appendChild(label);
  789.             field.appendChild(pop);
  790.           }
  791.           svg.setAttribute('width', Root.clientWidth);
  792.           svg.setAttribute('height', Root.clientHeight);
  793.           field.style.background = 'transparent';
  794.           field.style.border = 'none';
  795.           GM.mouse_track_start = true;
  796.           svg.appendChild(polyline);
  797.         }
  798.         GM.startX = e.clientX;
  799.         GM.startY = e.clientY;
  800.         var p = GM.svg.createSVGPoint();
  801.         p.x = GM.startX;
  802.         p.y = GM.startY;
  803.         GM.polyline.points.appendItem(p);
  804.       }
  805.       if (dx < GM.config.minimumUnit && dy < GM.config.minimumUnit) return;
  806.       var direction;
  807.       if (dx > dy) {
  808.         direction = x < GM._lastX ? "L" : "R";
  809.       } else {
  810.         direction = y < GM._lastY ? "U" : "D";
  811.       }
  812.       var lastDirection = GM._directionChain[GM._directionChain.length - 1];
  813.       if (direction !== lastDirection) {
  814.         GM._directionChain += direction;
  815.         if (GM.mouse_track && GM.visualized_arrow) {
  816.           var img = document.createElement('img');
  817.           img.src = ARROW_ICON[direction];
  818.           GM.arrows.appendChild(img);
  819.           var act;
  820.           if (!GM.dragging) {
  821.             act = GM.normal_actions[GM._directionChain];
  822.           } else {
  823.             if (GM.linkdrag) {
  824.               act = GM.linkdrag_actions[GM._directionChain];
  825.             } else {
  826.               act = GM.textdrag_actions[GM._directionChain];
  827.             }
  828.           }
  829.           if (act) {
  830.             var name = act.name, _name;
  831.             if ((_name = chrome.i18n.getMessage('action_name_' + name.replace(/\W/g, '_')))) {
  832.               name = _name;
  833.             }
  834.             GM.label.textContent = name.replace(/#(\d+)/g, function (_, _1) {
  835.               return act.args[parseInt(_1, 10)] || '...';
  836.             });
  837.             GM.label.style.display = 'inline-block';
  838.           } else {
  839.             GM.label.style.display = 'none';
  840.           }
  841.         }
  842.       }
  843.       GM._lastX = x;
  844.       GM._lastY = y;
  845.     },
  846.     _stopGuesture: function (e) {
  847.       var isGS = GM._performAction(e);
  848.       GM._directionChain = "";
  849.       return isGS !== false;
  850.     },
  851.     _performAction: function (e) {
  852.       if (GM.flip_case) {
  853.         GM._directionChain = GM.flip_case;
  854.         GM.flip_case = false;
  855.       }
  856.       var act = GM.normal_actions[GM._directionChain];
  857.       var action = act && ACTION[act.name];
  858.       if (act && !action) {
  859.         var ev = document.createEvent('MessageEvent');
  860.         ev.initMessageEvent(act.name, true, false, JSON.stringify(act), location.protocol + "//" + location.host, "", window);
  861.         document.dispatchEvent(ev);
  862.         return true;
  863.       }
  864.       if (action) {
  865.         action({config: GM.config, key: GM._directionChain, action: act, event: e});
  866.         return true;
  867.       } else if (GM._directionChain && GM.config.suppress_contextmenu) {
  868.         return true;
  869.       } else {
  870.         return false;
  871.       }
  872.     }
  873.   };
  874. // e.g. '//body[@class = "foo"]/p' -> '//prefix:body[@class = "foo"]/prefix:p'
  875. // http://nanto.asablo.jp/blog/2008/12/11/4003371
  876.   function addDefaultPrefix(xpath, prefix) {
  877.     var tokenPattern = /([A-Za-z_\u00c0-\ufffd][\w\-.\u00b7-\ufffd]*|\*)\s*(::?|\()?|(".*?"|'.*?'|\d+(?:\.\d*)?|\.(?:\.|\d+)?|[\)\]])|(\/\/?|!=|[<>]=?|[\(\[|,=+-])|([@$])/g;
  878.     var TERM = 1, OPERATOR = 2, MODIFIER = 3;
  879.     var tokenType = OPERATOR;
  880.     prefix += ':';
  881.     function replacer(token, identifier, suffix, term, operator, modifier) {
  882.       if (suffix) {
  883.         tokenType =
  884.           (suffix == ':' || (suffix == '::' && (identifier == 'attribute' || identifier == 'namespace')))
  885.             ? MODIFIER : OPERATOR;
  886.       } else if (identifier) {
  887.         if (tokenType == OPERATOR && identifier != '*')
  888.           token = prefix + token;
  889.         tokenType = (tokenType == TERM) ? OPERATOR : TERM;
  890.       } else {
  891.         tokenType = term ? TERM : operator ? OPERATOR : MODIFIER;
  892.       }
  893.       return token;
  894.     }
  895.  
  896.     return xpath.replace(tokenPattern, replacer);
  897.   }
  898.  
  899. // $X on XHTML
  900. // $X(exp);
  901. // $X(exp, context);
  902. // @target Freifox3, Chrome3, Safari4, Opera10
  903. // @source http://gist.github.com/184276.txt
  904.   function $X(exp, context) {
  905.     context || (context = document);
  906.     var _document = context.ownerDocument || document,
  907.       documentElement = _document.documentElement;
  908.     var isXHTML = documentElement.tagName !== 'HTML' && _document.createElement('p').tagName === 'p';
  909.     var defaultPrefix = null;
  910.     if (isXHTML) {
  911.       defaultPrefix = '__default__';
  912.       exp = addDefaultPrefix(exp, defaultPrefix);
  913.     }
  914.     function resolver(prefix) {
  915.       return context.lookupNamespaceURI(prefix === defaultPrefix ? null : prefix) ||
  916.         documentElement.namespaceURI || '';
  917.     }
  918.  
  919.     var result = _document.evaluate(exp, context, resolver, XPathResult.ANY_TYPE, null);
  920.     switch (result.resultType) {
  921.       case XPathResult.STRING_TYPE :
  922.         return result.stringValue;
  923.       case XPathResult.NUMBER_TYPE :
  924.         return result.numberValue;
  925.       case XPathResult.BOOLEAN_TYPE:
  926.         return result.booleanValue;
  927.       case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
  928.         // not ensure the order.
  929.         var ret = [], i = null;
  930.         while (i = result.iterateNext()) ret.push(i);
  931.         return ret;
  932.     }
  933.     return null;
  934.   }
  935.  
  936.   connection.postMessage({init: true, location: location}, function (message) {
  937.     if (/BackCompat/i.test(document.compatMode)) {
  938.       var body_check = function () {
  939.         Root = document.body;
  940.         if (!Root) {
  941.           setTimeout(body_check, 100);
  942.         }
  943.       };
  944.       body_check();
  945.     } else {
  946.       NotRoot = document.body;
  947.       Root = document.documentElement;
  948.     }
  949.     GM.init(message.conf);
  950.   });
  951.   this.ChromeGesture = GM;
  952. }).call(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement