-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
140 lines (119 loc) · 5.98 KB
/
main.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
console.log('script loaded');
// Listen from any message coming from the background worker script
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
mainHandler(request.message);
});
/**
* Main handler for Simply Better Instagram. This function is responsible of accessing
* and modifying the DOM directly
* @param {String} message The message coming from the background worker script
*/
const mainHandler = function (message) {
console.log(message);
// TODO: find a way to wait until the page is fully loaded before doing anything
// Fetch the settings before doing anything
chrome.storage.sync.get(['settings'], (storageData) => {
// If the settings were not fetched correctly, cut the treatment instantly
if (storageData == null || storageData.settings == null) {
return;
}
// Constant for shorter variable names later
const extensionSettings = storageData.settings;
// Construct the style string
let globalStyle = '';
// ----------------
// GENERAL SETTINGS
// ----------------
if (extensionSettings.general.apply === true) {
// Fetch the main block
const mainBlockFetchString = 'main[role="main"]';
const mainBlock = document.body.querySelector(mainBlockFetchString);
// SETTING: Make the header bar's content take the full page's width
if (extensionSettings.general.headerbarContentTakesFullPageWidth === true) {
// Search the header bar (navbar)
const navbarBlock = mainBlock.nextSibling;
// Search the container to modify
const classToTarget = `div.${Array.from(navbarBlock.childNodes.item(1).firstChild.firstChild.classList).join('.')}`;
globalStyle += `${classToTarget}{max-width:none;}`;
}
}
// ----------
// INDEX PAGE
// ----------
if (message === 'SBI-index' && extensionSettings.index.apply === true) {
// Fetch the main block
const mainBlockFetchString = 'main[role="main"]';
const mainBlock = document.body.querySelector(mainBlockFetchString);
// SETTING: Hide right panel
if (extensionSettings.index.hideRightPanel === true) {
// Remove the right panel's DIV
mainBlock.firstChild.lastChild.remove();
// Override the style of the left DIV
const leftPanelClasses = `section.${Array.from(mainBlock.firstChild.classList).join('.')}`;
globalStyle += `${mainBlockFetchString}>${leftPanelClasses}{max-width:max-content;}`;
}
// SETTING: Reduce the gaps above and below the stories bar
if (extensionSettings.index.reduceStoriesBarGaps === true) {
// Search the section container to update the top padding
const sectionClasses = `section.${Array.from(mainBlock.firstChild.classList).join('.')}`;
globalStyle += `${mainBlockFetchString} ${sectionClasses}{padding-top: 4px;}`;
// Search the bar to update the bottom margin
const barClasses = `div.${Array.from(mainBlock.firstChild.firstChild.firstChild.classList).join('.')}`;
globalStyle += `${mainBlockFetchString} ${barClasses}{margin-bottom: 4px;}`;
}
// SETTING: Reduce the gap between publications
if (extensionSettings.index.reduceGapBetweenPublications === true) {
// Search one publication and read its first class
const classToTarget = `article.${Array.from(mainBlock.firstChild.firstChild.childNodes.item(1).firstChild.firstChild.classList)[0]}`;
globalStyle += `${mainBlockFetchString} ${classToTarget}{margin-bottom: 4px;}`;
}
// SETTING: Bigger publications
if (extensionSettings.index.biggerPublications === true) {
// Override the style of the left DIV
const leftPanelClasses = `div.${Array.from(mainBlock.firstChild.firstChild.classList).join('.')}`;
console.log(mainBlock.firstChild.firstChild);
console.log(leftPanelClasses);
globalStyle += `${mainBlockFetchString} ${leftPanelClasses}{max-width:none;}`;
}
}
// -------
// PROFILE
// -------
else if (message === 'SBI-publications-feed' && extensionSettings.profile.apply === true) {
// Fetch the main block
const mainBlockFetchString = 'main[role="main"]';
const mainBlock = document.body.querySelector(mainBlockFetchString);
// SETTING: zeroGapPublicationsGrid
if (extensionSettings.profile.zeroGapPublicationsGrid) {
// Get the publications wrapper
const publicationsWrapperFetchString = 'div div:last-child article>div>div';
const publicationsWrapper = mainBlock.querySelector(publicationsWrapperFetchString);
if (publicationsWrapper != null) {
// Check if the publications grid exists
if (publicationsWrapper.firstChild != null && publicationsWrapper.firstChild.firstChild != null) {
// Publications row
const publicationsRowClasses = `div.${Array.from(publicationsWrapper.firstChild.classList).join('.')}`;
// Publications column
const publicationsColumnClasses = `div.${Array.from(publicationsWrapper.firstChild.firstChild.classList).join('.')}`;
globalStyle += `${mainBlockFetchString} ${publicationsWrapperFetchString} ${publicationsRowClasses}{margin:0;}`;
globalStyle += `${mainBlockFetchString} ${publicationsWrapperFetchString} ${publicationsColumnClasses}{margin:0;}`;
}
}
}
} else if (message === 'SBI-reels-feed') {
// TODO:
} else if (message === 'SBI-igtv-feed') {
// TODO:
} else if (message === 'SBI-tagged-feed') {
// TODO:
} else if (message === 'SBI-saved-feed') {
// TODO:
}
// Create the HTML style element
var globalStyleOverrideElement = document.createElement('style');
// globalStyleOverrideElement.id = 'sbi-profile-style-override';
globalStyleOverrideElement.appendChild(document.createTextNode(globalStyle));
// Append the HTML style element to the web page
document.getElementsByTagName('head')[0].appendChild(globalStyleOverrideElement);
});
};