-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
131 lines (110 loc) · 3.89 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
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
//background.js
// Function to create a new tab with the proxied URL
function openProxiedUrl(url) {
try {
let cleanedUrl = cleanUrl(url);
let processedUrl = processArchiveUrl(cleanedUrl);
let finalUrl = cleanTrackingParams(processedUrl);
const encodedUrl = encodeURIComponent(finalUrl);
const proxyUrl = `https://archive.is/?run=1&url=${encodedUrl}`;
chrome.tabs.create({ url: proxyUrl });
} catch (error) {
console.error("Failed to open proxied URL: ", error);
}
}
// Listener for the extension icon click
chrome.action.onClicked.addListener((tab) => {
const currentUrl = tab.url;
openProxiedUrl(currentUrl);
});
// Create context menu for hyperlinks and selected text
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "archive-selection",
title: "Archive Embedded URL in Selection",
contexts: ["selection", "link"]
});
});
// Listener for right-click menu
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.linkUrl) {
openProxiedUrl(info.linkUrl);
} else if (info.selectionText) {
let url = extractUrl(info.selectionText);
// Check for an embedded hyperlink within the selection
if (info.linkUrl) {
url = info.linkUrl; // Use the embedded hyperlink if available
}
if (url) {
openProxiedUrl(url);
} else {
console.log("No valid URL found in the selected text.");
}
}
});
// List of common tracking parameters to be removed
const trackingParams = new Set([
"utm_source", "utm_medium", "utm_campaign", "utm_content", "utm_term",
"fbclid", "gclid", "dclid", "gbraid", "wbraid", "msclkid", "tclid",
"aff_id", "affiliate_id", "ref", "referer", "campaign_id", "ad_id",
"adgroup_id", "adset_id", "creativetype", "placement", "network",
"mc_eid", "mc_cid", "si", "icid", "_ga", "_gid", "scid", "click_id",
"trk", "track", "trk_sid", "sid", "mibextid", "fb_action_ids",
"fb_action_types", "twclid", "igshid", "s_kwcid", "sxsrf", "sca_esv",
"source", "tbo", "sa", "ved", "pi" // sxsrf might be needed on some sites, but Google uses it for tracking
]);
// Function to clean tracking parameters from the URL
function cleanTrackingParams(url) {
let uri = new URL(url);
// Remove tracking parameters
trackingParams.forEach(param => uri.searchParams.delete(param));
// Additional handling for YouTube URLs
if (uri.host.includes("youtube.com") || uri.host.includes("youtu.be")) {
let nestedQueryParams = uri.searchParams.get("q");
if (nestedQueryParams) {
let nestedUri = new URL(nestedQueryParams);
let newNestedUri = new URL(nestedUri.origin + nestedUri.pathname);
nestedUri.searchParams.forEach((value, key) => {
newNestedUri.searchParams.append(key, value);
});
uri.searchParams.set("q", newNestedUri.toString());
}
uri.pathname = uri.pathname.replace("/shorts/", "/v/");
}
else if(uri.host.endsWith(".substack.com")){
uri.pathname = uri.pathname + "/?no_cover=true";
}
return uri.toString();
}
// Function to clean erroneous prefixes from the URL
function cleanUrl(url) {
let lastValidUrlIndex = url.lastIndexOf("https://");
return lastValidUrlIndex !== -1 ? url.substring(lastValidUrlIndex).replace(/%09+/g, "") : url.replace(/%09+/g, "");
}
// Function to process archive-specific URLs
function processArchiveUrl(url) {
const pattern = /archive\.[a-z]+\/o\/[a-zA-Z0-9]+\/(.+)/;
const match = url.match(pattern);
return match ? match[1] : url;
}
function isValidUrl(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}
function extractUrl(text) {
// Regular expression to match URLs
const urlPattern = /https?:\/\/[^\s/$.?#].[^\s]*/g;
const match = text.match(urlPattern);
if (match && match.length > 0) {
let url = match[0];
// Clean the URL by removing erroneous prefixes
url = cleanUrl(url);
return url;
} else {
return null;
}
}