-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
141 lines (119 loc) · 4.64 KB
/
contentScript.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
// contentScript.js
// Function to hide specific elements based on the URL
function hideElementsBasedOnURL() {
if (location.href.includes('https://youtube.com') || location.href.includes('www.youtube.com')) {
if (
!location.href.includes('search_query') &&
!location.href.includes('watch')
) {
const youtubeContents = document.querySelector('#contents');
if (youtubeContents) {
youtubeContents.style.display = 'none';
}
const shortsContainer = document.querySelector('#shorts-container');
if (shortsContainer) {
shortsContainer.style.display = 'none';
}
const pageManager = document.querySelector('#page-manager');
if (pageManager) {
pageManager.remove();
}
}
if (location.href.includes('watch')) {
const items = document.querySelector('#items');
if (items) {
items.remove();
}
const below = document.querySelector('#below');
if (below) {
below.remove();
}
}
}
if (location.href.includes('instagram')) {
const instagramSection = document.querySelector('section');
if (instagramSection) {
instagramSection.style.display = 'none';
}
}
if (location.href.includes('facebook')) {
document.querySelectorAll('*').forEach((el) => el.remove());
}
}
// Initialize the extension
function initializeExtension() {
// Get the current state from storage
chrome.storage.local.get('isActive', (data) => {
if (data.isActive) {
// Add event listeners and observers
addEventListenersAndObservers();
} else {
// Remove event listeners and observers
removeEventListenersAndObservers();
// Optionally, reload the page to undo changes
// location.reload();
}
});
}
// Function to add event listeners and observers
function addEventListenersAndObservers() {
// Run the function when the DOM is fully loaded
document.addEventListener('DOMContentLoaded', hideElementsBasedOnURL);
// Observe URL changes in single-page applications
window.lastUrl = location.href;
const urlObserver = new MutationObserver(() => {
const currentUrl = location.href;
if (currentUrl !== window.lastUrl) {
window.lastUrl = currentUrl;
hideElementsBasedOnURL(); // URL has changed, run the function again
}
});
urlObserver.observe(document.body, { childList: true, subtree: true });
// For history pushState or replaceState (used in SPAs)
(function (history) {
const pushState = history.pushState;
const replaceState = history.replaceState;
history.pushState = function () {
const result = pushState.apply(history, arguments);
window.dispatchEvent(new Event('locationchange'));
return result;
};
history.replaceState = function () {
const result = replaceState.apply(history, arguments);
window.dispatchEvent(new Event('locationchange'));
return result;
};
window.addEventListener('locationchange', hideElementsBasedOnURL);
})(window.history);
// Handle popstate for back/forward navigation
window.addEventListener('popstate', hideElementsBasedOnURL);
// Observe changes in the body to catch dynamically added elements
const observer = new MutationObserver(hideElementsBasedOnURL);
observer.observe(document.body, { childList: true, subtree: true });
// Save observers so we can disconnect them later
window.extensionObservers = { urlObserver, observer };
}
// Function to remove event listeners and observers
function removeEventListenersAndObservers() {
document.removeEventListener('DOMContentLoaded', hideElementsBasedOnURL);
window.removeEventListener('popstate', hideElementsBasedOnURL);
window.removeEventListener('locationchange', hideElementsBasedOnURL);
// Disconnect observers if they exist
if (window.extensionObservers) {
if (window.extensionObservers.urlObserver)
window.extensionObservers.urlObserver.disconnect();
if (window.extensionObservers.observer)
window.extensionObservers.observer.disconnect();
window.extensionObservers = null;
}
// Optionally, reload the page to undo changes
// location.reload();
}
// Initialize when the content script loads
initializeExtension();
// Listen for messages from the background script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.isActive !== undefined) {
initializeExtension();
}
});