daisy
daisy

Reputation: 23501

Chromium extension: javascript not executed

Making a simple plugin , which executes javascript when a page is loaded , and I want to execute different javascript on different page

But the following code wasn't working as expected , no "alert" was triggered:

background.html:

<html><body>
<script>
chrome.webRequest.onCompleted.addListener(
  function(details) {
    alert (details.url);
  },
  {
      urls: ["*"],
      types: ["main_frame"]
  },
  []
);
</script>
</body></html>

manifest.json:

{
  "name": "JS",
  "background_page": "background.html",
  "permissions": [
    "webRequest",
    "*"
  ],
  "version":"0.10"
}

Upvotes: 0

Views: 125

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

Alerts and console.log made from the background page of an extension simply aren't visible on the general pages.

If you want to see them, you have to open the background page : Go to the extensions settings page (menu tools/extensions) and click the "background.html" link below the name of your extension.

In your case it may be better, during development phase, to simply add the console.log and alerts in the content scripts (i.e. not the background page). So you can read them without opening the background page.

EDIT : as requested, an extension that will simply alert the location :

main.js :

alert(document.location.href);

manifest.json :

{
    "name": "Any name",
    "version": "3.3",
    "background_page": "background.html",
    "content_scripts": [
        {
            "matches": ["http://*/*"],
            "all_frames" : true,
            "run_at" : "document_end",
            "js": [
                "main.js"
            ]
        }
    ]
}

Yes I tested it. And yes it's as painful as it sounds. You should use console.log instead of alert during your dev.

Upvotes: 1

Related Questions