WebAutomationSessionProxy.js [plain text]
(function (sessionIdentifier, evaluate, createUUID) {
const sessionNodePropertyName = "session-node-" + sessionIdentifier;
let AutomationSessionProxy = class AutomationSessionProxy
{
constructor()
{
this._nodeToIdMap = new Map;
this._idToNodeMap = new Map;
}
evaluateJavaScriptFunction(functionString, argumentStrings, expectsImplicitCallbackArgument, frameID, callbackID, resultCallback, callbackTimeout)
{
let functionValue = evaluate("(" + functionString + ")");
if (typeof functionValue !== "function")
throw new TypeError("Script did not evaluate to a function.");
let argumentValues = argumentStrings.map(this._jsonParse, this);
let timeoutIdentifier = 0;
let resultReported = false;
let reportResult = (result) => {
if (timeoutIdentifier)
clearTimeout(timeoutIdentifier);
resultCallback(frameID, callbackID, this._jsonStringify(result), false);
resultReported = true;
};
let reportTimeoutError = () => { resultCallback(frameID, callbackID, "JavaScriptTimeout", true); };
if (expectsImplicitCallbackArgument) {
argumentValues.push(reportResult);
functionValue.apply(null, argumentValues);
if (!resultReported)
timeoutIdentifier = setTimeout(reportTimeoutError, callbackTimeout);
} else
reportResult(functionValue.apply(null, argumentValues));
}
nodeForIdentifier(identifier)
{
try {
return this._nodeForIdentifier(identifier);
} catch (error) {
return null;
}
}
_jsonParse(string)
{
if (!string)
return undefined;
return JSON.parse(string, (key, value) => this._reviveJSONValue(key, value));
}
_jsonStringify(original)
{
return JSON.stringify(original, (key, value) => this._replaceJSONValue(key, value)) || "";
}
_reviveJSONValue(key, value)
{
if (value && typeof value === "object" && value[sessionNodePropertyName])
return this._nodeForIdentifier(value[sessionNodePropertyName]);
return value;
}
_replaceJSONValue(key, value)
{
if (value instanceof Node)
return this._createNodeHandle(value);
if (value instanceof NodeList || value instanceof HTMLCollection)
value = Array.from(value).map(this._createNodeHandle, this);
return value;
}
_createNodeHandle(node)
{
return {[sessionNodePropertyName]: this._identifierForNode(node)};
}
_nodeForIdentifier(identifier)
{
let node = this._idToNodeMap.get(identifier);
if (node)
return node;
throw {name: "NodeNotFound", message: "Node with identifier '" + identifier + "' was not found"};
}
_identifierForNode(node)
{
let identifier = this._nodeToIdMap.get(node);
if (identifier)
return identifier;
identifier = "node-" + createUUID();
this._nodeToIdMap.set(node, identifier);
this._idToNodeMap.set(identifier, node);
return identifier;
}
};
return new AutomationSessionProxy;
})