-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
179 lines (160 loc) · 6.38 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
document.addEventListener('DOMContentLoaded', function () {
const pureUrlTextArea = document.getElementById('pureurl');
const copyUrlButton = document.getElementById('copy_url');
const copyMarkdownButton = document.getElementById('copy_markdown');
const copyHtmlButton = document.getElementById('copy_html');
const addToWhitelistButton = document.getElementById('add_to_whitelist');
// const closePopupButton = document.getElementById('close_popup');
const tipsDiv = document.getElementById('popup_tips');
const notificationDiv = document.getElementById('notification');
// i18n
copyUrlButton.innerHTML = chrome.i18n.getMessage('popup_copy_url');
copyMarkdownButton.innerHTML = chrome.i18n.getMessage('popup_copy_markdown');
copyHtmlButton.innerHTML = chrome.i18n.getMessage('popup_copy_html');
addToWhitelistButton.innerHTML = chrome.i18n.getMessage('popup_add_to_whitelist');
// closePopupButton.innerHTML = chrome.i18n.getMessage('popup_close');
chrome.tabs.query({ active: true, currentWindow: true }, async function (tabs) {
const currentUrl = tabs[0].url;
const cleanedUrl = await cleanUrl(currentUrl);
pureUrlTextArea.value = cleanedUrl;
const title = tabs[0].title.split(/[_|]/)[0].trim();
const markdownFormat = `[${title}](${cleanedUrl})`;
const htmlFormat = `<a href="${cleanedUrl}">${title}</a>`;
const isWhitelisted = await isUrlWhitelisted(new URL(currentUrl).hostname);
// 根据白名单匹配结果显示提示
tipsDiv.innerHTML = isWhitelisted
? chrome.i18n.getMessage('popup_tips_true') // 白名单匹配成功
: chrome.i18n.getMessage('popup_tips_false'); // 白名单匹配失败
// 按钮事件处理
copyUrlButton.onclick = () => {
copyToClipboard(cleanedUrl);
};
copyMarkdownButton.onclick = () => {
copyToClipboard(markdownFormat);
};
copyHtmlButton.onclick = () => {
copyToClipboard(htmlFormat);
};
// closePopupButton.onclick = () => {
// window.close();
// };
addToWhitelistButton.onclick = () => {
addToWhitelist(new URL(currentUrl).hostname);
};
});
async function cleanUrl(url) {
const defaultWhitelist = [
'github.com',
'*.1688.com',
'*.aliyun.com',
'*.baidu.com',
'*.bing.com',
'*.bilibili.com',
'*.fliggy.com',
'*.google.com',
'*.jd.com',
'*.jd.hk',
'*.so.com',
'*.taobao.com',
'*.tmall.com',
'*.tmall.hk',
'*.yandex.com',
'b23.tv',
'cloud.tencent.com'
];
return new Promise((resolve) => {
chrome.storage.sync.get('whitelist', function (data) {
const whitelist = data.whitelist || defaultWhitelist;
const urlObj = new URL(url);
const hostname = urlObj.hostname;
const params = new URLSearchParams(urlObj.search);
let cleanedUrl = urlObj.origin + urlObj.pathname;
// 将泛域名转换为正则表达式
const regexWhitelist = whitelist.map(domain => {
if (domain.startsWith('*.')) {
const baseDomain = domain.slice(2);
return new RegExp(`^(.+\\.)?${baseDomain.replace(/\./g, '\\.')}$`);
} else {
return new RegExp(`^${domain.replace(/\./g, '\\.')}$`);
}
});
// 检查是否匹配白名单
const isWhitelisted = regexWhitelist.some(regex => regex.test(hostname));
if (isWhitelisted) {
// 处理特定域名的参数保留逻辑
if (['taobao.com', 'tmall.com', 'tmall.hk', 'aliyun.com', 'so.com', 'google.com', 'baidu.com', 'bind.com', 'yandex.com', 'bilibili.com'].some(domain => hostname.endsWith(domain))) {
const id = params.get('id');
const liveId = params.get('liveId');
const q = params.get('q');
const wd = params.get('wd');
const text = params.get('text');
const t = params.get('t');
const sessionId = params.get('sessionId');
const retainedParams = [];
if (id) retainedParams.push(`id=${id}`);
if (liveId) retainedParams.push(`liveId=${liveId}`);
if (q) retainedParams.push(`q=${q}`);
if (wd) retainedParams.push(`wd=${wd}`);
if (text) retainedParams.push(`text=${text}`);
if (t) retainedParams.push(`t=${t}`);
if (sessionId) retainedParams.push(`sessionId=${sessionId}`);
if (retainedParams.length > 0) {
cleanedUrl += `?${retainedParams.join('&')}`;
}
}
resolve(cleanedUrl);
} else {
resolve(url); // 若不在白名单中,返回原始 URL
}
});
});
}
function isUrlWhitelisted(domain) {
return new Promise((resolve) => {
chrome.storage.sync.get('whitelist', function (data) {
const whitelist = data.whitelist || [];
const regexWhitelist = whitelist.map(domain => {
if (domain.startsWith('*.')) {
const baseDomain = domain.slice(2);
return new RegExp(`^(.+\\.)?${baseDomain.replace(/\./g, '\\.')}$`);
} else {
return new RegExp(`^${domain.replace(/\./g, '\\.')}$`);
}
});
// 检查当前域名是否匹配白名单中的任何规则
const isWhitelisted = regexWhitelist.some(regex => regex.test(domain));
resolve(isWhitelisted);
});
});
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
showNotification(chrome.i18n.getMessage('popup_copy_true'), true);
}, () => {
showNotification(chrome.i18n.getMessage('popup_copy_false'), false);
});
}
function addToWhitelist(domain) {
chrome.storage.sync.get('whitelist', function (data) {
const whitelist = data.whitelist || [];
if (!whitelist.includes(domain)) {
whitelist.push(domain);
chrome.storage.sync.set({ whitelist: whitelist }, function () {
showNotification(chrome.i18n.getMessage('popup_add_true'), true);
});
} else {
showNotification(chrome.i18n.getMessage('popup_add_exist'), false);
}
});
}
function showNotification(message, shouldClosePopup) {
notificationDiv.textContent = message;
notificationDiv.style.display = 'block';
setTimeout(() => {
notificationDiv.style.display = 'none';
if (shouldClosePopup) {
window.close(); // 关闭弹出窗口
}
}, 1500);
}
});