2

I am developed one chrome extension basic of current page url. Below are listed my file of chrome extension.

My manifest.json file is

{
    "name": "Domain Name",
    "description": "http://stackoverflow.com/questions/14796722/javascript-google-chrome-extension-getting-domain-name",
    "version": "1",
    "manifest_version": 2,
    "content_scripts": [
        {
            "matches": [
                "<all_urls>"
            ],
            "js": [
                "myscript.js"
            ]
        }
    ],
    "browser_action": {
        "default_popup": "popup.html",
"default_icon": {                    
            "38": "icon.png"            
          }
    },
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "permissions": [
        "tabs",
        "<all_urls>"
    ],
"content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["myscript.js","popup.js"]
    }
  ]
}

My background.js file

chrome.tabs.onUpdated.addListener(function (tabId, change, tab) {

    if (tab.url == "https://www.facebook.com/") {
        chrome.browserAction.setIcon({ path: 'BGicon.png' });
    }
    else {
        chrome.browserAction.setIcon({ path: 'icon.png' });
    }
});

myscript.js

console.log(document.domain);

var domainURL = document.domain;

if (document.domain == "facebook.com") {
    alert("Succes");
}

popup.js

document.addEventListener("DOMContentLoaded", function () {
    console.log(document.domain);//It outputs id of extension to console

    renderStatus('Search term: Google image search result: ');

    chrome.tabs.query({ //This method output active URL 
        "active": true,
        "currentWindow": true,
        "status": "complete",
        "windowType": "normal"
    }, function (tabs) {
        for (tab in tabs) {
            console.log(tabs[tab].url);
        }
    });
});


function renderStatus(statusText) {
    document.getElementById('status').textContent = statusText;
}

I want to set msg text as per open page. I mean if i am open facebook.com then popup msg set and display otherwise popup msg not displayed.

  • open your developer console and set a breakpoint in your content script (or use the debugger command) and I'm sure you'll see what's going wrong when you go to facebook. – James Apr 28 '15 at 09:34
  • There is nothing wrong in my above files. But i do not have an idea to display proper message while open facebook.com. Like in myscript.js when (document.domain = "facebook.com") then how i will be set massge in popup – Brijesh Mandaliya Apr 28 '15 at 10:06
  • If i would be write `document.getElementById('status').textContent = "Sucess";` in myscript then its gives up error **Cannot set property 'textContent' of null** – Brijesh Mandaliya Apr 28 '15 at 10:12

0 Answers0