Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Google docs
  3. // @include      https://*docs.google.*/document/*
  4. // @require    http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
  5. // ==/UserScript==
  6. // sources:
  7. // for iframe https://stackoverflow.com/a/46217408/3154274
  8. // for switch https://stackoverflow.com/q/13362028/3154274
  9. // combinaison of key  https://stackoverflow.com/a/37559790/3154274
  10. // dispatchEvent https://stackoverflow.com/a/33887557/3154274
  11. // simulate keypress https://stackoverflow.com/a/26863396/3154274    or https://stackoverflow.com/a/5920206/3154274
  12.  
  13.  
  14. // js key code http://keycode.info/
  15.  
  16. // listen for key shorcuts on the text part of google gocs
  17. var editingIFrame = $('iframe.docs-texteventtarget-iframe')[0];
  18. if (editingIFrame) {
  19.     editingIFrame.contentDocument.addEventListener("keydown", hook, false);
  20. }
  21.  
  22. // match the key with the color
  23. function hook(key) {
  24.  
  25.     if (key.altKey  &&  key.code === "KeyQ") {
  26.         var button = document.getElementById("zoomSelect");
  27.         callMouseEvent(button);
  28.         keyEvent(button);
  29.  
  30.   }
  31.  
  32.  
  33. }
  34.  
  35. //call each mouse event
  36. function callMouseEvent(button){
  37.     triggerMouseEvent (button, "mouseover");
  38.     triggerMouseEvent (button, "mousedown");
  39.     triggerMouseEvent (button, "mouseup");
  40. }
  41.  
  42. // send mouse even
  43. function triggerMouseEvent (node, eventType) {
  44.     var eventObj        = document.createEvent('MouseEvents');
  45.     eventObj.initEvent (eventType, true, true);
  46.     node.dispatchEvent   (eventObj);
  47. }
  48.  
  49.  
  50. // send keyboard event
  51. function keyEvent(node) {
  52.  
  53. var eventObj = document.createEvent("KeyboardEvent");
  54. (eventObj.initKeyEvent || eventObj.initKeyboardEvent)("keypress", true, true, null,
  55.                0, 0, 0, 0,
  56.                0, character.charCodeAt(9));
  57.  
  58. node.dispatchEvent(eventObj);
  59.  
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement