forked from eljeffeg/SmartCopy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffscreen.js
More file actions
26 lines (22 loc) · 730 Bytes
/
offscreen.js
File metadata and controls
26 lines (22 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const iframe = document.getElementById("offscreen");
console.log("Offscreen script loaded...");
let listenerAdded = false;
let pendingResponse = null;
function handleMessage(event) {
if (pendingResponse) {
pendingResponse(event.data);
pendingResponse = null;
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "offscreen" && sender.id === chrome.runtime.id) {
if (!listenerAdded) {
window.addEventListener("message", handleMessage);
listenerAdded = true;
}
pendingResponse = sendResponse;
iframe.contentWindow.postMessage(request.data, "*");
return true;
}
return false;
});