-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbackground.js
More file actions
executable file
·168 lines (146 loc) · 4.45 KB
/
Copy pathbackground.js
File metadata and controls
executable file
·168 lines (146 loc) · 4.45 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
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
var sbId, sbPrevUrl;
// Create the window and save the tab id
browser.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.type === 'share-backid') {
browser.windows.create(request.data).then(function (tab) {
sbId = tab.id;
sbPrevUrl = request.data.url;
return tab.id;
});
} else if (request.type === 'share-backid-container') {
browser.tabs.create(request.data).then(function (tab) {
sbId = tab.id;
sbPrevUrl = request.data.url;
return tab.id;
});
}
});
// Autoclose the window when the url change
browser.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (sbId === tab.windowId) {
console.log(sbPrevUrl)
const cssInjectionsFor = [
'https://www.reddit.com/',
'shaarli'
];
if (sbPrevUrl !== undefined) {
// True if any of the listed links show up
const shallInjectCss = cssInjectionsFor.some(function (urlPart) {
return sbPrevUrl.includes(urlPart);
});
if (shallInjectCss) {
browser.tabs.insertCSS(tabId, {
code: 'body { overflow: auto !important; }'
});
}
}
urlChangeStrategy(tabId, sbPrevUrl);
}
});
const closeWhen = ['latest_status_id='];
const closeCarefullyWhen = [
'reddit\.com\/user\/.+\/comments\/.+\/',
'reddit\.com\/r\/.+\/comments\/.+\/'
];
const closeCarefullyOnlyIf = [
'https://www.facebook.com/',
'https://www.linkedin.com/feed/',
'https://x.com/home'
];
function urlChangeStrategy(tabId, sbPrevUrl) {
browser.tabs.get(tabId, function (tabinfo) {
const modalUrl = tabinfo.url;
console.log(modalUrl)
if (sbPrevUrl !== modalUrl && modalUrl !== 'about:blank') {
checkAndCloseTab(tabId, sbPrevUrl, modalUrl);
}
});
}
function checkAndCloseTab(tabId, sbPrevUrl, modalUrl) {
var shallClose = closeWhen.some(function (urlPart) {
return modalUrl.includes(urlPart);
});
if (!shallClose) {
shallClose = closeCarefullyWhen.some(function (regex) {
var testUrl = RegExp(regex, 'g');
return testUrl.test(modalUrl);
});
}
if (!shallClose) {
shallClose = closeCarefullyOnlyIf.some(function (url) {
return url == modalUrl;
});
}
if (shallClose) {
browser.tabs.remove(tabId);
return;
}
browser.storage.local.get('mastodon').then(function (result) {
var shouldCloseMastodon = false;
if (result.mastodon && result.mastodon.length > 6) {
try {
const mastodonInstanceUrl = new URL(result.mastodon);
const prevUrl = new URL(sbPrevUrl);
const currentUrl = new URL(modalUrl);
if (mastodonInstanceUrl.host === prevUrl.host &&
prevUrl.pathname.includes('/share') &&
mastodonInstanceUrl.host === currentUrl.host &&
/\/@[a-zA-Z0-9._-]+\/\d+/.test(currentUrl.pathname)) {
shouldCloseMastodon = true;
}
} catch (error) {
console.log('Error parsing Mastodon URLs:', error);
}
}
if (shouldCloseMastodon) {
browser.tabs.remove(tabId);
}
});
}
// call function which get the address bar color (toolbar_field)
setPageActionIcon();
async function setPageActionIcon() {
var themeInfo = await browser.theme.getCurrent();
let color = 'light';
var rgb_list = ['rgb(71, 71, 73)', 'rgb(50, 50, 52)']; // list of RGB where icon_theme should be light
if (themeInfo.colors && themeInfo.colors.toolbar_field) {
if (!rgb_list.includes(themeInfo.colors.toolbar_field.toString())) {
color = 'dark';
}
}
setPageAction(color);
}
function setPageAction(color) {
var ext_icon = 'icon-' + color + '.svg';
browser.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
var getting = browser.storage.local.get('pageaction');
getting.then(
function (result) {
var pageaction = result.pageaction;
if (!pageaction) {
if (
tab.url.indexOf('about:') !== 0 &&
tab.url.indexOf('moz-extension:') !== 0
) {
browser.pageAction.setIcon({
tabId: tab.id,
path: ext_icon
});
browser.pageAction.setTitle({
tabId: tab.id,
title: 'Share'
});
browser.pageAction.show(tab.id);
browser.pageAction.setPopup({
tabId,
popup: '/modal/modal.html'
});
}
}
},
function (error) {
console.log(`Error: ${error}`);
}
);
});
}