Skip to content

Commit

Permalink
Merge pull request #5 from pantyetta/niconico
Browse files Browse the repository at this point in the history
Niconico suport
  • Loading branch information
pantyetta authored Sep 23, 2024
2 parents 87310c2 + 5cd472a commit 2763e33
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# auto skip ads
youtube広告を自動でスキップします.
- 15-30程度の広告のスキップボタンを自動で押します.
- スキップ不能の広告をスキップします.
`v1.0.6`からニコニコをサポート!

**広告自体のブロックはしません**

## suport site
- youtube
- niconico

## install

### Store (推奨)
Expand Down
61 changes: 56 additions & 5 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const skipAds = (tabId) => {
const y_skipAds = (tabId) => {
chrome.scripting.executeScript({
target : {tabId: tabId },
func: () => {
Expand Down Expand Up @@ -35,6 +35,51 @@ const skipAds = (tabId) => {
});
}

const n_skipAds = (tabId) => {
chrome.scripting.executeScript({
target : {tabId: tabId },
func: () => {
const config = {
attributes: true,
childList: true,
characterData: true,
subtree: true,
};

const $root = document.getElementById('root');
let skip_enable = true;

const rootObserver = new MutationObserver(() => {
const video = document.querySelector("#nv_watch_VideoAdContainer > div > div:nth-child(1) > video") || null;

if(!video) return;

if(!video.duration) {
skip_enable = true;
return;
}

if(video.currentTime == video.duration){
const skipButton = document.querySelector("#nv_watch_VideoAdContainer > button") || null;
if(!skipButton) return;
skipButton.click();
return;
}

if(!skip_enable) return;


try {
video.currentTime = video.duration;
skip_enable = false;
} catch (error) {}

});
rootObserver.observe($root, config);
},
});
}


const isEmpty = (obj) => {
return !Object.keys(obj).length;
Expand All @@ -44,18 +89,24 @@ chrome.webNavigation.onCommitted.addListener((details) => {
if (details.transitionType === 'reload') {
chrome.storage.session.remove([details.tabId.toString()]);
}
}, {url: [{urlMatches : 'https://www.youtube.com/*'}]});
}, {url: [{urlMatches : 'https://www.youtube.com/*'}, {urlMatches : 'https://www.nicovideo.jp/watch/*'}]});

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
const pattern = /^https:\/\/(www\.)?youtube\.com\/.+/
if(tab.url.match(pattern) && changeInfo.status === 'complete'){
const y_pattern = /^https:\/\/(www\.)?youtube\.com\/.+/
const n_pattern = /^https:\/\/(www\.)?nicovideo\.jp\/watch\/.+/

if(tab.url.match(y_pattern) && changeInfo.status === 'complete'){

chrome.storage.session.get([tabId.toString()]).then((result) => {
if(isEmpty(result)){
skipAds(tabId);
y_skipAds(tabId);
}
});

chrome.storage.session.set({ [tabId]: true });
}

if(tab.url.match(n_pattern) && changeInfo.status === 'complete'){
n_skipAds(tabId);
}
});
6 changes: 4 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auto skip ads",
"version": "1.0.5",
"version": "1.0.6",
"description": "youtube広告をブロックせずにスキップします",
"manifest_version": 3,
"background": {
Expand All @@ -20,6 +20,8 @@
],
"host_permissions": [
"https://www.youtube.com/*",
"https://youtube.com/*"
"https://youtube.com/*",
"https://nicovideo.jp/watch/*",
"https://www.nicovideo.jp/watch/*"
]
}

0 comments on commit 2763e33

Please sign in to comment.