javascript - Simulated Click in Chrome Extension - Stack Overflow

I am making a Chrome extension. One part of this extension needs to be able to simulate a click in orde

I am making a Chrome extension. One part of this extension needs to be able to simulate a click in order to activate the onClick events on the page. 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 JavaScript debugging:

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.

I am making a Chrome extension. One part of this extension needs to be able to simulate a click in order to activate the onClick events on the page. 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 JavaScript debugging:

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.

Share Improve this question edited Mar 23, 2022 at 14:25 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 10, 2012 at 3:55 godismyjudge95godismyjudge95 9221 gold badge10 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Script execution environments are different for extension and page.

Use chrome.tabs.executeScript

For example, to paste some text to Google search field

File: 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

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://*/*").

But if question was strictly on JavaScript click event, look here How to simulate a click with JavaScript?

Not sure if this is applicable to Chrome extensions but maybe you could use the document.createEvent method as explained here:

document.createEvent - Document Object Model (DOM) | MDN

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742416615a4439867.html

相关推荐

  • javascript - Simulated Click in Chrome Extension - Stack Overflow

    I am making a Chrome extension. One part of this extension needs to be able to simulate a click in orde

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信