"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.NonRecoverableDOMError = exports.InjectedScriptPollHandler = exports.FrameExecutionContext = exports.ElementHandle = void 0;
exports.assertDone = assertDone;
exports.isNonRecoverableDOMError = isNonRecoverableDOMError;
exports.kUnableToAdoptErrorMessage = void 0;
exports.throwRetargetableDOMError = throwRetargetableDOMError;
var _utilsBundle = require("../utilsBundle");
var injectedScriptSource = _interopRequireWildcard(require("../generated/injectedScriptSource"));
var _protocolError = require("./protocolError");
var js = _interopRequireWildcard(require("./javascript"));
var _progress = require("./progress");
var _utils = require("../utils");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class NonRecoverableDOMError extends Error {}
exports.NonRecoverableDOMError = NonRecoverableDOMError;
function isNonRecoverableDOMError(error) {
return error instanceof NonRecoverableDOMError;
}
class FrameExecutionContext extends js.ExecutionContext {
constructor(delegate, frame, world) {
super(frame, delegate, world || 'content-script');
this.frame = void 0;
this._injectedScriptPromise = void 0;
this.world = void 0;
this.frame = frame;
this.world = world;
}
adoptIfNeeded(handle) {
if (handle instanceof ElementHandle && handle._context !== this) return this.frame._page._delegate.adoptElementHandle(handle, this);
return null;
}
async evaluate(pageFunction, arg) {
return js.evaluate(this, true /* returnByValue */, pageFunction, arg);
}
async evaluateHandle(pageFunction, arg) {
return js.evaluate(this, false /* returnByValue */, pageFunction, arg);
}
async evaluateExpression(expression, options, arg) {
return js.evaluateExpression(this, expression, {
...options,
returnByValue: true
}, arg);
}
async evaluateExpressionHandle(expression, options, arg) {
return js.evaluateExpression(this, expression, {
...options,
returnByValue: false
}, arg);
}
createHandle(remoteObject) {
if (this.frame._page._delegate.isElementHandle(remoteObject)) return new ElementHandle(this, remoteObject.objectId);
return super.createHandle(remoteObject);
}
injectedScript() {
if (!this._injectedScriptPromise) {
const custom = [];
const selectorsRegistry = this.frame._page.context().selectors();
for (const [name, {
source
}] of selectorsRegistry._engines) custom.push(`{ name: '${name}', engine: (${source}) }`);
const sdkLanguage = this.frame.attribution.playwright.options.sdkLanguage;
const source = `
(() => {
const module = {};
${injectedScriptSource.source}
return new (module.exports.InjectedScript())(
globalThis,
${(0, _utils.isUnderTest)()},
"${sdkLanguage}",
${JSON.stringify(selectorsRegistry.testIdAttributeName())},
${this.frame._page._delegate.rafCountForStablePosition()},
"${this.frame._page._browserContext._browser.options.name}",
[${custom.join(',\n')}]
);
})();
`;
this._injectedScriptPromise = this.rawEvaluateHandle(source).then(objectId => new js.JSHandle(this, 'object', 'InjectedScript', objectId));
}
return this._injectedScriptPromise;
}
}
exports.FrameExecutionContext = FrameExecutionContext;
class ElementHandle extends js.JSHandle {
constructor(context, objectId) {
super(context, 'node', undefined, objectId);
this.__elementhandle = true;
this._page = void 0;
this._frame = void 0;
this._page = context.frame._page;
this._frame = context.frame;
this._initializePreview().catch(e => {});
}
async _initializePreview() {
const utility = await this._context.injectedScript();
this._setPreview(await utility.evaluate((injected, e) => 'JSHandle@' + injected.previewNode(e), this));
}
asElement() {
return this;
}
async evaluateInUtility(pageFunction, arg) {
try {
const utility = await this._frame._utilityContext();
return await utility.evaluate(pageFunction, [await utility.injectedScript(), this, arg]);
} catch (e) {
if (js.isJavaScriptErrorInEvaluate(e) || (0, _protocolError.isSessionClosedError)(e)) throw e;
return 'error:notconnected';
}
}
async evaluateHandleInUtility(pageFunction, arg) {
try {
const utility = await this._frame._utilityContext();
return await utility.evaluateHandle(pageFunction, [await utility.injectedScript(), this, arg]);
} catch (e) {
if (js.isJavaScriptErrorInEvaluate(e) || (0, _protocolError.isSessionClosedError)(e)) throw e;
return 'error:notconnected';
}
}
async evaluatePoll(progress, pageFunction, arg) {
try {
const utility = await this._frame._utilityContext();
const poll = await utility.evaluateHandle(pageFunction, [await utility.injectedScript(), this, arg]);
const pollHandler = new InjectedScriptPollHandler(progress, poll);
return await pollHandler.finish();
} catch (e) {
if (js.isJavaScriptErrorInEvaluate(e) || (0, _protocolError.isSessionClosedError)(e)) throw e;
return 'error:notconnected';
}
}
async ownerFrame() {
const frameId = await this._page._delegate.getOwnerFrame(this);
if (!frameId) return null;
const frame = this._page._frameManager.frame(frameId);
if (frame) return frame;
for (const page of this._page._browserContext.pages()) {
const frame = page._frameManager.frame(frameId);
if (frame) return frame;
}
return null;
}
async isIframeElement() {
return this.evaluateInUtility(([injected, node]) => node && (node.nodeName === 'IFRAME' || node.nodeName === 'FRAME'), {});
}
async contentFrame() {
const isFrameElement = throwRetargetableDOMError(await this.isIframeElement());
if (!isFrameElement) return null;
return this._page._delegate.getContentFrame(this);
}
async getAttribute(name) {
return throwRetargetableDOMError(await this.evaluateInUtility(([injected, node, name]) => {
if (node.nodeType !== Node.ELEMENT_NODE) throw injected.createStacklessError('Node is not an element');
const element = node;
return {
value: element.getAttribute(name)
};
}, name)).value;
}
async inputValue() {
return throwRetargetableDOMError(await this.evaluateInUtility(([injected, node]) => {
const element = injected.retarget(node, 'follow-label');
if (!element || element.nodeName !== 'INPUT' && element.nodeName !== 'TEXTAREA' && element.nodeName !== 'SELECT') throw injected.createStacklessError('Node is not an ,