Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
63 lines (54 loc) · 1.78 KB
/
Copy pathbackground.js
File metadata and controls
63 lines (54 loc) · 1.78 KB
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
// Function to convert Medium URL to Freedium URL
function convertToFreedium(url) {
try {
// Parse the URL
const urlObj = new URL(url);
// Check if it's a Medium URL
if (urlObj.hostname === 'medium.com' || urlObj.hostname.endsWith('.medium.com')) {
// Keep the pathname and query string, but remove protocol and hostname
const path = urlObj.pathname + urlObj.search;
// Construct new URL with freedium-mirror.cfd
const newUrl = `https://freedium-mirror.cfd/medium.com${path}`;
console.log('Converted URL:', newUrl);
return newUrl;
}
return null;
} catch (error) {
console.error('Error parsing URL:', error);
return null;
}
}
// Listen for clicks on the extension icon
chrome.action.onClicked.addListener(async (tab) => {
const currentUrl = tab.url;
const freediumUrl = convertToFreedium(currentUrl);
if (freediumUrl) {
// Open the Freedium URL in a new tab
chrome.tabs.create({ url: freediumUrl, active: true });
} else {
// Show a notification if not a Medium article
chrome.tabs.sendMessage(tab.id, {
action: "showNotification",
message: "This is not a Medium article URL"
});
}
});
// Create context menu item
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "openInFreedium",
title: "Open in Freedium Mirror",
contexts: ["page", "link"]
});
});
// Handle context menu clicks
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "openInFreedium") {
let urlToConvert = info.linkUrl || tab.url;
const freediumUrl = convertToFreedium(urlToConvert);
if (freediumUrl) {
chrome.tabs.create({ url: freediumUrl, active: true });
}
}
});
// END OF FILE - No extra } here