Skip to content

Commit

Permalink
Added new modes hideAds and hideMixes
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfrosty committed Nov 26, 2024
1 parent 0c44847 commit 94a2809
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 42 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name":"Shift Youtube Optimizer",
"description":"Chrome extension to hide/reduce the number of videos shown on YouTube.",
"version":"1.0.3",
"version":"1.1.2",
"manifest_version":3,
"content_scripts":[
{
Expand Down
55 changes: 51 additions & 4 deletions observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function attrQueryAll(cls=null, tag=null, attr, qterm) {
function resetReapply() {
// Reset everything
let toremove = document.getElementsByClassName('shift-yt-wm-deleteme');
for(let i=toremove.length-1;i>=0;i--){toremove[i].style.display="flex";toremove[i].style.opacity="1";toremove[i].classList.remove('shift-yt-wm-deleteme')};
for(let i=toremove.length-1;i>=0;i--){toremove[i].style.display="block";toremove[i].style.opacity="1";toremove[i].classList.remove('shift-yt-wm-deleteme')};
let toremoven = document.getElementsByClassName('shift-yt-wm-used');
for(let ii=toremoven.length-1;ii>=0;ii--){toremoven[ii].classList.remove('shift-yt-wm-used')};
document.body.setAttribute('async-inject-listener', 'false');
Expand Down Expand Up @@ -132,6 +132,48 @@ async function hideWorkMode(elist=null) {
}
}

function hideAds(){
var hideparam = null;
var tohide = null;

if(window.location.pathname.split('/')[1] ==''){
hideparam = ["ytd-rich-grid-renderer"];
tohide = document.querySelector("ytd-browse").querySelectorAll("ytd-ad-slot-renderer");

for(i=0;i<tohide.length;i++){
ancCont="";
ancCont = findAncs(tohide[i], hideparam);
if(ancCont) {
ancCont.classList.add('shift-yt-ads-deleteme');
ancCont.style.opacity = "0";
setTimeout(function(ancCont){ancCont.style.display="none"}, 1000, ancCont);
tohide[i].classList.add('shift-yt-ads-used');
}
}
}
}

function hideMixes(){
var hideparam = null;
var tohide = null;

if(window.location.pathname.split('/')[1] ==''){
hideparam = ["ytd-rich-grid-renderer"];
tohide = document.querySelector("ytd-browse").querySelectorAll("yt-collection-thumbnail-view-model");

for(i=0;i<tohide.length;i++){
ancCont="";
ancCont = findAncs(tohide[i], hideparam);
if(ancCont) {
ancCont.classList.add('shift-yt-mix-deleteme');
ancCont.style.opacity = "0";
setTimeout(function(ancCont){ancCont.style.display="none"}, 1000, ancCont);
tohide[i].classList.add('shift-yt-mix-used');
}
}
}
}

function hideWatched(hideparam=['ytd-rich-grid-renderer']) {
if(window.location.href.split("/")[3] == 'playlist?list=LL') {
console.log("shift-yt-optimizer has been disabled for this page.");
Expand All @@ -153,7 +195,7 @@ function hideWatched(hideparam=['ytd-rich-grid-renderer']) {
} else {
if(locname[1] == '') { //homepage
hideparam=['ytd-rich-grid-renderer'];
tohide = attrQueryAll(null, 'ytd-browse', 'role', 'main')[0].querySelectorAll('ytd-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)');
tohide = attrQueryAll(null, 'ytd-browse', 'role', 'main')[0].querySelectorAll('ytd-thumbnail-overlay-resume-playback-renderer:not(.shift-yt-used)'); //yt-thumbnail-overlay-progress-bar-view-model:not(.shift-yt-used)
} else if (locname[1] == 'c' || locname[1] == 'user' || locname[1] == 'channel' || locname[1].includes("@")) { //someone's channel
if (locname[locname.length - 1] == 'videos') {
hideparam=['ytd-rich-grid-renderer'];
Expand Down Expand Up @@ -242,14 +284,14 @@ chrome.runtime.onMessage.addListener(
let r = request.remparams.trim();
let toremove = document.getElementsByClassName(r);
for(let i=toremove.length-1;i>=0;i--){
toremove[i].style.display="flex";
toremove[i].style.display="block";
toremove[i].style.opacity="1";
toremove[i].classList.remove('shift-yt-wm-deleteme');
toremove[i].querySelector('.shift-yt-wm-used').classList.remove('shift-yt-wm-used');
}
} else { // no remove params, remove everything
let toremove = document.getElementsByClassName('shift-yt-wm-deleteme');
for(let i=toremove.length-1;i>=0;i--){toremove[i].style.display="flex";toremove[i].style.opacity="1";toremove[i].classList.remove('shift-yt-wm-deleteme')};
for(let i=toremove.length-1;i>=0;i--){toremove[i].style.display="block";toremove[i].style.opacity="1";toremove[i].classList.remove('shift-yt-wm-deleteme')};
let toremoven = document.getElementsByClassName('shift-yt-wm-used');
for(let ii=toremoven.length-1;ii>=0;ii--){toremoven[ii].classList.remove('shift-yt-wm-used')};
document.body.setAttribute('async-inject-listener', 'false');
Expand All @@ -271,6 +313,11 @@ function scrollHide(e) {
hideWorkMode();
}
});
chrome.storage.sync.get('adMode', function(obj) {
if(obj['adMode']){
hideAds();
}
})
}
this.oldScroll = this.scrollY;
}
Expand Down
162 changes: 126 additions & 36 deletions ui/content.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Functions to be executed within the active Chrome window
function extRun() {
chrome.storage.sync.get('mK', function(obj) {
if(obj['mK'] > 0) {
Expand All @@ -11,49 +12,95 @@ function extRun() {
}
} else {
let toremove = document.getElementsByClassName('shift-yt-deleteme');
for(let i=toremove.length-1;i>=0;i--){toremove[i].style.display="flex";toremove[i].style.opacity="1";toremove[i].classList.remove('shift-yt-deleteme')};
for(let i=toremove.length-1;i>=0;i--){toremove[i].style.display="block";toremove[i].style.opacity="1";toremove[i].classList.remove('shift-yt-deleteme')};
let toremoven = document.getElementsByClassName('shift-yt-used');
for(let ii=toremoven.length-1;ii>=0;ii--){toremoven[ii].classList.remove('shift-yt-used')};
document.body.setAttribute('async-inject-listener', 'false');
}
});
}

function extRun2() {
function extRun_work() {
resetReapply();
}
function extRun_ads(){
chrome.storage.sync.get('adMode', function(obj) {
if(obj["adMode"] > 0) {
if(window.location.host == 'www.youtube.com'){
console.log("Youtube");
hideAds();
} else if (window.location.host == 'm.youtube.com') {
console.log("Mobile Youtube");
hideAds();
} else {
console.log('Not Youtube');
}
} else {
console.log("OFF");
//Add refresh here
let toremove = document.getElementsByClassName('shift-yt-ads-deleteme');
for(let i=toremove.length-1;i>=0;i--){toremove[i].style.display="block";toremove[i].style.opacity="1";toremove[i].classList.remove('shift-yt-ads-deleteme')};
let toremoven = document.getElementsByClassName('shift-yt-ads-used');
for(let ii=toremoven.length-1;ii>=0;ii--){toremoven[ii].classList.remove('shift-yt-ads-used')};
document.body.setAttribute('async-inject-listener', 'false');
}
});
}
function extRun_mixes() {
chrome.storage.sync.get('mixMode', function(obj) {
if(obj["mixMode"] > 0) {
if(window.location.host == 'www.youtube.com'){
console.log("Youtube");
hideMixes();
} else if (window.location.host == 'm.youtube.com') {
console.log("Mobile Youtube");
hideMixes();
} else {
console.log('Not Youtube');
}
} else {
console.log("OFF");
//Add refresh here
let toremove = document.getElementsByClassName('shift-yt-mix-deleteme');
for(let i=toremove.length-1;i>=0;i--){toremove[i].style.display="block";toremove[i].style.opacity="1";toremove[i].classList.remove('shift-yt-mix-deleteme')};
let toremoven = document.getElementsByClassName('shift-yt-mix-used');
for(let ii=toremoven.length-1;ii>=0;ii--){toremoven[ii].classList.remove('shift-yt-mix-used')};
document.body.setAttribute('async-inject-listener', 'false');
}
});
}

// Get local stored variables when tab is reloaded / refreshed, then set the UI accordingly
chrome.storage.sync.get('mK', function(obj) {
if (obj['mK'] > 0) {
document.getElementById('hide_watched').checked = 'true';
}
});

document.getElementById('hide_watched').addEventListener('change', async() => {
let [tab] = await chrome.tabs.query({active:true,currentWindow:true});

chrome.storage.sync.get('mK', function(obj) {
if (typeof obj['mK'] == 'undefined' ) {
chrome.storage.sync.set({'mK':1});
}else if (obj['mK'] > 0) {
chrome.storage.sync.set({'mK':0});
}else if (obj['mK'] == 0){
chrome.storage.sync.set({'mK':1});
chrome.storage.sync.get('adMode', function(obj) {
if(obj['adMode'] > 0) {
document.getElementById('hide_ads').checked = "true";
}
});
chrome.storage.sync.get('mixMode', function(obj) {
if(obj['mixMode'] > 0) {
document.getElementById('hide_mixes').checked = "true";
}
});
chrome.storage.sync.get('excl', function(obj) {
if(obj['excl'] === undefined) {
chrome.storage.sync.set({'excl':true});
} else {
if(!obj['excl']) {
document.getElementById("ext_include").checked = 'true';
}
});

chrome.scripting.executeScript({
target:{tabId:tab.id},
function: extRun,
});
}
});

// Async functions with event handles that are responsible for tags
inclhandler = async function(e) {
let [tab] = await chrome.tabs.query({active:true,currentWindow:true});
chrome.tabs.sendMessage(tab.id, {ftr:'work-mode', checkval:true, insparams:[this.value]});
e.target.value="";
}

inputhandler = async function(e) {
if(e.keyCode === 32) {
if(e.target.value.length <= 1) {
Expand Down Expand Up @@ -96,6 +143,7 @@ inputhandler = async function(e) {
}
}

// Complex functions to set UI according to tags and including/excluding option
chrome.storage.sync.get(['incl_tags'], function(obj) {
if(typeof obj['incl_tags'] == 'undefined') {
console.log('empty');
Expand All @@ -110,7 +158,6 @@ chrome.storage.sync.get(['incl_tags'], function(obj) {
document.getElementById('ext_include_params').focus();
}
});

chrome.storage.sync.get('wM', function(obj) {
if (obj['wM'] > 0) {
document.getElementById('work_mode_toggle').checked = 'true';
Expand All @@ -133,6 +180,61 @@ chrome.storage.sync.get('wM', function(obj) {
}
});

// Event Listeners that are responsible for inserting functions to be executed into the active Chrome tabs
document.getElementById('hide_watched').addEventListener('change', async() => {
let [tab] = await chrome.tabs.query({active:true,currentWindow:true});

chrome.storage.sync.get('mK', function(obj) {
if (typeof obj['mK'] == 'undefined' ) {
chrome.storage.sync.set({'mK':1});
}else if (obj['mK'] > 0) {
chrome.storage.sync.set({'mK':0});
}else if (obj['mK'] == 0){
chrome.storage.sync.set({'mK':1});
}
});

chrome.scripting.executeScript({
target:{tabId:tab.id},
function: extRun,
});
});
document.getElementById('hide_ads').addEventListener('change', async() => {
let [tab] = await chrome.tabs.query({active:true,currentWindow:true});

chrome.storage.sync.get('adMode', function(obj) {
if (typeof obj['adMode'] == 'undefined' ) {
chrome.storage.sync.set({'adMode':1});
}else if (obj['adMode'] > 0) {
chrome.storage.sync.set({'adMode':0});
}else if (obj['adMode'] == 0){
chrome.storage.sync.set({'adMode':1});
}
});

chrome.scripting.executeScript({
target:{tabId:tab.id},
function: extRun_ads,
});
})
document.getElementById('hide_mixes').addEventListener('change', async() => {
let [tab] = await chrome.tabs.query({active:true,currentWindow:true});

chrome.storage.sync.get('mixMode', function(obj) {
if (typeof obj['mixMode'] == 'undefined' ) {
chrome.storage.sync.set({'mixMode':1});
}else if (obj['mixMode'] > 0) {
chrome.storage.sync.set({'mixMode':0});
}else if (obj['mixMode'] == 0){
chrome.storage.sync.set({'mixMode':1});
}
});

chrome.scripting.executeScript({
target:{tabId:tab.id},
function: extRun_mixes,
});
})
document.getElementById('work_mode_toggle').addEventListener('change', async() => {
let [tab] = await chrome.tabs.query({active:true,currentWindow:true});
document.getElementsByClassName('work_mode_dets')[0].classList.toggle('nonactive');
Expand All @@ -155,31 +257,19 @@ document.getElementById('work_mode_toggle').addEventListener('change', async() =
});

});

chrome.storage.sync.get('excl', function(obj) {
if(obj['excl'] === undefined) {
chrome.storage.sync.set({'excl':true});
} else {
if(!obj['excl']) {
document.getElementById("ext_include").checked = 'true';
}
}
});

document.querySelector("label[for='ext_include']").addEventListener('click', async () => {
let [tab] = await chrome.tabs.query({active:true,currentWindow:true});
chrome.storage.sync.set({'excl':false});
chrome.scripting.executeScript({
target:{tabId: tab.id},
func:extRun2,
func:extRun_work,
});
});

document.querySelector("label[for='ext_exclude']").addEventListener('click', async () => {
let [tab] = await chrome.tabs.query({active:true,currentWindow:true});
chrome.storage.sync.set({'excl':true});
chrome.scripting.executeScript({
target:{tabId: tab.id},
func:extRun2,
func:extRun_work,
});
});
6 changes: 6 additions & 0 deletions ui/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
</head>
<body>
<div class="ext_title"><img class="ext_logo" src="icons/icon128px.png"><span>Shift</span></div>
<div class="ext_checkbox_cont">
<input type="checkbox" id="hide_ads" name="hide_ads">
<label for="hide_ads">Hide Ads</label>
<input type="checkbox" id="hide_mixes" name="hide_mixes">
<label for="hide_mixes">Hide Mixes</label>
</div>
<div class="ext_labels">
<div class="ext_label">
<label class="ext_switch">
Expand Down
8 changes: 7 additions & 1 deletion ui/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ body {
}

.ext_labels {
padding: 5vw 0 0 0;
padding: 2vw 0 0 0;
}

.ext_label {
Expand Down Expand Up @@ -195,4 +195,10 @@ input:checked + .ext_slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}

.ext_checkbox_cont {
text-align: center;
margin: 2vw;
padding: 2vw;
}

0 comments on commit 94a2809

Please sign in to comment.