Skip to content

Instantly share code, notes, and snippets.

@paulirish
Forked from mattdesl/debugger.js
Last active May 11, 2019 09:19
Show Gist options
  • Save paulirish/920a260cadfe05e8388c to your computer and use it in GitHub Desktop.
Save paulirish/920a260cadfe05e8388c to your computer and use it in GitHub Desktop.
var Chrome = require('chrome-remote-interface')
Chrome({
chooseTab: function(tabs) {
var idx = 0
tabs.forEach(function(tab, i) {
if (tab.url === 'http://localhost:9966/')
idx = i
})
return idx
}
}, function(chrome) {
chrome.on('Debugger.scriptParsed', function(params) {
if (params.url.indexOf('http://') > -1) {
//will print ID of index.js e.g. 36
console.log("got script ID", params.scriptId)
//now to change index.js to something else
var r = Math.floor(Math.random() * 255)
chrome.Debugger.setScriptSource({
scriptId: params.scriptId,
scriptSource: 'function foo(){ document.body.style.background = "hsl(' + r + ',50%,50%)"; }\n\nfoo(); setInterval(foo, 1000);'
}, function(err, msg) {
if (err)
console.log("Err with setScriptSource", params.scriptId, msg.message)
else
console.log("setScriptSource win:", msg);
//getScriptSource always seems to work fine
chrome.Debugger.getScriptSource({
scriptId: params.scriptId
}, function(err, msg) {
if (err)
console.log("Err with getScriptSource", params.scriptId, msg.message)
else
console.log("getScriptSource", msg)
})
})
}
});
chrome.Debugger.enable(function() {
console.log("Debugger enabled")
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="index.js"></script>
</body>
</html>
function foo(){ document.body.style.background = 'blue' }
foo(); setInterval(foo, 1000);

first serve folder on 9966

http-server -p 9966

then open remote debugging chrome:

/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary http://localhost:9966/ --remote-debugging-port=9222

then run debugger

node debugger.js

and reload page in Chrome (without opening dev tools). Output:

Debugger enabled
got script ID 43
Err with setScriptSource 43 Uncaught Script not found
{ scriptSource: 'document.body.style.background = \'blue\'' }

On Chrome Canary 42.0.2303.0.

Same problem with Chrome 40.0.2214.111.

OSX Yosemite 10.10.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment