0

I know on stackoverflow there are many solutions but still I failed, I cannot do it. I want to print hello world in main page console from extension. How can I do it? I will share what I did until now.

(As you see in the picture, there is nothing on the main console.)

html of extension:

<!DOCTYPE html>
<html>
    <head>
        <title>Saitama Techno</title>
    </head>
    <body>
        <h1>Welcome to Saitama Techno</h1>
        <button id="mybutton">Click me</button>
        <script src="myscript.js"></script>
    </body>
</html>

javascript code:

function btnclicked(){
    console.log("Hello from extension");
    chrome.extension.getBackgroundPage().console.log('hello to main console');
}
document.getElementById("mybutton").addEventListener("click", btnclicked);

manifest.json:

{
    "name": "Saitama Techno scraping",
    "version": "1.0.0",
    "description": "webscraping duuuuuuude",
    "manifest_version": 3,
    "author": "Enes Telli",
    "action": {
        "default_popup": "index.html",
        "default_title": "Saitama Techno"
    },
    "permissions": ["https://*/*",
    "http://*/*", "storage", "tabs", "declarativeContent", "activeTab", "scripting"]
}

myimage

  • You can't use chrome.extension.getBackgroundPage() with manifest 3 (at least I don't know what it can be return as mv3 extension have not background page). If you want to continue with mv3 you have first to declare a service worker in manifest file. Then, in order to log a message in service worker you'll have to send a message from "index.html" to the service worker (SW) with **chrome.runtime.sendMessage()**. SW will listen to that message through **chrome.runtime.onMessage.addListener** and log what you want to. – Robbi Jan 01 '23 at 20:44
  • To print in the main page console you need to run the code as a content script e.g. via chrome.scripting.executeScript ([examples](https://stackoverflow.com/a/67227376)). – wOxxOm Jan 01 '23 at 21:34

0 Answers0