You can't pass the result of $0 back to the DevTools page. Additionally, you can't call JSON.stringify on a DOM object.
You have to do all of the work inside the eval and pass back a string/object/number.
chrome.devtools.panels.elements.onSelectionChanged.addListener(function() {
chrome.devtools.inspectedWindow.eval("$0.tagName", function(result, isException) {
if (isException) {
document.write('Error');
} else {
document.write('You clicked on a ' + result + ' element');
}
});
});
Practically speaking if you have a lot of code, you don't really want to write it all in an eval. So you can use a technique where you write a normal JavaScript 'class' and call toString() to create it
function MyCode {}
MyCode.prototype.doSomething = function() { console.log('Hello'); return true }
var run = 'new (' + MyCode.toString() + ')(). doSomething()'
chrome.devtools.inspectedWindow.eval(run, function(result){
if(result) {
console.debug("Success!");
}
});