Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Content Scripts for Chrome Extension

General Questions

Hello! I'm delving into the world of Chrome Extensions and am having some problems getting the overall workflow down. It seems that Google has recently switched to heavily advocating Event Pages instead of keeping everything in background.js and background.html. I take part of this to mean that we should pass off most of your extension logic to a content script.

In Google's Event Page primer, they have the content script listed in the manifest.json file. But in their event page example extension, it is brought in via this code block in background.js: chrome.tabs.executeScript(tab.id, {file: "content.js"}, function() { });

What are the advantages of doing it one way over the other?

My Code

I'm going forward with the programatic way of injecting the content script, like Google's example.

manifest.json

{     "manifest_version": 2,     "name": "Test",     "description": "Let's get this sucker working",     "version": "0.0.0.1",     "permissions": [         "tabs",         "*://*/*"     ],     "background": {         "scripts": ["background.js"],         "persistent": false     },     "browser_action": {         "default_icon": "icon.png"     } } 

background.js

chrome.browserAction.onClicked.addListener(function() {     console.log("alert from background.js");     chrome.tabs.executeScript({file: "jquery-2.0.2.min.js"}, function() {         console.log("jquery Loaded");     });     chrome.tabs.executeScript({file: "content.js"}, function() {         console.log("content loaded");     }); }); 

content.js

console.log('you\'r in the world of content.js'); var ans = {};  ans.createSidebar = function() {      return {         init: function(){             alert("why hello there");         }     } }();  ans.createSidebar.init(); 

I am able to get the first 3 console.log statements to show up in the background page's debugger. I'm also able to get the alert from content.js to show up in any website. But I'm not able to see the console.log from content.js, nor am I able to view any of the JS from content.js. I've tried looking in the "content scripts" section of the background page debugger's Sources tab. A few other posts on SO have suggested adding debugger; statements to get it to show, but I'm not having any luck with anything. The closest solution I've seen is this post, but is done by listing the content script in the manifest.

Any help would be appreciated. Thanks!

like image 251
NoR Avatar asked Jun 15 '13 01:06

NoR


2 Answers

Content scripts' console.log messages are shown in the web page's console instead of the background page's inspector.

Adding debugger; works if the Developer Tool (for the web page where your content script is injected) is opened.

Therefore, in this case, you should first activate the Developer Tool (of the web page) before clicking the browser action icon and everything should work just fine.

like image 92
方 觉 Avatar answered Oct 06 '22 14:10

方 觉


I tried to use the debuggermethod, but it doesn't not work well because the project is using require.js to bundle javascript files.

If you are also using require.js for chrome extension development, you can try adding something like this to the code base, AND change eval(xhr.responseText) to eval(xhr.responseText + "\n//@ sourceURL=" + url);. (like this question)

Then you can see the source file in your dev tool (but not the background html window)

like image 39
fengshuo Avatar answered Oct 06 '22 14:10

fengshuo