0

I'm trying to make a simple extension, that can click in a element on the my principal page, example: I have a login page that have 6 buttons to choose a password and I want to simulate a click in each button(I already did this on Selenium).

But, when I use de DOM(document object) the chrome is getting the document from a new HTML and is not getting my HTML.

Above my example:

background.js

function checkForValidUrl(tabId, changeInfo, tab) {
    chrome.pageAction.show(tabId);
}

function getElementByXpath(path) {
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

chrome.tabs.onUpdated.addListener(checkForValidUrl);

chrome.pageAction.onClicked.addListener(function(tab) {
 var element = getElementByXpath("//label[contains(text(),'1')]");
    element.click();
});

On my code, when I try to select a Element I can't get the main page.

MANIFEST.js

{
  // Required
  "manifest_version": 2,
  "name": "Minha Extensão",
  "version": "2",

  // Recommended
  "description": "A plain text description",
  "icons": { "16": "icon-w-1.png",
           "48": "icon-w-2.png",
          "128": "icon-w-3.png" },
  "background": {
        "scripts": ["background.js"]
    },
    "page_action": {
        "default_icon": "icon-w-3.png"
    },
    "permissions": ["tabs", "http://*/*", "https://*/*"]        
}

My next step will be, in the moment that I click on the extension button, open a new window with buttons that when I click, can simulate a click on another button in the main page.

Willians Martins
  • 198
  • 2
  • 13

1 Answers1

1

You can't access web page DOM from background page. You have to insert content script.

See more in - Chrome documentation

Elad
  • 1,054
  • 7
  • 15