-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
33 lines (26 loc) · 1.18 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
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status === 'complete' && tab.url && tab.url.includes('www.reddit.com')) {
// Trigger the transformation logic
transformRedditPage(tabId, tab.url);
}
});
function transformRedditPage(tabId, url) {
var subredditName = getSubredditName(url);
// Check if the URL contains 'www.reddit.com' and '/comments/' indicating a post page
if (url.includes('www.reddit.com') && url.includes('/comments/')) {
chrome.storage.sync.get({ enabledSubreddits: [] }, function(data) {
var enabledSubreddits = data.enabledSubreddits || [];
if (isSubredditEnabled(subredditName, enabledSubreddits)) {
var modifiedUrl = 'read://https_www.reddit.com/?url=' + encodeURIComponent(url);
chrome.tabs.update(tabId, { url: modifiedUrl });
}
});
}
}
function getSubredditName(url) {
var match = url.match(/reddit\.com\/r\/([^/]+)/);
return match ? match[1] : null;
}
function isSubredditEnabled(subredditName, enabledSubreddits) {
return enabledSubreddits.includes(subredditName);
}