SimonD
SimonD

Reputation: 1547

Simple chrome extension logging to console not working

Trying to write my first very simple chrome extension: it should write to console some message. Here is my code:

manifest.json

{
    "manifest_version": 2,
    "name" : "Hello world",
    "version" : "1.0",
    "description" : "This is a simple chrome extention",
    "background": "background.html"
}

background.html

<script type="text/javascript">

    window.onload = function() {
        window.setInterval( function() {
            console.log("Hello world");
        }, 10000);
    }
</script>

But it logs nothing into chrome console. What's wrong here?

Upvotes: 0

Views: 354

Answers (1)

woxxom
woxxom

Reputation: 73506

In modern Chrome it's better to use event pages (nonpersistent background pages) and declare only the scripts.

  • manifest.json:

    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    
  • background.js:

    window.setInterval( function() {
        console.log("Hello world");
    }, 10000);
    

    It prints in the background page console, not in a webpage console!

The only case when it makes sense to declare the html page is when you actually utilize DOM of the background page, for example for canvas.

Upvotes: 2

Related Questions