-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
77 lines (68 loc) · 2.48 KB
/
background.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"use strict";
let manifest = chrome.runtime.getManifest();
console.log(manifest.name + " v" + manifest.version);
// add contextMenu entry to action button
function createContextMenu() {
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({
id: "WebClipElement_onPageScreenshotContextMenu",
title: "Take page screenshot...",
contexts: ["action"],
});
});
}
chrome.runtime.onInstalled.addListener(() => {
createContextMenu();
});
// enable picker when clicking the browser action
chrome.action.onClicked.addListener(async (tab) => {
console.log("[WebClipElement:BG] togglePicker");
chrome.tabs.sendMessage(
tab.id,
{
event: "togglePicker",
data: null,
}
);
});
async function takeScreenshot() {
let dataURL = await chrome.tabs.captureVisibleTab(null, {
format: 'png'
},
);
return dataURL;
}
chrome.runtime.onMessage.addListener(async (msg, sender, sendResponse) => {
console.log("[WebClipElement:BG]", msg);
const { event, data } = msg;
if (event === "takeScreenshot") {
console.log("[WebClipElement:BG] taking screenshot...");
let dataURL = await takeScreenshot();
console.log("[WebClipElement:BG]", dataURL);
let [activeTab] = await chrome.tabs.query({active: true, lastFocusedWindow: true});
chrome.tabs.sendMessage(activeTab.id, {event: "takenScreenshot", data:{dataURL: dataURL, hoverInfo: data.hoverInfo, continuePicking: data?.continuePicking}});
} else if (event === "openCroppedInNewTab") {
console.log("[WebClipElement:BG] opening cropped image in new tab...");
let dataURL = data.dataURL;
let focusNewTab = !(data?.continuePicking);
let [activeTab] = await chrome.tabs.query({active: true, lastFocusedWindow: true});
chrome.tabs.create({url: dataURL, index: activeTab.index + 1, active: focusNewTab});
}
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
console.log("[WebClipElement:BG] onContextMenuClicked:", [info, tab]);
if (info.menuItemId === "WebClipElement_onPageScreenshotContextMenu") {
console.log("[WebClipElement:BG] taking page screenshot...");
chrome.tabs.sendMessage(
tab.id,
{
event: "togglePicker",
data: { enable: false },
}
);
let dataURL = await takeScreenshot();
let focusNewTab = true;
let [activeTab] = await chrome.tabs.query({active: true, lastFocusedWindow: true});
chrome.tabs.create({url: dataURL, index: activeTab.index + 1, active: focusNewTab});
}
});