Skip to content

Commit ce8a5ae

Browse files
committed
AxSnapshot now added to hierarchy
1 parent 1271a22 commit ce8a5ae

File tree

1 file changed

+126
-33
lines changed

1 file changed

+126
-33
lines changed

src/extension/background.js

Lines changed: 126 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ function createTabObj(title) {
9393
// 1. param 'obj' : arg request.payload, which is an object containing a tree from snapShot.ts and a route property
9494
// 2. param tabObj: arg tabsObj[tabId], which is an object that holds info about a specific tab. Should change the name of tabObj to tabCollection or something
9595
class HistoryNode {
96-
constructor(obj, tabObj) {
96+
constructor(obj, tabObj, axSnap) {
97+
console.log('background.js: HistoryNode constructor: obj:\n', obj, '\n', 'tabObj:', tabObj);
9798
// continues the order of number of total state changes
9899
this.index = tabObj.index;
99100
tabObj.index += 1;
@@ -103,7 +104,8 @@ class HistoryNode {
103104
// marks from what branch this node is originated
104105
this.branch = tabObj.currBranch;
105106
this.stateSnapshot = obj;
106-
this.axSnapshot;
107+
this.axSnapshot = axSnap;
108+
console.log('this.axSnapshot:', this.axSnapshot);
107109
this.children = [];
108110
}
109111
}
@@ -128,6 +130,7 @@ function countCurrName(rootNode, name) {
128130
// 1. param tabObj : arg tabObj[tabId]
129131
// 2. param newNode : arg an instance of the Node class
130132
function sendToHierarchy(tabObj, newNode) {
133+
// newNode.axSnapshot = tabObj.axSnapshots[tabObj.axSnapshots.length - 1];
131134
if (!tabObj.currLocation) {
132135
tabObj.currLocation = newNode;
133136
tabObj.hierarchy = newNode;
@@ -285,9 +288,9 @@ chrome.runtime.onConnect.addListener((port) => {
285288
return true;
286289

287290
case 'jumpToSnap':
288-
console.log('background.js: tabsObj before jump:', tabsObj);
291+
// console.log('background.js: tabsObj before jump:', tabsObj);
289292
chrome.tabs.sendMessage(tabId, msg);
290-
console.log('background.js: tabsObj after jump:', tabsObj);
293+
// console.log('background.js: tabsObj after jump:', tabsObj);
291294
return true; // attempt to fix message port closing error, consider return Promise
292295

293296
case 'toggleRecord':
@@ -306,7 +309,7 @@ chrome.runtime.onConnect.addListener((port) => {
306309

307310
// INCOMING MESSAGE FROM CONTENT SCRIPT TO BACKGROUND.JS
308311
// background.js listening for a message from contentScript.js
309-
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
312+
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
310313
// AUTOMATIC MESSAGE SENT BY CHROME WHEN CONTENT SCRIPT IS FIRST LOADED: set Content
311314
if (request.type === 'SIGN_CONNECT') {
312315
return true;
@@ -399,14 +402,93 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
399402
break;
400403
}
401404
case 'recordSnap': {
405+
function addAxSnap(snap) {
406+
const pruned = pruneAxTree(snap);
407+
tabsObj[tabId].axSnapshots.push(pruned);
408+
console.log('addAxSnap axSnapshot:', pruned);
409+
// return new HistoryNode();
410+
}
411+
412+
function attachDebugger(tabId, version) {
413+
return new Promise((resolve, reject) => {
414+
chrome.debugger.attach({ tabId: tabId }, version, () => {
415+
if (chrome.runtime.lastError) {
416+
reject(chrome.runtime.lastError);
417+
} else {
418+
resolve();
419+
}
420+
});
421+
});
422+
}
423+
424+
function sendDebuggerCommand(tabId, command, params = {}) {
425+
return new Promise((resolve, reject) => {
426+
chrome.debugger.sendCommand({ tabId: tabId }, command, params, (response) => {
427+
if (chrome.runtime.lastError) {
428+
reject(chrome.runtime.lastError);
429+
} else {
430+
resolve(response);
431+
}
432+
});
433+
});
434+
}
435+
436+
function detachDebugger(tabId) {
437+
return new Promise((resolve, reject) => {
438+
chrome.debugger.detach({ tabId: tabId }, () => {
439+
if (chrome.runtime.lastError) {
440+
reject(chrome.runtime.lastError);
441+
} else {
442+
resolve();
443+
}
444+
});
445+
});
446+
}
447+
448+
async function axRecord(tabId) {
449+
try {
450+
await attachDebugger(tabId, '1.3');
451+
await sendDebuggerCommand(tabId, 'Accessibility.enable');
452+
const response = await sendDebuggerCommand(tabId, 'Accessibility.getFullAXTree');
453+
addAxSnap(response.nodes);
454+
await detachDebugger(tabId);
455+
} catch (error) {
456+
console.error('Debugger command failed:', error);
457+
}
458+
}
402459
const sourceTab = tabId;
403460
tabsObj[tabId].webMetrics = metrics;
461+
462+
// console.log('after chrome.debugger axSnap:', axSnap);
404463
if (!firstSnapshotReceived[tabId]) {
464+
console.log('!firstSnapshotReceived[tabId]:', true);
465+
// chrome.debugger.attach({ tabId: tabId }, '1.3', () => {
466+
// chrome.debugger.sendCommand({ tabId: tabId }, 'Accessibility.enable', () => {
467+
// chrome.debugger.sendCommand(
468+
// { tabId: tabId },
469+
// 'Accessibility.getFullAXTree',
470+
// {},
471+
// (response) => {
472+
// addAxSnap(response.nodes);
473+
// chrome.debugger.detach({ tabId: tabId });
474+
// },
475+
// );
476+
// });
477+
// });
478+
await axRecord(tabId);
405479
firstSnapshotReceived[tabId] = true;
406480
reloaded[tabId] = false;
407481
tabsObj[tabId].webMetrics = metrics;
408482
tabsObj[tabId].snapshots.push(request.payload);
409-
sendToHierarchy(tabsObj[tabId], new HistoryNode(request.payload, tabsObj[tabId]));
483+
sendToHierarchy(
484+
tabsObj[tabId],
485+
new HistoryNode(
486+
request.payload,
487+
tabsObj[tabId],
488+
tabsObj[tabId].axSnapshots[tabsObj[tabId].axSnapshots.length - 1],
489+
),
490+
);
491+
console.log('first snap tabsObj[tabId].axSnapshots', tabsObj[tabId].axSnapshots);
410492
if (portsArr.length > 0) {
411493
portsArr.forEach((bg) =>
412494
bg.postMessage({
@@ -415,19 +497,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
415497
}),
416498
);
417499
}
418-
chrome.debugger.attach({ tabId: tabId }, '1.3', () => {
419-
chrome.debugger.sendCommand({ tabId: tabId }, 'Accessibility.enable', () => {
420-
chrome.debugger.sendCommand(
421-
{ tabId: tabId },
422-
'Accessibility.getFullAXTree',
423-
{},
424-
(response) => {
425-
console.log(response);
426-
chrome.debugger.detach({ tabId: tabId });
427-
},
428-
);
429-
});
430-
});
500+
431501
break;
432502
}
433503

@@ -445,25 +515,48 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
445515
reloaded[tabId] = false;
446516
} else {
447517
tabsObj[tabId].snapshots.push(request.payload);
518+
// console.log('push subsequent axSnap:', axSnap);
448519
// INVOKING buildHierarchy FIGURE OUT WHAT TO PASS IN
449520
if (!tabsObj[tabId][index]) {
450-
sendToHierarchy(tabsObj[tabId], new HistoryNode(request.payload, tabsObj[tabId]));
521+
console.log('!tabsObj[tabId][index]:', true);
522+
// chrome.debugger.attach({ tabId: tabId }, '1.3', () => {
523+
// chrome.debugger.sendCommand({ tabId: tabId }, 'Accessibility.enable', () => {
524+
// chrome.debugger.sendCommand(
525+
// { tabId: tabId },
526+
// 'Accessibility.getFullAXTree',
527+
// {},
528+
// (response) => {
529+
// addAxSnap(response.nodes);
530+
// chrome.debugger.detach({ tabId: tabId });
531+
// },
532+
// );
533+
// });
534+
// });
535+
await axRecord(tabId);
536+
sendToHierarchy(
537+
tabsObj[tabId],
538+
new HistoryNode(
539+
request.payload,
540+
tabsObj[tabId],
541+
tabsObj[tabId].axSnapshots[tabsObj[tabId].axSnapshots.length - 1],
542+
),
543+
);
451544
}
452545
}
453546

454-
chrome.debugger.attach({ tabId: tabId }, '1.3', () => {
455-
chrome.debugger.sendCommand({ tabId: tabId }, 'Accessibility.enable', () => {
456-
chrome.debugger.sendCommand(
457-
{ tabId: tabId },
458-
'Accessibility.getFullAXTree',
459-
{},
460-
(response) => {
461-
console.log(response);
462-
chrome.debugger.detach({ tabId: tabId });
463-
},
464-
);
465-
});
466-
});
547+
// chrome.debugger.attach({ tabId: tabId }, '1.3', () => {
548+
// chrome.debugger.sendCommand({ tabId: tabId }, 'Accessibility.enable', () => {
549+
// chrome.debugger.sendCommand(
550+
// { tabId: tabId },
551+
// 'Accessibility.getFullAXTree',
552+
// {},
553+
// (response) => {
554+
// console.log(response);
555+
// chrome.debugger.detach({ tabId: tabId });
556+
// },
557+
// );
558+
// });
559+
// });
467560

468561
// sends new tabs obj to devtools
469562
if (portsArr.length > 0) {

0 commit comments

Comments
 (0)