Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AnonymousXiaoLongBao/c417c881d3d572aa5f598f4c3803530d to your computer and use it in GitHub Desktop.
Save AnonymousXiaoLongBao/c417c881d3d572aa5f598f4c3803530d to your computer and use it in GitHub Desktop.
(dont look, personal thing)
//Note to Editor, simplify the var things.
//NTE(look up), i corrected most errors liao, give my thanks from 20/2/2020 to the person who created this.
//sUDDENLY CANNOT WORK, PLS TAKE A LOOK WHY
//suddenly can, bluestackoverflow and gif op(24/2/20)
//now cannot...
//
// Help needed: I failed to send a value to the text resize and to apply it. Any idea ?
/*
Colour Codes:
alt+b = light blue
alt+shift+b = dark blue
alt+h = black
alt+r = red
alt+p = marron brown???
alt+shift+g = dark green
alt+g = green
*/
// to install this script: install the tampermonkey extension. Then click on "raw" on this github page.
// *perso note:
//to install or to send update:
// 1/ edit must be done on github (cf. the url in the tab settings).
// 2/ Edit/update the script (using the edit github button) + increase the version (or it will ask to reinstall in the next step)
// (don't need to increase the version if the change are just for myself: just reinstall it to keep the version)
// 3/ click on RAW (it will show the changes).
// 4/ Click Update (if needed refresh the dashboard page)
// last update: 2019_07_18
// ==UserScript==
// @name Google Docs Colour Tweaks
// @namespace some_google_doc_shortcuts_to_change_font_highlight
// @version 1.0.0
// @author Willi
// @description some google doc shortcuts to change the font and highlights (help needed for font size!). It also hide the page break dotted lines.
// @updateURL https://gist.github.com/WiliTest/b78ad2c234565ba8ce40df15440540d9.js
// @downloadURL https://gist.github.com/WiliTest/b78ad2c234565ba8ce40df15440540d9.js
// @supportURL https://gist.github.com/WiliTest/b78ad2c234565ba8ce40df15440540d9
// @include https://*docs.google.*/document/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// add a style sheet to the page (needed to tweak CSS) https://stackoverflow.com/a/19392142/1486850
// @grant GM_addStyle
// ==/UserScript==
console.log('ok, tamperstarted !');
// hide the page break doted line:
// https://stackoverflow.com/a/52870899/3154274
GM_addStyle ( `
.kix-page-compact::before {
border-top: none;
}
` );
// listen for key shorcuts on the text part of google gocs
// ● Tuto - sources:
// ○ for iframe https://stackoverflow.com/a/46217408/3154274
// ○ for switch https://stackoverflow.com/q/13362028/3154274
// ○ combinaison of key https://stackoverflow.com/a/37559790/3154274
// ○ dispatchEvent https://stackoverflow.com/a/33887557/3154274
// ○ for dispatch :
// https://jsfiddle.net/6vyL98mz/33/
// https://jsfiddle.net/ox2La621/1/
var editingIFrame = ('iframe.docs-texteventtarget-iframe')[0];
if (editingIFrame) {
editingIFrame.contentDocument.addEventListener("keydown", dispatchkeyboard, false);
}
// match the key with the action
function dispatchkeyboard(key) {
var button = document.getElementById("textColorButton")
// fontcolor
if (key.altKey && key.shiftKey && key.code === "KeyB") {// alt+shift+b
button = document.getElementById("textColorButton");
callMouseEvent(button);
setTimeout(function(){
var color_choice = document.getElementById("docs-material-colorpalette-cell-17"); // dark blue
console.log("dark blue: clickbutton wait 2sec");
callMouseEvent(color_choice);
}, 1);
}
if (key.altKey && key.code === "KeyB") {// alt+b #rubbish stuff(not really) --> (ctrl+alt+X doest work (conflict with inbuilt shortcut?))
button = document.getElementById("textColorButton");
callMouseEvent(button);
setTimeout(function(){
var color_choice = document.getElementById("docs-material-colorpalette-cell-16"); // light blue
console.log("light blue: clickbutton wait 2sec");
callMouseEvent(color_choice);
}, 1);
}
if (key.altKey && key.code === "KeyH") {//alt+H
button = document.getElementById("textColorButton");
callMouseEvent(button);
setTimeout(function(){
var color_choice = document.getElementById("docs-material-colorpalette-cell-0"); // black
console.log("clickbutton wait 2sec");
callMouseEvent(color_choice);
}, 1);
}
if (key.altKey && key.code === "KeyR") {
button = document.getElementById("textColorButton");
callMouseEvent(button);
setTimeout(function(){
var color_choice = document.getElementById("docs-material-colorpalette-cell-11"); //red
console.log("clickbutton wait 2sec");
callMouseEvent(color_choice);
}, 1);
}
if (key.altKey && key.code === "KeyP") {
button = document.getElementById("textColorButton");
callMouseEvent(button);
setTimeout(function(){
var color_choice = document.getElementById("docs-material-colorpalette-cell-50"); // Marron brun
console.log("clickbutton wait 2sec");
callMouseEvent(color_choice);
}, 1);
};
if (key.altKey && key.shiftKey && key.code === "KeyG") {
button = document.getElementById("textColorButton");
callMouseEvent(button);
setTimeout(function(){
var color_choice = document.getElementById("docs-material-colorpalette-cell-54"); // dark green 1
console.log("clickbutton wait 2sec");
callMouseEvent(color_choice);
}, 1);
};
if (key.altKey && key.code === "KeyG") {
button = document.getElementById("textColorButton");
callMouseEvent(button);
setTimeout(function(){
var color_choice = document.getElementById("docs-material-colorpalette-cell-53"); // green 1
console.log("clickbutton wait 2sec");
callMouseEvent(color_choice);
}, 1);
}
/*
//--------------- background color
var buttonbg = document.getElementById("bgColorButton")
// hl yellow
if (key.altKey && key.code === "KeyY") {
buttonbg = document.getElementById("bgColorButton");
callMouseEvent(buttonbg);
setTimeout(function(){
var color_choice = document.getElementById("docs-material-colorpalette-cell-103"); //buttonbg.querySelector('[title="yellow"]');
console.log("clickbutton wait 2sec");
callMouseEvent(color_choice);
}, 1);
}
// hl gray
if (key.altKey && key.code === "KeyG") {
buttonbg = document.getElementById("bgColorButton");
callMouseEvent(buttonbg);
setTimeout(function(){
var color_choice = document.getElementById("docs-material-colorpalette-cell-96"); //buttonbg.querySelector('[title="yellow"]');
console.log("clickbutton wait 2sec");
callMouseEvent(color_choice);
}, 1);
}
//---------------Failed attempt to change the font size:
*/
function dispatchkeyboard(key) {
if (key.altKey && key.code === "KeyJ") {
var divfont = document.getElementById("fontSizeSelect");
console.log(divfont);
var inputt = divfont.getElementsByTagName('input')[0];
inputt.select();
inputt.value = "6";
console.log('done');
var ev = document.createEvent('Event');
ev.initEvent('keypress');
ev.which = ev.keyCode = 13;
console.log(ev);
inputt.dispatchEvent(ev);
}
}
// //document.getElementById("fontSizeSelect").click();
// console.log('done');
// triggerMouseEvent (button, "mouseover");
// triggerMouseEvent (button, "mousedown");
// triggerMouseEvent (button, "mouseup");
//
}// end of dispatchkeyboard
//call each mouse event
function callMouseEvent(button){
triggerMouseEvent (button, "mouseover");
triggerMouseEvent (button, "mousedown");
triggerMouseEvent (button, "mouseup");
}
// send mouse even
function triggerMouseEvent (node, eventType) {
var eventObj = document.createEvent('MouseEvents');
eventObj.initEvent (eventType, true, true);
node.dispatchEvent (eventObj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment