-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
173 lines (163 loc) · 5.46 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
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
169
170
171
172
173
/**
* Filter the page URL and send a message to the tab running the script based on the URL patterns
* @param {String} pageUrl The URL of the page loaded in the tab running the script
* @param {Number} tabId The identifier of the tab running the script
* @returns
*/
const urlFiltering = function (pageUrl, tabId) {
// If the URL does not match with Instagram's one, stop here
if (!pageUrl.includes('https://www.instagram.com')) {
return;
}
// Routes that are currently skipped
const explore = new RegExp('https://www.instagram.com/explore/.*');
const inbox = new RegExp('https://www.instagram.com/direct/.*');
const accounts = new RegExp('https://www.instagram.com/accounts/.*');
const pictures = new RegExp('https://www.instagram.com/p/.+');
const postIGVT = new RegExp('https://www.instagram.com/tv/[^/]+/');
const stories = new RegExp('https://www.instagram.com/stories/.+');
const reel = new RegExp('https://www.instagram.com/reel/[^/]+/');
// Profile and its submenus
const profilePublications = new RegExp('https://www.instagram.com/[^/]+/');
const profileReels = new RegExp('https://www.instagram.com/[^/]+/reels/.*');
const profileIGTV = new RegExp('https://www.instagram.com/[^/]+/channel/');
const profileSaved = new RegExp('https://www.instagram.com/[^/]+/saved/');
const profileTagged = new RegExp('https://www.instagram.com/[^/]+/tagged/');
// Any URL that currently needs to be ignored
if (
explore.test(pageUrl) ||
inbox.test(pageUrl) ||
accounts.test(pageUrl) ||
pictures.test(pageUrl) ||
postIGVT.test(pageUrl) ||
stories.test(pageUrl) ||
reel.test(pageUrl)
) {
console.info('URL filtered');
} else if (profileReels.test(pageUrl)) {
chrome.tabs.sendMessage(tabId, {
message: 'SBI-reels-feed',
url: pageUrl,
});
}
// Profile: IGTV feed
else if (profileIGTV.test(pageUrl)) {
chrome.tabs.sendMessage(tabId, {
message: 'SBI-igtv-feed',
url: pageUrl,
});
}
// Profile: saved posts
else if (profileSaved.test(pageUrl)) {
chrome.tabs.sendMessage(tabId, {
message: 'SBI-saved-feed',
url: pageUrl,
});
}
// Profile: Tagged feed
else if (profileTagged.test(pageUrl)) {
chrome.tabs.sendMessage(tabId, {
message: 'SBI-tagged-feed',
url: pageUrl,
});
}
// Profile: Publications feed
else if (profilePublications.test(pageUrl)) {
chrome.tabs.sendMessage(tabId, {
message: 'SBI-publications-feed',
url: pageUrl,
});
}
// Index page
else if (pageUrl === 'https://www.instagram.com' || pageUrl === 'https://www.instagram.com/' || pageUrl.startsWith('https://www.instagram.com/?')) {
chrome.tabs.sendMessage(tabId, {
message: 'SBI-index',
url: pageUrl,
});
}
// Any other URL (this part should no be reachable)
else {
console.error(`Simply Better Instagram extension: unhandled URL found: '${pageUrl}'`);
}
};
// Function executed when the extension is installed or updated, or Google Chrome is updated
chrome.runtime.onInstalled.addListener(() => {
// Set the extension settings to default
chrome.storage.sync.set({
settings: {
general: {
apply: true, // false
showHTMLPlayerControls: false, // false
headerbarContentTakesFullPageWidth: true, // false
},
index: {
apply: true, // false
hideRightPanel: true, // false
reduceStoriesBarGaps: true, // false
reduceGapBetweenPublications: true, // false
biggerPublications: false, // false
},
profile: {
apply: true, // false
zeroGapPublicationsGrid: true, // false
zeroGapReelsGrid: true, // false
zeroGapIGTVGrid: true, // false
imagesByRow: 3, // 3
profileLinkWidthFix: false, // false
backgroundColor: '#fafafa', // '#fafafa'
fontColor: '',
linksColor: '',
buttonsBackgroundColor: '',
buttonsFontColor: '',
inactiveTabsFontColor: '',
activeTabFontColor: '',
roundSpinnedReelsButton: true,
roundProfilePicture: true,
betterVerifiedUserBadge: false,
hideSuggestionsButton: false,
},
},
other: {
todo: true,
},
});
});
const sendMessage = function (details) {
if (details.frameId === 0) {
// Fires only when details.url === currentTab.url
chrome.tabs.get(details.tabId, function (tab) {
if (tab.url === details.url) {
console.log('onHistoryStateUpdated');
urlFiltering(details.url, details.tabId);
}
});
}
};
// Event listener when a page is loaded for the first time
chrome.webNavigation.onCompleted.addListener((details) => {
console.log('onCompleted');
});
// Event listener when a page's URL is changed
// chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// if (changeInfo.url) {
// urlFiltering(changeInfo.url, tabId);
// }
// });
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.url) {
console.log('onUpdated');
}
});
chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
console.log('onHistoryStateUpdated');
});
chrome.webNavigation.onDOMContentLoaded.addListener((details) => {
console.log('onDOMContentLoaded');
});
chrome.tabs.onActivated.addListener(function (activeInfo) {
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (activeInfo.tabId === tabId && changeInfo.url) {
console.log(`URL has changed to ${changeInfo.url}`);
}
});
});