Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Google Chrome background script? [duplicate]

I have very simple extension:

manifest.json

{   "name": "historyCleaner",   "version": "0.1.1",   "manifest_version": 1,   "description": "This is my first Chrome extension",   "background": {     "scripts": ["cleaner.js"]   },    "permissions": [     "history"   ] } 

cleaner.js

chrome.history.onVisited.addListener(function(HistoryItem result) {    console.log("it works!");   alert("it works!");  }); 

I've loaded it in Google Chrome, it is turned on and... it doesn't work. It doesn't log anything in console, it doesn't alert anything and what is worse, I can't find it in developers tools "Scripts" tab. How can I find why it doesn't work?

//edit

I've changed manifest.json to this one:

{   "name": "historyCleaner",   "version": "0.1.5",   "manifest_version": 1,   "description": "This is my first Chrome extension",   "background_page": "background.html",   "permissions": [     "history",     "background"   ] } 

And embeded JavaScript in background.html

like image 872
ciembor Avatar asked Apr 10 '12 01:04

ciembor


People also ask

How do you debug a background script?

The background script is like any other JavaScript code. You can debug it using same tools you debug other JavaScript code in Chrome. Now you can debug any extension that have a background page or script. Just scroll to the extension you want to debug and click on the background page link to inspect it.

How to debug Google chrome?

Press the F12 function key in the Chrome browser to launch the JavaScript debugger and then click "Scripts". Choose the JavaScript file on top and place the breakpoint to the debugger for the JavaScript code.

What is Chrome background script extension?

Adding background script to manifest. Background script can react to browser events and perform certain actions, the background page is loaded when it is needed, and unloaded when it goes idle. To use the background file, declare it as follows in the manifest.


1 Answers

enter image description here

and also if your console.log("it works!"); does not show up, then that's mean chrome.history.onVisited is not fired yet.

ps: For function(HistoryItem result), you may want to change it to function(result).

like image 128
Derek 朕會功夫 Avatar answered Oct 02 '22 20:10

Derek 朕會功夫