mirror of
https://github.com/Zetaphor/browser-recall.git
synced 2025-12-06 02:19:37 +00:00
Start of redux
This commit is contained in:
@@ -47,13 +47,8 @@ class WebSocketClient {
|
||||
}
|
||||
|
||||
tryReconnect() {
|
||||
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
||||
this.reconnectAttempts++;
|
||||
console.log(`Attempting to reconnect (${this.reconnectAttempts}/${this.maxReconnectAttempts})...`);
|
||||
setTimeout(() => this.connect(), 2000 * this.reconnectAttempts);
|
||||
} else {
|
||||
console.log('Max reconnection attempts reached');
|
||||
}
|
||||
console.log(`Attempting to reconnect (${this.reconnectAttempts}/${this.maxReconnectAttempts})...`);
|
||||
setTimeout(() => this.connect(), 2000 * this.reconnectAttempts);
|
||||
}
|
||||
|
||||
sendMessage(data) {
|
||||
@@ -77,56 +72,10 @@ class WebSocketClient {
|
||||
|
||||
const wsClient = new WebSocketClient();
|
||||
|
||||
async function isContentScriptReady(tabId) {
|
||||
try {
|
||||
await browser.tabs.sendMessage(tabId, { type: "PING" });
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForContentScript(tabId, maxAttempts = 10) {
|
||||
console.log(`Waiting for content script in tab ${tabId}`);
|
||||
for (let i = 0; i < maxAttempts; i++) {
|
||||
if (await isContentScriptReady(tabId)) {
|
||||
console.log(`Content script ready in tab ${tabId}`);
|
||||
return true;
|
||||
}
|
||||
console.log(`Attempt ${i + 1}: Content script not ready, waiting...`);
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
}
|
||||
console.log(`Content script not ready after ${maxAttempts} attempts`);
|
||||
return false;
|
||||
}
|
||||
|
||||
async function sendMessageToTab(tabId) {
|
||||
try {
|
||||
console.log(`Checking content script status for tab ${tabId}`);
|
||||
if (await waitForContentScript(tabId)) {
|
||||
console.log(`Sending GET_PAGE_CONTENT message to tab ${tabId}`);
|
||||
await browser.tabs.sendMessage(tabId, {
|
||||
type: "GET_PAGE_CONTENT"
|
||||
});
|
||||
console.log(`Successfully sent message to tab ${tabId}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error sending message to tab ${tabId}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// Listen for messages from content scripts
|
||||
browser.runtime.onMessage.addListener((message, sender) => {
|
||||
if (message.type === "SEND_PAGE_CONTENT") {
|
||||
console.log('Received page content from tab:', sender.tab.id);
|
||||
if (message.type === "SEND_PAGE_URL") {
|
||||
console.log('Received page url from tab:', sender.tab.id);
|
||||
wsClient.sendMessage(message.data);
|
||||
}
|
||||
});
|
||||
|
||||
browser.webNavigation.onCompleted.addListener(async (details) => {
|
||||
console.log("Navigation completed", details);
|
||||
if (details.frameId === 0) {
|
||||
console.log(`Main frame navigation detected for tab ${details.tabId}`);
|
||||
await sendMessageToTab(details.tabId);
|
||||
}
|
||||
});
|
||||
@@ -3,12 +3,11 @@ console.log("Content script starting initialization...");
|
||||
function sendPageContent() {
|
||||
const pageContent = {
|
||||
url: window.location.href,
|
||||
html: document.documentElement.outerHTML,
|
||||
timestamp: new Date().toISOString().replace(/\.\d{3}Z$/, 'Z')
|
||||
};
|
||||
|
||||
browser.runtime.sendMessage({
|
||||
type: "SEND_PAGE_CONTENT",
|
||||
type: "SEND_PAGE_URL",
|
||||
data: pageContent
|
||||
});
|
||||
}
|
||||
@@ -19,10 +18,6 @@ browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
return Promise.resolve({ status: "ready" });
|
||||
}
|
||||
|
||||
if (message.type === "GET_PAGE_CONTENT") {
|
||||
sendPageContent();
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user