Chris Jenkins
Chris Jenkins

Reputation: 719

How can I manipulate a new window in a Chrome Extension?

I am writing a chrome extension to allow users to login to social media sites from a single page. I am able to create a new incognito window but am unable to manipulate anything inside of the window that I created. I want to create an onload function for the new window to execute jquery. Thanks for getting me pointed in the right direction!

Upvotes: 4

Views: 1515

Answers (1)

Sudarshan
Sudarshan

Reputation: 18544

Refer the following demonstration for manipulation of new incognito window created and injecting some jquery into it.

References

manifest file

This is used to bind permissions and register background pages to extension.Ensure it has all permissions needed.

{
"name":"Hanlder for Incognito window",
"description":"http://stackoverflow.com/questions/14044338",
"version":"1",
"manifest_version":2,
"background":{
    "scripts":["background.js"]
},
"permissions":["tabs","http://www.google.co.in/"]
}

background.js

Inject jquery into new incognito window, from background page.

var _tabId_To_Look_For;

// Create a new incognito Window with some arbitary URL and give it focus
chrome.windows.create({
    "url": "http://www.google.co.in/",
    "focused": true,
    "incognito": true
}, function (window) {
    // Trace tab id which is created with this query 
    _tabId_To_Look_For = window.tabs[0].id
});

// Add an event Listener for new tab created
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
    // Inject script into chosen tab after it is loaded completely
    if (tabId == _tabId_To_Look_For && changeInfo.status == "complete") {
        // Inject Jquery and in current tab
        chrome.tabs.executeScript(tabId, {
            "file": "jquery.js"
        }, function () {
            // I am in call back
            console.log("Injected some jquery ");
        });
    }
});

Ensure you have enabled incognito access.

enter image description here

Output

You will observe a new window with jquery injected.

Upvotes: 6

Related Questions