【问题标题】:Clicking a button on Google Docs through Chrome Extension通过 Chrome 扩展程序单击 Google Docs 上的按钮
【发布时间】:2020-10-17 19:54:07
【问题描述】:

我正在尝试创建一个 chrome 扩展程序,当您单击扩展程序上的按钮时,它将单击 Google 文档上的按钮。下面是我对这个问题的尝试(这似乎不起作用,我只是点击文档上的打印按钮)。

manifest.json

{  
    "manifest_version": 2,  

    "name": "Google Docs Button Click",  
    "description": "Google Docs Button Click Simulator",  
    "version": "1.0",    
    "permissions": ["tabs", "http://*.docs.google.com/*","*://*/*"],  

    "browser_action": {  
        "default_icon": "cursor.png",  
        "default_popup": "popup.html"  
    }
}

popup.html

<!doctype html>  
<html>  
    <head><title>activity</title></head>  
<body>  
    <button id="clickactivity">click</button>  
    <script src="popup.js"></script> 
</body>
</html>

popup.js

function injectTheScript() {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.executeScript(tabs[0].id, {file: "content_script.js"});
    });
}

document.getElementById('clickactivity').addEventListener('click', injectTheScript);

content_script.js

function clickButton() {
    var buttons = document.getElementById("printButton");
    buttons.click();
}

clickButton();

请帮忙。

【问题讨论】:

    标签: javascript html google-chrome google-chrome-extension content-script


    【解决方案1】:

    目前与 Google-Docs HTML 元素交互并不简单。

    根据我最近的经验(在阅读您的问题并玩弄之后),鼠标点击的 DOM 侦听器附加到“printButton”元素。

    如果您使用检查元素显示“printButton”元素的 JS 侦听器(打开 exclude-ancestor-listeners),您将看到该文档没有任何鼠标点击侦听器。

    所以我们需要在其他地方调用 .click() 调用。 另一种方法是抓取 XY 页面位置,并尝试在 body 元素上单击鼠标。 但我认为 Google 产品团队故意向用户隐藏此功能以减少恶意行为。

    总结:我认为这是不可能的,除非你在谷歌工作并且你知道一个秘密的方式。


    话虽如此,您的代码正在尝试打印文档。 那么怎么样

    window.print()
    

    :D

    【讨论】:

    • 感谢您提及选项。实际上,我从打印按钮开始,但还有其他任务栏功能要实现,例如。撤消、重做、缩放等。因此,仅仅为打印找到替代方案并不能解决整个问题陈述:(
    • 他们有这些东西的API,就像他们有Google Sheets一样。这与他们可能会查看合成事件的 isTrusted 属性或以其他方式忽略它们的原因相吻合。
    【解决方案2】:

    使用google-docs-utils 库。

    GoogleDocsUtils.pressOn.PrintDialog();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2010-10-04
      • 2012-08-13
      • 1970-01-01
      相关资源
      最近更新 更多