-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
90 lines (83 loc) · 2.41 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
78
79
80
81
82
83
84
85
86
87
88
89
90
/* eslint-disable no-console */
/* eslint-disable no-undef */
importScripts('constants.js');
importScripts('utils.js');
const screenCapture = (windowID) => new Promise((resolve, reject) => {
chrome.tabs.captureVisibleTab(
windowID,
{ format: 'png' },
chromeCallbackHandler(resolve, reject),
);
});
const sendTabsMessage = (tabID, request) => new Promise((resolve, reject) => {
chrome.tabs.sendMessage(
tabID,
request,
chromeCallbackHandler(resolve, reject),
);
});
const getActiveTab = async (activeWindowID) => {
const tabs = await chrome.tabs.query({
windowId: activeWindowID,
active: true,
});
return tabs[0];
};
const messageHandler = async (request, sender, sendResponse) => {
try {
switch (request.action) {
case TAKE_SCREEN_SHOT: {
const tab = await getActiveTab(request.activeWindowID);
const [screenShotDataURI, videoPositionData] = await Promise.all([
screenCapture(request.activeWindowID),
sendTabsMessage(tab.id, {
action: GET_VIDEO_POSITION_DATA,
}),
]);
sendResponse({ dataURI: screenShotDataURI, videoPositionData });
break;
}
case GET_SCREEN_DIMENSIONS: {
const data = await sendTabsMessage(request.tabID, {
action: request.action,
});
sendResponse({ screenWidth: data.screenWidth });
break;
}
case SHOW_CAPTURE_AREA_IF_NO_VIDEO: {
await sendTabsMessage(request.tabID, {
action: request.action,
});
sendResponse({ success: true });
break;
}
case TOGGLE_SHOW_HIDE_CAPTURE_AREA:
case DISABLE_CAPTURE_AREA:
case PLAY_PAUSE_VIDEO:
case SKIP_5S:
case BACK_5S:
case INCREASE_SPEED:
case DECREASE_SPEED: {
const activeTab = await getActiveTab(request.activeWindowID);
const response = await sendTabsMessage(activeTab.id, {
action: request.action,
});
sendResponse(response);
break;
}
default:
break;
}
} catch (err) {
sendResponse({ success: false });
console.error(err.message);
}
};
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
messageHandler(request, sender, sendResponse);
return true;
});
chrome.runtime.onInstalled.addListener(() => {
chrome.tabs.create({ url: HOW_TO_USE_URL });
});
chrome.runtime.setUninstallURL(FEEDBACK_URL);