15

i'm injecting a js file to every page in google chrome via

chrome.browserAction.onClicked.addListener(function(tab) {  
  chrome.tabs.executeScript(null,{file:"js/content.js"},function(resultArr){
    console.log(resultArr);
  });  
});

content.js

console.log("hello stackoverflow");

I can see hello stackoverflow printed in the console of the webpage. But i'm not able to find the source file, so i can debug it. Any idea how?

2
  • Have you tried Tools > Developer tools > Sources? Commented Nov 19, 2013 at 8:59
  • Yeah. i used to check here for js files injected through manifest.json, but can't find this. maybe becoz its injected programmatically. Commented Nov 19, 2013 at 9:02

3 Answers 3

16

Use the debugger keyword. This is like inserting a breakpoint into your JS code

so in content.js

debugger;
console.log("hello stackoverflow");
Sign up to request clarification or add additional context in comments.

3 Comments

@shahalpk you're welcome. Just remember to delete all your debugger lines when you're done!
debugger is a statement. The semicolon will be added by the Automatic Semicolon Insertion rules, but it's wrong to suggest that a semicolon has to be omitted.
@RobW I had a problem previously with the semi-colon (debugger never stopped code execution). I just tried it again in jsFiddle though and it worked so I guess my previous issue was unrelated. I've updated my answer
3

I know this is an old question, but for anyone who stumbles upon this, there is a cleaner solution.

Just add

//@ sourceURL = your_injected_file.js

at the top of your_injected_file.js. After first injection of the script, the script should be visible under Developer tools > Sources > Content Scripts.

3 Comments

It didn't work for me. I guess this depends on the type of JavaScript injection. It can be injected without actually having a file.
For debugging inline JavaScript, Chrome inspect element should work.
I don't get your point, Matt. The injected javascript is not binded to any of the page's elements. What do you mean with "inline javascript", in the context of javascript injection? The two ways I know how to inject it is using a browser extension (for automatic injection) or using the Developer Console (console tab, for manual injection). Then just add code that manipulates DOM elements using the document object. In my case, to add styles. I tried your suggestion, but yet I can't find the injected script using Windows key + P and writing the sourceURL either.
0

You can type following inside your console: angular.element($0).scope()

Then you can search for the function you want to debug by expanding the result.

You will see FunctionLocation link as one of the child under the node. You can click on the link to open file where you can put debug point and debug.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.