chrome-remote-interface
Advanced tools
Comparing version 0.2.0 to 0.3.0
#!/usr/bin/env node | ||
var repl = require('repl'); | ||
var ChromeInterface = require('../'); | ||
var protocol = require('../lib/Inspector.json'); | ||
var Chrome = require('../'); | ||
ChromeInterface(function (chrome) { | ||
Chrome(function (chrome) { | ||
var chromeRepl = repl.start({ | ||
@@ -11,2 +12,3 @@ 'prompt': 'chrome> ' | ||
// disconnect on exit | ||
chromeRepl.on('exit', function () { | ||
@@ -16,5 +18,7 @@ chrome.close(); | ||
for (var domain in chrome) { | ||
chromeRepl.context[domain] = chrome[domain]; | ||
// add protocol API | ||
for (var domainIdx in protocol.domains) { | ||
var domainName = protocol.domains[domainIdx].domain; | ||
chromeRepl.context[domainName] = chrome[domainName]; | ||
} | ||
}); |
@@ -30,1 +30,3 @@ var events = require('events'); | ||
}; | ||
module.exports.listTabs = Chrome.listTabs; |
@@ -9,7 +9,10 @@ var protocol = require('./Inspector.json'); | ||
var self = this; | ||
addCommandShorthands.call(self); | ||
self.host = options.host; | ||
self.port = options.port; | ||
self.chooseTab = options.chooseTab; | ||
self.notifier = notifier; | ||
self.callbacks = {}; | ||
self.nextCommandId = 1; | ||
connectToChrome.call(self, options.host, options.port, options.chooseTab); | ||
addCommandShorthands.call(self); | ||
connectToChrome.call(self); | ||
}; | ||
@@ -19,2 +22,26 @@ | ||
Chrome.listTabs = function (options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = undefined; | ||
} | ||
options = options || {}; | ||
options.host = options.host || 'localhost'; | ||
options.port = options.port || 9222; | ||
var options = {'host': options.host, 'port': options.port, 'path': '/json'}; | ||
var request = http.get(options, function (response) { | ||
var data = ''; | ||
response.on('data', function (chunk) { | ||
data += chunk; | ||
}); | ||
response.on('end', function () { | ||
var tabs = JSON.parse(data); | ||
callback(null, tabs); | ||
}); | ||
}); | ||
request.on('error', function (err) { | ||
callback(err); | ||
}); | ||
}; | ||
Chrome.prototype.send = function (method, params, callback) { | ||
@@ -43,6 +70,6 @@ var self = this; | ||
var self = this; | ||
Chrome.prototype[domainName][command.name] = function (params, callback) { | ||
self[domainName][command.name] = function (params, callback) { | ||
self.send(domainName + '.' + command.name, params, callback); | ||
}; | ||
Chrome.prototype[domainName][command.name].help = command; | ||
self[domainName][command.name].help = command; | ||
} | ||
@@ -52,6 +79,6 @@ | ||
var self = this; | ||
Chrome.prototype[domainName][event.name] = function (handler) { | ||
self[domainName][event.name] = function (handler) { | ||
self.on(domainName + '.' + event.name, handler); | ||
}; | ||
Chrome.prototype[domainName][event.name].help = event; | ||
self[domainName][event.name].help = event; | ||
} | ||
@@ -61,3 +88,3 @@ | ||
var self = this; | ||
Chrome.prototype[domainName][type.id] = type; | ||
self[domainName][type.id] = type; | ||
} | ||
@@ -70,3 +97,3 @@ | ||
var domainName = domain.domain; | ||
Chrome.prototype[domainName] = {}; | ||
self[domainName] = {}; | ||
// add commands | ||
@@ -99,14 +126,11 @@ var commands = domain.commands; | ||
function connectToChrome(host, port, chooseTab) { | ||
function connectToChrome() { | ||
var self = this; | ||
var options = {'host': host, 'port': port, 'path': '/json'}; | ||
var request = http.get(options, function (response) { | ||
var data = ''; | ||
response.on('data', function (chunk) { | ||
data += chunk; | ||
}); | ||
response.on('end', function () { | ||
var error; | ||
var tabs = JSON.parse(data); | ||
var tab = tabs[chooseTab(tabs)]; | ||
var options = {'host': self.host, 'port': self.port}; | ||
Chrome.listTabs(options, function (err, tabs) { | ||
if (err) { | ||
self.notifier.emit('error', err); | ||
} else { | ||
var tabError; | ||
var tab = tabs[self.chooseTab(tabs)]; | ||
if (tab) { | ||
@@ -118,14 +142,11 @@ var tabDebuggerUrl = tab.webSocketDebuggerUrl; | ||
// a WebSocket is already connected to this tab? | ||
error = new Error('Unable to connect to the WebSocket'); | ||
self.notifier.emit('error', error); | ||
tabError = new Error('Chosen tab does not support inspection'); | ||
self.notifier.emit('error', tabError); | ||
} | ||
} else { | ||
error = new Error('Invalid tab index'); | ||
self.notifier.emit('error', error); | ||
tabError = new Error('Invalid tab index'); | ||
self.notifier.emit('error', tabError); | ||
} | ||
}); | ||
} | ||
}); | ||
request.on('error', function (error) { | ||
self.notifier.emit('error', error); | ||
}); | ||
} | ||
@@ -160,4 +181,4 @@ | ||
}); | ||
self.ws.on('error', function (error) { | ||
self.notifier.emit('error', error); | ||
self.ws.on('error', function (err) { | ||
self.notifier.emit('error', err); | ||
}); | ||
@@ -164,0 +185,0 @@ } |
@@ -5,6 +5,3 @@ { | ||
"contributors": [ | ||
{ | ||
"name": "Andrey Sidorov", | ||
"email": "sidoares@yandex.ru" | ||
} | ||
"Andrey Sidorov <sidoares@yandex.ru>" | ||
], | ||
@@ -14,3 +11,3 @@ "description": "Chrome Remote Debugging Protocol interface", | ||
"homepage": "https://github.com/cyrus-and/chrome-remote-interface", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"repository": { | ||
@@ -17,0 +14,0 @@ "type": "git", |
@@ -93,5 +93,5 @@ chrome-remote-interface | ||
- `chooseTab`: callback used to determine which remote tab attach to. Takes the | ||
JSON array returned by `http://host:port/json` containing the tab list and | ||
must return the numeric index of a tab. Defaults to a function that returns | ||
the active one (`function (tabs) { return 0; }`). | ||
array returned by `http://host:port/json` containing the tab list and must | ||
return the numeric index of a tab. Defaults to a function that returns the | ||
active one (`function (tabs) { return 0; }`). | ||
@@ -113,3 +113,3 @@ `callback` is a listener automatically added to the `connect` event of the | ||
function (error) {} | ||
function (err) {} | ||
@@ -119,4 +119,30 @@ Emitted if `http://host:port/json` can't be reached or if it's not possible to | ||
`error` is an instance of `Error`. | ||
`err` is an instance of `Error`. | ||
### module.listTabs([options], callback) | ||
Request the list of the available open tabs of the remote Chrome instance. | ||
`options` is an object with the following optional properties: | ||
- `host`: [Remote Debugging Protocol][1] host. Defaults to `localhost`; | ||
- `port`: [Remote Debugging Protocol][1] port. Defaults to `9222`. | ||
`callback` is executed when the list is correctly received, it gets the | ||
following arguments: | ||
- `err`: a `Error` object indicating the success status; | ||
- `tabs`: the array returned by `http://host:port/json` containing the tab list. | ||
For example: | ||
```javascript | ||
var Chrome = require('chrome-remote-interface'); | ||
Chrome.listTabs(function (err, tabs) { | ||
if (!err) { | ||
console.log(tabs); | ||
} | ||
}); | ||
``` | ||
### Class: Chrome | ||
@@ -123,0 +149,0 @@ |
@@ -11,3 +11,3 @@ var Chrome = require('../'); | ||
done(); | ||
}).on('error', function (error) { | ||
}).on('error', function () { | ||
assert(false); | ||
@@ -14,0 +14,0 @@ }); |
@@ -45,4 +45,4 @@ var Chrome = require('../'); | ||
assert(false); | ||
}).on('error', function (error) { | ||
assert(error instanceof Error); | ||
}).on('error', function (err) { | ||
assert(err instanceof Error); | ||
done(); | ||
@@ -54,4 +54,4 @@ }); | ||
assert(false); | ||
}).on('error', function (error) { | ||
assert(error instanceof Error); | ||
}).on('error', function (err) { | ||
assert(err instanceof Error); | ||
done(); | ||
@@ -63,4 +63,4 @@ }); | ||
assert(false); | ||
}).on('error', function (error) { | ||
assert(error instanceof Error); | ||
}).on('error', function (err) { | ||
assert(err instanceof Error); | ||
done(); | ||
@@ -75,5 +75,5 @@ }); | ||
assert(false); | ||
}).on('error', function (error) { | ||
}).on('error', function (err) { | ||
chrome.close(); | ||
assert(error instanceof Error); | ||
assert(err instanceof Error); | ||
done(); | ||
@@ -80,0 +80,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
240667
14
4177
227
1