-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
64 lines (58 loc) · 1.73 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
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "openSlideShareUrl",
title: "Open SlideShare",
contexts: ["page", "selection", "link"]
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "openSlideShareUrl") {
if (info.linkUrl && isValidUrl(info.linkUrl)) {
processUrl(info.linkUrl);
} else if (tab && tab.url && isValidUrl(tab.url)) {
processUrl(tab.url);
} else {
console.log("Not a valid SlideShare URL or no URL found.");
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => alert("The URL is not a valid SlideShare URL.")
});
}
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'openUrl') {
processUrl(request.url).then(() => {
sendResponse({ status: 'completed' });
}).catch(error => {
sendResponse({ status: 'failed', message: error.message });
});
return true;
}
});
async function processUrl(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok.');
}
const html = await response.text();
const embedUrl = extractEmbedUrl(html);
if (embedUrl) {
chrome.tabs.create({ url: embedUrl });
} else {
throw new Error('No embed URL found in the provided SlideShare page.');
}
}
function isValidUrl(url) {
try {
const urlObj = new URL(url);
return urlObj.hostname.includes('slideshare.net');
} catch {
return false;
}
}
function extractEmbedUrl(html) {
const regex = /https:\/\/www\.slideshare\.net\/slideshow\/embed_code\/key\/\w+/;
const match = html.match(regex);
return match ? match[0] : null;
}