mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(page): introduce consoleMessage.location() (#3365)
This patch adds a new consoleMessage.location() method that returns console message origins. Fixes #3029
This commit is contained in:
36
lib/Page.js
36
lib/Page.js
@@ -187,11 +187,11 @@ class Page extends EventEmitter {
|
||||
* @param {!Protocol.Log.entryAddedPayload} event
|
||||
*/
|
||||
_onLogEntryAdded(event) {
|
||||
const {level, text, args, source} = event.entry;
|
||||
const {level, text, args, source, url, lineNumber} = event.entry;
|
||||
if (args)
|
||||
args.map(arg => helper.releaseObject(this._client, arg));
|
||||
if (source !== 'worker')
|
||||
this.emit(Page.Events.Console, new ConsoleMessage(level, text));
|
||||
this.emit(Page.Events.Console, new ConsoleMessage(level, text, undefined, { url, lineNumber }));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -510,7 +510,14 @@ class Page extends EventEmitter {
|
||||
async _onConsoleAPI(event) {
|
||||
const context = this._frameManager.executionContextById(event.executionContextId);
|
||||
const values = event.args.map(arg => createJSHandle(context, arg));
|
||||
this._addConsoleMessage(event.type, values);
|
||||
const location = {};
|
||||
if (event.stackTrace && event.stackTrace.callFrames) {
|
||||
const {url, lineNumber, columnNumber} = event.stackTrace.callFrames[0];
|
||||
if (url) location.url = url;
|
||||
if (lineNumber) location.lineNumber = lineNumber;
|
||||
if (columnNumber) location.columnNumber = columnNumber;
|
||||
}
|
||||
this._addConsoleMessage(event.type, values, location);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -567,8 +574,9 @@ class Page extends EventEmitter {
|
||||
/**
|
||||
* @param {string} type
|
||||
* @param {!Array<!Puppeteer.JSHandle>} args
|
||||
* @param {ConsoleMessage.Location} location
|
||||
*/
|
||||
_addConsoleMessage(type, args) {
|
||||
_addConsoleMessage(type, args, location) {
|
||||
if (!this.listenerCount(Page.Events.Console)) {
|
||||
args.forEach(arg => arg.dispose());
|
||||
return;
|
||||
@@ -581,7 +589,7 @@ class Page extends EventEmitter {
|
||||
else
|
||||
textTokens.push(helper.valueFromRemoteObject(remoteObject));
|
||||
}
|
||||
const message = new ConsoleMessage(type, textTokens.join(' '), args);
|
||||
const message = new ConsoleMessage(type, textTokens.join(' '), args, location);
|
||||
this.emit(Page.Events.Console, message);
|
||||
}
|
||||
|
||||
@@ -1226,16 +1234,25 @@ Page.Events = {
|
||||
* @property {("Strict"|"Lax")=} sameSite
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} ConsoleMessage.Location
|
||||
* @property {string=} url
|
||||
* @property {number=} lineNumber
|
||||
* @property {number=} columnNumber
|
||||
*/
|
||||
|
||||
class ConsoleMessage {
|
||||
/**
|
||||
* @param {string} type
|
||||
* @param {string} text
|
||||
* @param {!Array<!Puppeteer.JSHandle>} args
|
||||
* @param {ConsoleMessage.Location} location
|
||||
*/
|
||||
constructor(type, text, args = []) {
|
||||
constructor(type, text, args = [], location = {}) {
|
||||
this._type = type;
|
||||
this._text = text;
|
||||
this._args = args;
|
||||
this._location = location;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1258,6 +1275,13 @@ class ConsoleMessage {
|
||||
args() {
|
||||
return this._args;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {Object}
|
||||
*/
|
||||
location() {
|
||||
return this._location;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user