Skip to content

Commit 300ad6d

Browse files
authored
Merge pull request #8 from oslabs-beta/oliver/ax-time-travel
Oliver/ax time travel
2 parents 1271a22 + 061ea4c commit 300ad6d

File tree

1 file changed

+63
-31
lines changed

1 file changed

+63
-31
lines changed

src/extension/background.js

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class HistoryNode {
103103
// marks from what branch this node is originated
104104
this.branch = tabObj.currBranch;
105105
this.stateSnapshot = obj;
106-
this.axSnapshot;
106+
this.axSnapshot = tabObj.axSnapshots[tabObj.axSnapshots.length - 1];
107107
this.children = [];
108108
}
109109
}
@@ -128,6 +128,7 @@ function countCurrName(rootNode, name) {
128128
// 1. param tabObj : arg tabObj[tabId]
129129
// 2. param newNode : arg an instance of the Node class
130130
function sendToHierarchy(tabObj, newNode) {
131+
// newNode.axSnapshot = tabObj.axSnapshots[tabObj.axSnapshots.length - 1];
131132
if (!tabObj.currLocation) {
132133
tabObj.currLocation = newNode;
133134
tabObj.hierarchy = newNode;
@@ -285,9 +286,7 @@ chrome.runtime.onConnect.addListener((port) => {
285286
return true;
286287

287288
case 'jumpToSnap':
288-
console.log('background.js: tabsObj before jump:', tabsObj);
289289
chrome.tabs.sendMessage(tabId, msg);
290-
console.log('background.js: tabsObj after jump:', tabsObj);
291290
return true; // attempt to fix message port closing error, consider return Promise
292291

293292
case 'toggleRecord':
@@ -306,7 +305,7 @@ chrome.runtime.onConnect.addListener((port) => {
306305

307306
// INCOMING MESSAGE FROM CONTENT SCRIPT TO BACKGROUND.JS
308307
// background.js listening for a message from contentScript.js
309-
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
308+
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
310309
// AUTOMATIC MESSAGE SENT BY CHROME WHEN CONTENT SCRIPT IS FIRST LOADED: set Content
311310
if (request.type === 'SIGN_CONNECT') {
312311
return true;
@@ -399,9 +398,67 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
399398
break;
400399
}
401400
case 'recordSnap': {
401+
console.log(
402+
'background.js: top of recordSnap: tabsObj[tabId]:',
403+
JSON.parse(JSON.stringify(tabsObj[tabId])),
404+
);
405+
function addAxSnap(snap) {
406+
const pruned = pruneAxTree(snap);
407+
tabsObj[tabId].axSnapshots.push(pruned);
408+
}
409+
410+
function attachDebugger(tabId, version) {
411+
return new Promise((resolve, reject) => {
412+
chrome.debugger.attach({ tabId: tabId }, version, () => {
413+
if (chrome.runtime.lastError) {
414+
reject(chrome.runtime.lastError);
415+
} else {
416+
resolve();
417+
}
418+
});
419+
});
420+
}
421+
422+
function sendDebuggerCommand(tabId, command, params = {}) {
423+
return new Promise((resolve, reject) => {
424+
chrome.debugger.sendCommand({ tabId: tabId }, command, params, (response) => {
425+
if (chrome.runtime.lastError) {
426+
reject(chrome.runtime.lastError);
427+
} else {
428+
resolve(response);
429+
}
430+
});
431+
});
432+
}
433+
434+
function detachDebugger(tabId) {
435+
return new Promise((resolve, reject) => {
436+
chrome.debugger.detach({ tabId: tabId }, () => {
437+
if (chrome.runtime.lastError) {
438+
reject(chrome.runtime.lastError);
439+
} else {
440+
resolve();
441+
}
442+
});
443+
});
444+
}
445+
446+
async function axRecord(tabId) {
447+
try {
448+
await attachDebugger(tabId, '1.3');
449+
await sendDebuggerCommand(tabId, 'Accessibility.enable');
450+
const response = await sendDebuggerCommand(tabId, 'Accessibility.getFullAXTree');
451+
addAxSnap(response.nodes);
452+
await detachDebugger(tabId);
453+
} catch (error) {
454+
console.error('axRecord debugger command failed:', error);
455+
}
456+
}
402457
const sourceTab = tabId;
403458
tabsObj[tabId].webMetrics = metrics;
459+
404460
if (!firstSnapshotReceived[tabId]) {
461+
await axRecord(tabId);
405462
firstSnapshotReceived[tabId] = true;
406463
reloaded[tabId] = false;
407464
tabsObj[tabId].webMetrics = metrics;
@@ -415,19 +472,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
415472
}),
416473
);
417474
}
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-
});
475+
431476
break;
432477
}
433478

@@ -447,24 +492,11 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
447492
tabsObj[tabId].snapshots.push(request.payload);
448493
// INVOKING buildHierarchy FIGURE OUT WHAT TO PASS IN
449494
if (!tabsObj[tabId][index]) {
495+
await axRecord(tabId);
450496
sendToHierarchy(tabsObj[tabId], new HistoryNode(request.payload, tabsObj[tabId]));
451497
}
452498
}
453499

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-
});
467-
468500
// sends new tabs obj to devtools
469501
if (portsArr.length > 0) {
470502
portsArr.forEach((bg) =>

0 commit comments

Comments
 (0)