Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
handleRawMessage(message) {
const object = /** @type {LH.Protocol.RawMessage} */(JSON.parse(message));
// Responses to commands carry "id" property, while events do not.
if (!('id' in object)) {
log.formatProtocol('<= event',
{method: object.method, params: object.params}, 'verbose');
this.emitProtocolEvent(object);
return;
}
const callback = this._callbacks.get(object.id);
if (callback) {
this._callbacks.delete(object.id);
callback.resolve(Promise.resolve().then(_ => {
if (object.error) {
log.formatProtocol('method <= browser ERR', {method: callback.method}, 'error');
throw LHError.fromProtocolMessage(callback.method, object.error);
}
log.formatProtocol('method <= browser OK',
once(eventName, cb) {
if (this._eventEmitter === null) {
throw new Error('connect() must be called before attempting to listen to events.');
}
// log event listeners being bound
log.formatProtocol('listen once for event =>', {method: eventName}, 'verbose');
this._eventEmitter.once(eventName, cb);
}
on(eventName, cb) {
if (this._eventEmitter === null) {
throw new Error('connect() must be called before attempting to listen to events.');
}
// log event listeners being bound
log.formatProtocol('listen for event =>', {method: eventName}, 'verbose');
this._eventEmitter.on(eventName, cb);
}
chrome.debugger.sendCommand({tabId: this._tabId}, method, params || {}, result => {
if (chrome.runtime.lastError) {
// The error from the extension has a `message` property that is the
// stringified version of the actual protocol error object.
const message = chrome.runtime.lastError.message || '';
let errorMessage;
try {
errorMessage = JSON.parse(message).message;
} catch (e) {}
errorMessage = errorMessage || message || 'Unknown debugger protocol error.';
log.formatProtocol('method <= browser ERR', {method}, 'error');
return reject(new Error(`Protocol error (${method}): ${errorMessage}`));
}
log.formatProtocol('method <= browser OK', {method, params: result}, 'verbose');
resolve(result);
});
});
callback.resolve(Promise.resolve().then(_ => {
if (object.error) {
log.formatProtocol('method <= browser ERR', {method: callback.method}, 'error');
throw LHError.fromProtocolMessage(callback.method, object.error);
}
log.formatProtocol('method <= browser OK',
{method: callback.method, params: object.result}, 'verbose');
return object.result;
}));
} else {
// In DevTools we receive responses to commands we did not send which we cannot act on, so we
// just log these occurrences.
const error = object.error && object.error.message;
log.formatProtocol(`disowned method <= browser ${error ? 'ERR' : 'OK'}`,
{method: 'UNKNOWN', params: error || object.result}, 'verbose');
}
}
chrome.debugger.sendCommand({tabId: this._tabId}, method, params || {}, result => {
if (chrome.runtime.lastError) {
// The error from the extension has a `message` property that is the
// stringified version of the actual protocol error object.
const message = chrome.runtime.lastError.message || '';
let errorMessage;
try {
errorMessage = JSON.parse(message).message;
} catch (e) {}
errorMessage = errorMessage || message || 'Unknown debugger protocol error.';
log.formatProtocol('method <= browser ERR', {method}, 'error');
return reject(new Error(`Protocol error (${method}): ${errorMessage}`));
}
log.formatProtocol('method <= browser OK', {method, params: result}, 'verbose');
resolve(result);
});
});
once(eventName, cb) {
if (this._eventEmitter === null) {
throw new Error('connect() must be called before attempting to listen to events.');
}
// log event listeners being bound
log.formatProtocol('listen once for event =>', {method: eventName}, 'verbose');
this._eventEmitter.once(eventName, cb);
}
callback.resolve(Promise.resolve().then(_ => {
if (object.error) {
log.formatProtocol('method <= browser ERR', {method: callback.method}, 'error');
throw LHError.fromProtocolMessage(callback.method, object.error);
}
log.formatProtocol('method <= browser OK',
{method: callback.method, params: object.result}, 'verbose');
return object.result;
}));
} else {
callback.resolve(Promise.resolve().then(_ => {
if (object.error) {
log.formatProtocol('method <= browser ERR', {method: callback.method}, 'error');
throw LHError.fromProtocolMessage(callback.method, object.error);
}
log.formatProtocol('method <= browser OK',
{method: callback.method, params: object.result}, 'verbose');
return object.result;
}));
} else {