Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
fix: sendResponse does not work in prod version
Browse files Browse the repository at this point in the history
  • Loading branch information
gantrol committed Mar 2, 2023
1 parent baf73e7 commit 7ec17eb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 44 deletions.
44 changes: 30 additions & 14 deletions background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,48 +74,64 @@ chromeSyncGet(Settings.REQUEST_UA).then(r => {


chrome.runtime.onMessage.addListener(
async (request, sender, sendResponse) => {
(request, sender, sendResponse) => {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.type === Messages.SAVE_CHAT) {
// TODO: 完善日志体系……
const saveChat = async () => {
await saveChatToDB(request.body);
sendResponse({ success: true });
} else if (request.type === Messages.RESIZE_WINDOW) {
}
const handleGetWindowSize = async (request, sendResponse) => {
console.log(`GET_WINDOW_SIZE: ${request.body}`);
const currentWindow = await chrome.windows.getCurrent();
console.log(`GET_WINDOW_SIZE:currentWindow`);
console.log(currentWindow);
sendResponse(currentWindow);
}

async function resizeWindow() {
console.log(request.body);
const r1 = await chrome.windows.update(request.body.id, {
width: request.body.width,
height: request.body.height,
state: request.body.state,
state: request.body.state
}).catch(error => {
console.log(error);
}
);
await sendResponse(r1);
} else if (request.type === Messages.RESIZE_WINDOW_2) {
// Have no idea why RESIZE_WINDOW is not working when resizing from small to large
}

async function resizeWindow2() {
await chrome.windows.update(request.body.id, {
width: request.body.width,
height: request.body.height,
height: request.body.height
}).catch(error => {
console.log(error);
}
);
const r2 = await chrome.windows.update(request.body.id, {
state: request.body.state,
state: request.body.state
}).catch(error => {
console.log(error);
}
)
);
await sendResponse(r2);
}

if (request.type === Messages.SAVE_CHAT) {
// TODO: 完善日志体系……
saveChat();
} else if (request.type === Messages.RESIZE_WINDOW) {
resizeWindow();
} else if (request.type === Messages.RESIZE_WINDOW_2) {
// Have no idea why RESIZE_WINDOW is not working when resizing from small to large
resizeWindow2();
} else if (request.type === Messages.GET_WINDOW_SIZE) {
chrome.windows.getCurrent((currentWindow) => {
console.log(currentWindow);
sendResponse(currentWindow);
});
handleGetWindowSize(request, sendResponse);
}
return true;
}
);

Expand Down
68 changes: 38 additions & 30 deletions utils/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ export class DownloadVisitor {

// TODO: try to remove sleep...
// for now, it promise the windows is resized
console.log("sleep 20ms");
await (new Promise((resolve) => {
setTimeout(resolve, 10);
setTimeout(resolve, 20);
}));

console.log("start to download");
const main = Page.getMain();
// 处理链接分行的问题
Page.setFontWeightForAllRefs();
Expand Down Expand Up @@ -175,37 +177,43 @@ export class DownloadVisitor {
const size = await chrome.runtime.sendMessage({
type: Messages.GET_WINDOW_SIZE
});

prev_window = {...size};
size.width = width;
size.state = 'normal';
console.log(`Set to normal STATE: ${size.state}`)

const response = await chrome.runtime.sendMessage({
type: Messages.RESIZE_WINDOW,
body: size
});
console.log(response)
console.log(prev_window)
// while check current window state
let current_size;
do {
current_size = await chrome.runtime.sendMessage({
type: Messages.GET_WINDOW_SIZE
console.log(`GET window, Current STATE: ${size.state}`);
console.log(size);
if (size) {
prev_window = {...size};
// Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'width')
size.width = width;
size.state = 'normal';
console.log(`Set to normal STATE: ${size.state}`)

const response = await chrome.runtime.sendMessage({
type: Messages.RESIZE_WINDOW,
body: size
});
console.log(`Current STATE: ${current_size.state}`)
} while (current_size.state !== size.state)
const callback = async () => {
await chrome.runtime.sendMessage({
type: Messages.RESIZE_WINDOW_2,
body: prev_window
}).catch((error) => {
console.error(error);
})
console.log(response)
console.log(prev_window)
// while check current window state
let current_size;
do {
current_size = await chrome.runtime.sendMessage({
type: Messages.GET_WINDOW_SIZE
});
console.log(`Current STATE: ${current_size.state}`)
} while (current_size.state !== size.state)
const callback = async () => {
await chrome.runtime.sendMessage({
type: Messages.RESIZE_WINDOW_2,
body: prev_window
}).catch((error) => {
console.error(error);
})
}
return callback
} else {
throw Error('Cannot get window size')
}
return callback
} else {
return () => {};
}
return () => {};

}
}

0 comments on commit 7ec17eb

Please sign in to comment.