Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multitab start/stop: fixes based on #127: #134

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/embed/replay/sw.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/embed/ui.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@webrecorder/archivewebpage",
"productName": "ArchiveWeb.page",
"version": "0.9.5",
"version": "0.9.6",
"main": "index.js",
"description": "Create Web Archives directly in your browser",
"repository": "https://github.com/webrecorder/archiveweb.page",
Expand Down
58 changes: 34 additions & 24 deletions src/ext/browser-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Recorder } from "../recorder";
// ===========================================================================
const DEBUG = false;

const hasInfoBar = (self.chrome && self.chrome.braveWebrecorder != undefined);

const IS_AGREGORE = navigator.userAgent.includes("agregore-browser");


Expand All @@ -24,6 +22,7 @@ class BrowserRecorder extends Recorder {
this.tabId = debuggee.tabId;
this.openWinMap = openWinMap;
this.autorun = autorun;
this.isAttached = false;

this.flatMode = IS_AGREGORE;

Expand All @@ -39,6 +38,8 @@ class BrowserRecorder extends Recorder {
return;
}

this.isAttached = false;

if (reason === "target_closed") {
this.tabId = 0;
}
Expand Down Expand Up @@ -79,11 +80,26 @@ class BrowserRecorder extends Recorder {
}

_doDetach() {
let numOtherRecorders = 0;
for (const rec of Object.values(self.recorders)) {
if (rec.tabId !== this.tabId && rec.running) {
numOtherRecorders++;
}
}

if (numOtherRecorders > 0) {
console.log(`closing session, not detaching, ${numOtherRecorders} other recording tab(s) left`);
return this.sessionClose([]);
} else {
console.log("detaching debugger, already tabs stopped");
}

return new Promise((resolve) => {
chrome.debugger.detach(this.debuggee, () => {
if (chrome.runtime.lastError) {
console.warn(chrome.runtime.lastError.message);
}
this.isAttached = false;
resolve();
});
});
Expand All @@ -92,7 +108,9 @@ class BrowserRecorder extends Recorder {
_doStop() {
//chrome.tabs.sendMessage(this.tabId, {"msg": "stopRecord"});

chrome.debugger.onDetach.removeListener(this._onDetached);
if (!this.isAttached) {
chrome.debugger.onDetach.removeListener(this._onDetached);
}
chrome.debugger.onEvent.removeListener(this._onEvent);

if (this.db) {
Expand All @@ -105,27 +123,16 @@ class BrowserRecorder extends Recorder {
return;
}

if (hasInfoBar) {
chrome.braveWebrecorder.onCanceled.removeListener(this._onCanceled);

chrome.braveWebrecorder.hideInfoBar(this.tabId);
}

this.doUpdateStatus();
}

async _doAttach() {
this.waitForTabUpdate = false;

chrome.debugger.onDetach.addListener(this._onDetached);

chrome.debugger.onEvent.addListener(this._onEvent);

if (hasInfoBar) {
chrome.braveWebrecorder.onCanceled.addListener(this._onCanceled);

chrome.braveWebrecorder.showInfoBar(this.tabId);
if (!this.isAttached) {
chrome.debugger.onDetach.addListener(this._onDetached);
}
chrome.debugger.onEvent.addListener(this._onEvent);

const coll = await this._initDB;
if (!coll) {
Expand All @@ -135,14 +142,17 @@ class BrowserRecorder extends Recorder {
this.db = coll.store;

try {
await new Promise((resolve, reject) => {
chrome.debugger.attach(this.debuggee, "1.3", async () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError.message);
}
resolve();
if (!this.isAttached) {
await new Promise((resolve, reject) => {
chrome.debugger.attach(this.debuggee, "1.3", async () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError.message);
}
this.isAttached = true;
resolve();
});
});
});
}

await this.start();
this.failureMsg = null;
Expand Down
22 changes: 22 additions & 0 deletions src/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,28 @@ class Recorder {
}
}

async sessionClose(sessions = []) {
await this.send("Page.disable");
await this.send("Runtime.disable");
await this.send("DOMSnapshot.disable");

await this.send("Debugger.disable");

await this.send("Network.disable", null, sessions);

await this.send("Fetch.disable", null, sessions);

try {
await this.send("Media.disable", null, sessions);
} catch(e) {
// ignore
}

await this.send("Target.setAutoAttach", {autoAttach: false, waitForDebuggerOnStart: false});

await this.send("Network.setBypassServiceWorker", {bypass: false}, sessions);
}

pendingReqResp(requestId, reuseOnly = false) {
if (!this.pendingRequests[requestId]) {
if (reuseOnly || !requestId) {
Expand Down