简体   繁体   English

在Chrome扩展程序中模拟点击

[英]Simulated Click in Chrome Extension

I am making a chrome extension. 我正在扩展Chrome。 One part of this extension needs to be able to simulate a click in order to activate the onClick events on the page. 此扩展程序的一部分需要能够模拟点击,以激活页面上的onClick事件。 Here is the code from the background script: 这是后台脚本的代码:

function checkForValidUrl(tabId, changeInfo, tab) {
  // If the letter 'g' is found in the tab's URL...
  if (tab.url.indexOf('maps') > -1 && tab.url.indexOf('google') > -1) {
    // ... show the page action.
    chrome.pageAction.show(tabId);

  }
};

// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);


chrome.pageAction.onClicked.addListener(function() {
    document.getElementById("paneltoggle2").click();
});

Here is the error message that I am getting from chrome's java script debugging: 这是我从chrome的Java脚本调试中得到的错误消息:

Error in event handler for 'pageAction.onClicked': Cannot call method 'click' of null TypeError: Cannot call method 'click' of null
    at chrome-extension://deogcaeekneeagffbhdlflichjlodlem/js/main.js:26:42
    at chrome.Event.dispatchToListener (event_bindings:387:21)
    at chrome.Event.dispatch_ (event_bindings:373:27)
    at dispatchArgs (event_bindings:249:22)
    at Object.chromeHidden.Event.dispatchEvent (event_bindings:257:7) event_bindings:377
chrome.Event.dispatch_ event_bindings:377
dispatchArgs event_bindings:249
chromeHidden.Event.dispatchEvent event_bindings:257

I am guessing that it is something to do with the permissions in the manifest file... Right now I only have permissions to "tabs". 我猜想这与清单文件中的权限有关……现在,我仅具有“标签”的权限。 Is there some other permissions I need to activate in order to simulate the click and not get an error? 我需要激活其他一些权限才能模拟点击并不会出现错误吗? Oh and I am trying to make this capable with version 2 manifest protocol. 哦,我正在尝试通过版本2清单协议使之具备此功能。

Thanks, Leinardo 谢谢莱纳尔多

Script execution environments are different for extension and page. 扩展名和页面的脚本执行环境不同。

Use chrome.tabs.executeScript 使用chrome.tabs.executeScript

For example, to paste some text to Google search field 例如,将一些文本粘贴到Google搜索字段

File: manifest.json 文件:manifest.json

{
    "name": "My Test",
    "version": "1",
    "manifest_version": 2,
    "background": {
        "scripts": ["background.js"]
    },
    "page_action": {
        "default_icon": "icon.png"
    },
    "permissions": ["tabs", "http://*/*", "https://*/*"]
}

File: background.js 文件:background.js

function checkForValidUrl(tabId, changeInfo, tab) {
    if (tab.url.indexOf("g") > -1) {
        chrome.pageAction.show(tabId);
    }
}

chrome.tabs.onUpdated.addListener(checkForValidUrl);

chrome.pageAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id, {code: "document.getElementById('gbqfq').value = 'Hello World!';"});
});

In manifest file there is need permission to host ( "http://*/*" ). 在清单文件中,需要托管权限( "http://*/*" )。

But if question was strictly on JavaScript click event, look here How to simulate a click with JavaScript? 但是,如果问题完全是关于JavaScript click事件的,请看此处如何使用JavaScript模拟点击?

Not sure if this is applicable to Chrome extensions but maybe you could use the document.createEvent method as explained here: 不知道这是否适用于Chrome扩展程序,但是您可以按照以下说明使用document.createEvent方法:

document.createEvent - Document Object Model (DOM) | document.createEvent-文档对象模型(DOM)| MDN MDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM