Skip to content

Commit 1e96343

Browse files
committed
finish with logic with bare min popup style
1 parent 0b67b55 commit 1e96343

File tree

6 files changed

+147
-5
lines changed

6 files changed

+147
-5
lines changed

background.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
chrome.runtime.onInstalled.addListener(() => {
2+
chrome.storage.sync.set({ isEnabled: true });
3+
});
4+
5+
chrome.action.onClicked.addListener((tab) => {
6+
chrome.storage.sync.get('isEnabled', (data) => {
7+
chrome.storage.sync.set({ isEnabled: !data.isEnabled }, () => {
8+
chrome.scripting.executeScript({
9+
target: { tabId: tab.id },
10+
files: ['content.js']
11+
});
12+
});
13+
});
14+
});
15+

content.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
chrome.storage.sync.get('isEnabled', (data) => {
2+
if (data.isEnabled) {
3+
const MAX_PLAYBACK_RATE = 16;
4+
let originalMuted = false;
5+
let originalPlaybackRate = 1;
6+
7+
function handleAdSpeed(video) {
8+
const observerCallback = (mutations) => {
9+
const skipAd = () => {
10+
//logic for finding and clicking skip button
11+
const skipContainers = [
12+
".ytp-ad-skip-button-container .ytp-ad-skip-button-modern",
13+
".ytp-skip-ad .ytp-skip-ad-button"
14+
];
15+
skipContainers.forEach(selector => {
16+
const skipButton = document.querySelector(selector);
17+
if (skipButton) {
18+
skipButton.click();
19+
}
20+
});
21+
};
22+
23+
// The MutationObserver is a JavaScript API that provides a way to watch for changes being made to the DOM tree.
24+
// It is designed to react to DOM mutations (such as when nodes are added, removed, or changed) and allows you to execute
25+
// callback functions when such changes occur. This is particularly useful for monitoring dynamic content changes in a web page.
26+
27+
28+
29+
mutations.forEach((mutation) => {
30+
if (mutation.attributeName === "style") {
31+
skipAd();
32+
}
33+
});
34+
35+
if (video.playbackRate !== MAX_PLAYBACK_RATE) {
36+
originalMuted = video.muted;
37+
originalPlaybackRate = video.playbackRate;
38+
}
39+
40+
//it detect is there ad or not on load
41+
const isAd = moviePlayer.classList.contains("ad-showing") || moviePlayer.classList.contains("ad-interrupting");
42+
//it make noise when a ad is playing at 16x speed so...... muting is a good optin
43+
video.muted = isAd ? true : originalMuted;
44+
video.playbackRate = isAd ? MAX_PLAYBACK_RATE : originalPlaybackRate;
45+
};
46+
47+
48+
//for the capability to skip ad in between videos, but it consume so much resource but not that much hehe
49+
const moviePlayer = document.querySelector("#movie_player");
50+
new MutationObserver(observerCallback).observe(moviePlayer, {
51+
attributeFilter: ["class", "style"],
52+
attributes: true,
53+
subtree: true,
54+
});
55+
}
56+
57+
function init() {
58+
const video = document.querySelector("#movie_player video");
59+
if (video) {
60+
handleAdSpeed(video);
61+
} else {
62+
setTimeout(init, 300);
63+
}
64+
}
65+
66+
init();
67+
}
68+
});

manifest.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"manifest_version": 1,
2+
"manifest_version": 3,
33
"name": "Not An Ad Blocker",
44
"version": "1.0",
55
"description": " Trying to speed up ads and skips them automatically.",
@@ -9,18 +9,18 @@
99
"scripting"
1010
],
1111
"background": {
12-
"service_worker": "background.js"
12+
"service_worker": "./background.js"
1313
},
1414
"content_scripts": [
1515
{
1616
"matches": ["*://www.youtube.com/*"],
17-
"js": ["content.js"]
17+
"js": ["./content.js"]
1818
}
1919
],
2020
"action": {
21-
"default_popup": "popup.html",
21+
"default_popup": "./popup.html",
2222
"default_icon": {
23-
"128": "icon.png"
23+
"128": "./icon.png"
2424
}
2525
}
2626
}

popup.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.container {
2+
text-align: center;
3+
width: 200px;
4+
padding: 20px;
5+
}
6+
7+
button {
8+
padding: 10px;
9+
background-color: #007bff;
10+
color: white;
11+
border: none;
12+
border-radius: 5px;
13+
cursor: pointer;
14+
}
15+
16+
button:disabled {
17+
background-color: #cccccc;
18+
}
19+

popup.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="popup.css">
5+
</head>
6+
<body>
7+
<div class="container">
8+
<h1>YouTube Ad Skipper</h1>
9+
<button id="toggleButton">Disable</button>
10+
</div>
11+
<script src="popup.js"></script>
12+
</body>
13+
</html>

popup.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
document.addEventListener('DOMContentLoaded', () => {
2+
const toggleButton = document.getElementById('toggleButton');
3+
4+
chrome.storage.sync.get('isEnabled', (data) => {
5+
updateButton(data.isEnabled);
6+
});
7+
8+
toggleButton.addEventListener('click', () => {
9+
chrome.storage.sync.get('isEnabled', (data) => {
10+
const newStatus = !data.isEnabled;
11+
chrome.storage.sync.set({ isEnabled: newStatus }, () => {
12+
updateButton(newStatus);
13+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
14+
chrome.scripting.executeScript({
15+
target: { tabId: tabs[0].id },
16+
files: ['content.js']
17+
});
18+
});
19+
});
20+
});
21+
});
22+
23+
function updateButton(isEnabled) {
24+
toggleButton.textContent = isEnabled ? 'Disable' : 'Enable';
25+
}
26+
});
27+

0 commit comments

Comments
 (0)