fix console log (#2274)

This commit is contained in:
Shuchang Zheng
2025-05-01 09:04:20 -07:00
committed by GitHub
parent 48fc490847
commit 162305306a

View File

@@ -1,6 +1,10 @@
// we only use chromium browser for now // we only use chromium browser for now
let browserNameForWorkarounds = "chromium"; let browserNameForWorkarounds = "chromium";
let _jsConsoleLog = console?.log ?? function () {}; // prevent no console.log error
let _jsConsoleError = console?.error ?? _jsConsoleLog;
let _jsConsoleWarn = console?.warn ?? _jsConsoleLog;
class SafeCounter { class SafeCounter {
constructor() { constructor() {
this.value = 0; this.value = 0;
@@ -1155,7 +1159,7 @@ function getElementContent(element, skipped_element = null) {
// childText = child.textContent.trim(); // childText = child.textContent.trim();
childText = getElementContent(child, skipped_element); childText = getElementContent(child, skipped_element);
} else { } else {
console.log("Unhandled node type: ", child.nodeType); _jsConsoleLog("Unhandled node type: ", child.nodeType);
} }
if (childText.length > 0) { if (childText.length > 0) {
childTextContentList.push(childText); childTextContentList.push(childText);
@@ -1205,7 +1209,7 @@ function getDOMElementBySkyvenElement(elementObj) {
`[unique_id="${elementObj.shadowHost}"]`, `[unique_id="${elementObj.shadowHost}"]`,
); );
if (!shadowHostEle) { if (!shadowHostEle) {
console.log( _jsConsoleLog(
"Could not find shadowHost element with unique_id: ", "Could not find shadowHost element with unique_id: ",
elementObj.shadowHost, elementObj.shadowHost,
); );
@@ -1293,7 +1297,7 @@ async function buildElementObject(
attrs[attr.name] = attrValue; attrs[attr.name] = attrValue;
} }
} else { } else {
console.warn( _jsConsoleWarn(
"element.attributes is not iterable. element_id=" + element_id, "element.attributes is not iterable. element_id=" + element_id,
); );
} }
@@ -1417,13 +1421,13 @@ async function buildElementTree(
} }
async function processElement(element, parentId) { async function processElement(element, parentId) {
if (element === null) { if (element === null) {
console.log("get a null element"); _jsConsoleLog("get a null element");
return; return;
} }
const tagName = element.tagName?.toLowerCase(); const tagName = element.tagName?.toLowerCase();
if (!tagName) { if (!tagName) {
console.log("get a null tagName"); _jsConsoleLog("get a null tagName");
return; return;
} }
@@ -1586,7 +1590,7 @@ async function buildElementTree(
...document.querySelectorAll(`label[for="${elementId}"]`), ...document.querySelectorAll(`label[for="${elementId}"]`),
]; ];
} catch (e) { } catch (e) {
console.log("failed to query labels: ", e); _jsConsoleLog("failed to query labels: ", e);
} }
} }
const labelled = currentEle.getAttribute("aria-labelledby"); const labelled = currentEle.getAttribute("aria-labelledby");
@@ -1736,7 +1740,7 @@ async function buildElementTree(
element.attributes.value === "" element.attributes.value === ""
) { ) {
// TODO (kerem): we may want to pass these elements to the LLM as empty but required fields in the future // TODO (kerem): we may want to pass these elements to the LLM as empty but required fields in the future
console.log( _jsConsoleLog(
"input element with required attribute and no value", "input element with required attribute and no value",
element, element,
); );
@@ -1747,19 +1751,19 @@ async function buildElementTree(
try { try {
ctxList = getContextByLinked(element, ctxList); ctxList = getContextByLinked(element, ctxList);
} catch (e) { } catch (e) {
console.error("failed to get context by linked: ", e); _jsConsoleError("failed to get context by linked: ", e);
} }
try { try {
ctxList = getContextByParent(element, ctxList); ctxList = getContextByParent(element, ctxList);
} catch (e) { } catch (e) {
console.error("failed to get context by parent: ", e); _jsConsoleError("failed to get context by parent: ", e);
} }
try { try {
ctxList = getContextByTable(element, ctxList); ctxList = getContextByTable(element, ctxList);
} catch (e) { } catch (e) {
console.error("failed to get context by table: ", e); _jsConsoleError("failed to get context by table: ", e);
} }
const context = ctxList.join(";"); const context = ctxList.join(";");
if (context && context.length <= 5000) { if (context && context.length <= 5000) {
@@ -1804,7 +1808,7 @@ async function buildElementsAndDrawBoundingBoxes(
} }
function captchaSolvedCallback() { function captchaSolvedCallback() {
console.log("captcha solved"); _jsConsoleLog("captcha solved");
if (!window["captchaSolvedCounter"]) { if (!window["captchaSolvedCounter"]) {
window["captchaSolvedCounter"] = 0; window["captchaSolvedCounter"] = 0;
} }
@@ -1882,7 +1886,7 @@ function generateHintStrings(count) {
function createHintMarkersForGroups(groups) { function createHintMarkersForGroups(groups) {
if (groups.length === 0) { if (groups.length === 0) {
console.log("No groups found, not adding hint markers to page."); _jsConsoleLog("No groups found, not adding hint markers to page.");
return []; return [];
} }
@@ -1921,11 +1925,11 @@ function createHintMarkersForGroups(groups) {
hintMarker.hintString.toUpperCase(), hintMarker.hintString.toUpperCase(),
); );
} catch (policyError) { } catch (policyError) {
console.warn("Could not create trusted types policy:", policyError); _jsConsoleWarn("Could not create trusted types policy:", policyError);
// Skip updating the hint marker if policy creation fails // Skip updating the hint marker if policy creation fails
} }
} else { } else {
console.error("trustedTypes is not supported in this environment."); _jsConsoleError("trustedTypes is not supported in this environment.");
} }
} }
} }
@@ -1994,7 +1998,7 @@ function safeWindowScroll(x, y) {
} else if (typeof window.scrollTo === "function") { } else if (typeof window.scrollTo === "function") {
window.scrollTo({ left: x, top: y, behavior: "instant" }); window.scrollTo({ left: x, top: y, behavior: "instant" });
} else { } else {
console.error("window.scroll and window.scrollTo are both not supported"); _jsConsoleError("window.scroll and window.scrollTo are both not supported");
} }
} }
@@ -2157,12 +2161,12 @@ function getHoverStylesMap() {
} }
} }
} catch (e) { } catch (e) {
console.warn("Could not access stylesheet:", e); _jsConsoleWarn("Could not access stylesheet:", e);
continue; continue;
} }
} }
} catch (e) { } catch (e) {
console.error("Error processing stylesheets:", e); _jsConsoleError("Error processing stylesheets:", e);
} }
return hoverMap; return hoverMap;
@@ -2173,7 +2177,7 @@ function findNodeById(arr, targetId, path = []) {
for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) {
const currentPath = [...path, arr[i].id]; const currentPath = [...path, arr[i].id];
if (arr[i].id === targetId) { if (arr[i].id === targetId) {
console.log("Lineage:", currentPath.join(" -> ")); _jsConsoleLog("Lineage:", currentPath.join(" -> "));
return arr[i]; return arr[i];
} }
if (arr[i].children && arr[i].children.length > 0) { if (arr[i].children && arr[i].children.length > 0) {
@@ -2234,7 +2238,7 @@ async function addIncrementalNodeToMap(parentNode, childrenNode) {
const maxParsedElement = 3000; const maxParsedElement = 3000;
const maxElementToWait = 100; const maxElementToWait = 100;
if ((await window.globalParsedElementCounter.get()) > maxParsedElement) { if ((await window.globalParsedElementCounter.get()) > maxParsedElement) {
console.warn( _jsConsoleWarn(
"Too many elements parsed, stopping the observer to parse the elements", "Too many elements parsed, stopping the observer to parse the elements",
); );
await window.globalParsedElementCounter.add(); await window.globalParsedElementCounter.add();
@@ -2272,7 +2276,7 @@ async function addIncrementalNodeToMap(parentNode, childrenNode) {
} }
} }
} catch (error) { } catch (error) {
console.error("Error building incremental element node:", error); _jsConsoleError("Error building incremental element node:", error);
} }
window.globalDomDepthMap.set(depth, newNodesTreeList); window.globalDomDepthMap.set(depth, newNodesTreeList);
} }
@@ -2544,11 +2548,11 @@ removeBoundingBoxes();
// Get the element tree // Get the element tree
const [elements, tree] = buildTreeFromBody(); const [elements, tree] = buildTreeFromBody();
console.log(elements); // All elements _jsConsoleLog(elements); // All elements
console.log(tree); // Tree structure _jsConsoleLog(tree); // Tree structure
// Test if a specific element is interactable // Test if a specific element is interactable
const element = document.querySelector('button'); const element = document.querySelector('button');
const hoverMap = getHoverStylesMap(); const hoverMap = getHoverStylesMap();
console.log(isInteractable(element, hoverMap)); _jsConsoleLog(isInteractable(element, hoverMap));
*/ */