Skip to content Skip to sidebar Skip to footer

Console.log Doesn't Work From Chrome.tabs.executescript

I need to execute script once user clicked my context menu item. So for the purpose I created the context menu from my background js: chrome.contextMenus.create({'title': title, 'c

Solution 1:

The only piece of code from your extension that has access to the console is the content script that is injected into the original page.

From your code it looks like you are trying to write to the console from a background script. So, to write to the console from a background page you've to inject a content script to do the job for you.

In my extensions I use my own function to write messages to the console from a background script. The code for the same is given below:

function logMessage(msg){
    chrome.tabs.executeScript({code:"console.log('"+msg+"')"});
}

Define the above as the first function in your background script and call it from anywhere in the background script to write messages to the console.

In your case you can use this from the genericOnClick function to write messages to the console.

Solution 2:

// addListener
chrome.browserAction.onClicked.addListener(function() {
    chrome.tabs.executeScript(null, {file: "content_script.js"}, function() {

        console.log("test 2");
    });
});


 // Context Menu
chrome.contextMenus.create({
    title: myTitle,
    contexts: ['page'],
    onclick: function (detail, tab) { fn(tab) }
});

so;

"permissions": [
"tabs", "http://*/*", "https://*/*"
]

chrome.tabs.executeScript(null,{code:"document.body.style.backgroundColor='red'"});

or:

// Functional structurefunctionhi() { alert("hi"); };

// hi function toString after run  function (hi)()
chrome.tabs.executeScript(null, { code: "(" + hi.toString() + ")()" });

Post a Comment for "Console.log Doesn't Work From Chrome.tabs.executescript"