-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
160 lines (132 loc) · 4.03 KB
/
popup.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
async function requestPermissions() {
const permissionGranted = await chrome.permissions.request({
permissions: ["activeTab"],
});
if (!permissionGranted) {
alert(
"You can't summarize without allowing access to the currently active tab.",
);
return false;
}
return true;
}
async function getActiveTab() {
const platformInfo = await chrome.runtime.getPlatformInfo();
const browserInfo =
typeof chrome.runtime.getBrowserInfo === 'function' &&
(await chrome.runtime.getBrowserInfo());
// Note, _hoping_ by 119 this works, but there's no guarantee.
if (
platformInfo.os === 'android' &&
browserInfo?.version &&
Number.parseInt(browserInfo.version, 10) <= 118
) {
return null;
}
const tabs = await chrome.tabs.query(
{
active: true,
lastFocusedWindow: true,
}
);
const tab =
tabs.find(
(tab) =>
tab?.url?.startsWith('http://') || tab?.url?.startsWith('https://'),
) || tabs[0];
if (tab?.url?.startsWith('about:reader?url=')) {
const newUrl = new URL(tab.url);
tab.url = newUrl.searchParams.get('url');
}
if (!tab || !tab.url) {
return null;
}
return tab;
}
async function setup() {
const summarizeSection = document.querySelector("#summarize");
const permissionsSection = document.querySelector("#permissions");
const hasPermissions = await chrome.permissions.contains({
permissions: ["activeTab"],
});
permissionsSection.hidden = hasPermissions;
summarizeSection.hidden = !hasPermissions;
const requestButton = document.querySelector("#request_permissions");
requestButton.addEventListener("click", async () => {
const res = await requestPermissions();
if (res) {
// This seems to be required to reload things and make the permission become 'active'.
window.close();
}
});
const summarizePageButton = document.querySelector("#summarize_page");
summarizePageButton.addEventListener("click", handleSummarizeClick);
const summaryType = document.querySelector("#summary_type");
const targetLanguage = document.querySelector("#target_language");
const apiEngine = document.querySelector("#engine");
const apiToken = document.querySelector("#api_key");
const settings = [
summaryType,
targetLanguage,
apiEngine,
];
const save = async () => {
await chrome.runtime.sendMessage({
type: "save_settings",
engine: apiEngine.value,
summary_type: summaryType.value,
target_language: targetLanguage.value,
api_token: apiToken.value,
});
};
settings.forEach((setting) => {
setting.addEventListener("change", async () => {
await save();
});
});
const saveButton = document.querySelector("#save_api_token");
saveButton.addEventListener("click", async () => {
await save();
});
const storageSettings = await chrome.storage.local.get("summarizer_settings");
const savedSettings = storageSettings?.summarizer_settings || {};
if (savedSettings.summary_type) {
summaryType.value = savedSettings.summary_type;
}
if (savedSettings.api_token) {
apiToken.value = savedSettings.api_token;
}
if (savedSettings.engine) {
// If the api token is not set - we default to free since it will be implied anyways.
if (apiToken.value.trim().length !== 0) {
apiEngine.value = savedSettings.engine;
}
}
if (savedSettings.target_language) {
targetLanguage.value = savedSettings.target_language;
}
}
async function handleSummarizeClick() {
let tab = await getActiveTab();
if (!tab) {
const res = await requestPermissions();
// cant do anything if we don't have tab perms.
if (!res) {
return;
}
}
if (!tab) return;
const {url} = tab;
const searchParams = new URLSearchParams();
searchParams.set("url", url);
await chrome.windows.create({
url: `${chrome.runtime.getURL(
'summarize_popup.html',
)}?${searchParams.toString()}`,
focused: true,
width: 600,
height: 500,
type: 'popup',
});
}
document.addEventListener('DOMContentLoaded', setup);