javascript - 谷歌文档 : click a button or change style with userscript (tampermonkey)

标签 javascript google-docs userscripts tampermonkey

我想要使用键盘快捷键来更改 google 文档中选定文本的颜色。

我能够通过 id 获取按钮元素,但是当我向该元素发送 click() 时,什么也没有发生(这是我尝试使用用于调试的粗体按钮,因为它比我的更简单想用按钮 textColorButton )

var button = document.getElementById("boldButton");
alert(button.innerHTML);   // working
button.click();   // not working

我也想获取选择然后应用样式,但 getSelection 是空的:

var text = document.getSelection();
alert(text);  // empty

到目前为止,这是我的脚本:

// ==UserScript==
// @name         Google docs
// @match        https://docs.google.com/document/d/#######
// @require    http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @require    http://cdnjs.cloudflare.com/ajax/libs/sugar/1.3/sugar.min.js
// ==/UserScript==

var editingIFrame = $('iframe.docs-texteventtarget-iframe')[0];
if (editingIFrame) {
    editingIFrame.contentDocument.addEventListener("keydown", hook, false);
}
function hook(key) { 
    if (key.ctrlKey  &&  key.altKey  &&  key.code === "KeyE") {
        var button = document.getElementById("boldButton");
        button.click();   // not working
    }
}

最佳答案

点击按钮的技巧是这样做的:

var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent(eventType, true, true);
button.dispatchEvent(clickEvent);

这是完整的脚本:

// ==UserScript==
// @name         Google docs
// @match        https://docs.google.com/document/d/#########
// @require    http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// ==/UserScript==
// 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
var editingIFrame = $('iframe.docs-texteventtarget-iframe')[0];
if (editingIFrame) {
    editingIFrame.contentDocument.addEventListener("keydown", hook, false);
}

function hook(key) {
    if (key.ctrlKey && key.altKey && key.code === "KeyY") {
        var button = document.getElementById("textColorButton");
        var color = document.querySelector('[title="light red berry 3"]');
        triggerMouseEvent(button, "mouseover");
        triggerMouseEvent(button, "mousedown");
        triggerMouseEvent(button, "mouseup");
        triggerMouseEvent(color, "mouseover");
        triggerMouseEvent(color, "mousedown");
        triggerMouseEvent(color, "mouseup");
    }

}

function triggerMouseEvent(node, eventType) {
    var clickEvent = document.createEvent('MouseEvents');
    clickEvent.initEvent(eventType, true, true);
    node.dispatchEvent(clickEvent);
}

关于javascript - 谷歌文档 : click a button or change style with userscript (tampermonkey),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49857599/

相关文章:

javascript - 使用 ERB 在 Javascript 中设置 CSS backgroundImage 路径

javascript - 如何在点击时激事件态侧边栏

css - 如何检查链接是否已被访问?

javascript - 如何将 ViolentMokey userScript 制作成浏览器扩展?

javascript - 阻止浏览器发送某些 JavaScript 事件

javascript - 如何将 Google Sheet 日期数值转换为 JavaScript 日期?

javascript - Backbone 的新手,但是当我循环遍历 Collection 时,我的所有属性都丢失了

excel - 在 Google 电子表格上正确使用 SUMIF

javascript - 如何在 javascript 字符串中插入一个新段落?

google-apps-script - 使用我自己的 Google Docs 插件而不发布它