3

I'm creating a Chrome Extension that opens a popup with simple HTML. I'm not being able to access this popup DOM.

Is it possible to manipulate the popup DOM?

Examples:

background.js

chrome.browserAction.onClicked.addListener(function(tab) {

  chrome.windows.create({"url": "popup.html", "type": "popup"}, function(wd){
     console.log(wd);
     wd.document.getElementById("txtField").value = "asd";
  })
});

popup.html

<html>
   <head></head>
   <body>
        <input type="text" id="txtField" />
   </body>
</html>

When I run this code, it says "Cannot read property 'getElementById' of undefined".

Lucas Mello
  • 342
  • 3
  • 8
  • what's the result of the `console.log` ? – jean-max Dec 09 '16 at 13:44
  • Possible duplicate of [Chrome Extension - Get DOM content](http://stackoverflow.com/questions/19758028/chrome-extension-get-dom-content) – André Dion Dec 09 '16 at 13:47
  • I would suggest that you read the [Chrome extension architecture overview](https://developer.chrome.com/extensions/overview#arch) (and perhaps work through reading the pages linked from there). It has overall architecture information which should help your understanding of how things are generally organized/done. – Makyen Dec 09 '16 at 15:02
  • @Jean-maximeBouloc is a chrome window object, not a regular DOM window... https://developer.chrome.com/extensions/windows#type-Window – Lucas Mello Dec 09 '16 at 15:23
  • @AndréDion I don't think it's the same question. He doesn't seem to know what is the function of background, popup or content scripts and I'm kind of used to it. – Lucas Mello Dec 09 '16 at 15:27
  • @Makyen I've already read it, but I couldn't find this answer, because chrome.window.create callback doesn't return a regular DOM object, but a chrome window object, and I'm not able to access the DOM. I may be wrong, 'cause I'm pretty new working with javascript and chrome extension. – Lucas Mello Dec 09 '16 at 15:30
  • @LucasMello, yeah. The info that you need is not clearly spelled out with respect to your exact situation. Generally, you will want to do DOM manipulations from within JavaScript loaded with a ` – Makyen Dec 09 '16 at 16:02

2 Answers2

2

Use JavaScript in a file included in the HTML with <script>

Generally, you should do DOM manipulations from within JavaScript loaded with a <script> tag within your popup.html, just like you would for a browser action popup or options page. For example from JavaScript in a popup.js file and included in popup.html like:

<html>
   <head>
     <script src="popup.js"></script>
   </head>
   <body>
        <input type="text" id="txtField" />
   </body>
</html>

Actually manipulating the DOM of the new window from the opener

If you are wanting to access the Window of a popup, you will need to get a view for it using chrome.extension.getViews(). This is complicated by the fact that the window does not actually exist yet when the callback from chrome.windows.create() is executed, or when a chrome.windows.onCreated listener (or windows.onFocusChanged) is called (both fire before the chrome.windows.create() callback is executed). There does not appear to be an event which will notify you when the new popup window actually exists, or when you can get a view for it.

Thus, you are much better off using code in a JavaScript file which is included in the HTML using a <script> tag.

If you still desire to manipulate the DOM of the opened window, you are going to have to poll until you find a view for it. The following will do what you want:

background.js:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.windows.create({"url": "popup.html", "type": "popup"}, function(wd){
        waitForWindowId(winObject.id,50,20,function(view){
            console.log(view);
            view.document.getElementById("txtField").value = "asd";
        });
    })
});

function waitForWindowId(id,delay,maxTries,callback) {
    let where = 'watchForWindow';
    if(maxTries--<=0){
        //Fail softly. You may want to do something else on failure to find the new window.
        return;
    }
    //This type of window is a type:'tab', not type:'popup'.
    if(!chrome.extension.getViews({windowId:id}).some(view=>{
        if(typeof callback === 'function'){
            callback(view);
        }
        return true;
    })){
        //Did not find the view for window ID. Keep trying
        setTimeout(waitForWindowId,delay,id,delay,maxTries,callback);
    }
}
Makyen
  • 31,849
  • 12
  • 86
  • 121
1
chrome.windows.create(
    {
        url:'https://site.ru',
        type: 'popup',
        state: 'maximized',
    },
    function(window) {
        chrome.tabs.executeScript(window.tabs[0].id, {
            file: '/back/inject.js',
        });
    }
);
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135