mirror of
https://github.com/lawrencehook/remove-youtube-suggestions.git
synced 2026-07-25 06:54:31 +00:00
Consolidate declarations of settings. Change refresh logic to use setTimeout.
This commit is contained in:
@@ -226,9 +226,6 @@ browser.runtime.onMessage.addListener((data, sender) => {
|
||||
if (getSettings) {
|
||||
const { frameId, tab } = sender;
|
||||
browser.storage.local.get(localSettings => {
|
||||
// const settings = Object.entries(localSettings).map(([id, value]) => {
|
||||
// return { id, value };
|
||||
// });
|
||||
const settings = { ...DEFAULT_SETTINGS, ...localSettings };
|
||||
browser.tabs.sendMessage(tab.id, { settings }, { frameId });
|
||||
});
|
||||
@@ -242,6 +239,7 @@ browser.runtime.onMessage.addListener((data, sender) => {
|
||||
if (!tab) browser.runtime.sendMessage({ FIELDSETS, settings });
|
||||
});
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.log(`ERROR: ${error}`);
|
||||
}
|
||||
|
||||
+38
-43
@@ -14,10 +14,16 @@ const REDIRECT_URLS = {
|
||||
};
|
||||
|
||||
const resultsPageRegex = new RegExp('.*://.*youtube\.com/results.*', 'i');
|
||||
const homepageRegex = new RegExp('.*://(www|m)\.youtube\.com/$', 'i');
|
||||
const homepageRegex = new RegExp('.*://(www|m)\.youtube\.com/$', 'i');
|
||||
const shortsRegex = new RegExp('.*://.*youtube\.com/shorts.*', 'i');
|
||||
|
||||
// Dynamic settings variables
|
||||
const cache = {};
|
||||
let url;
|
||||
let counter = 0, theaterClicked = false, hyper = false;
|
||||
let originalPlayback, originalMuted;
|
||||
let onResultsPage = resultsPageRegex.test(location.href);
|
||||
|
||||
|
||||
// Send a "get settings" message to the background script.
|
||||
browser.runtime.sendMessage({ getSettings: true });
|
||||
@@ -34,37 +40,12 @@ browser.runtime.onMessage.addListener((data, sender) => {
|
||||
cache[id] = value;
|
||||
});
|
||||
|
||||
window.requestAnimationFrame(runDynamicSettings);
|
||||
runDynamicSettings();
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
function handleRedirects() {
|
||||
if (cache['global_enable'] !== true) return;
|
||||
|
||||
const currentUrl = location.href;
|
||||
|
||||
// Mark whether or not we're on the "results" page
|
||||
const onResultsPage = resultsPageRegex.test(currentUrl);
|
||||
HTML.setAttribute('on_results_page', onResultsPage);
|
||||
|
||||
// Redirect homepage
|
||||
const onHomepage = homepageRegex.test(currentUrl);
|
||||
if (onHomepage && !cache['redirect_off']) {
|
||||
if (cache['redirect_to_subs']) location.replace(REDIRECT_URLS['redirect_to_subs']);
|
||||
if (cache['redirect_to_wl']) location.replace(REDIRECT_URLS['redirect_to_wl']);
|
||||
}
|
||||
|
||||
// Redirect shorts
|
||||
const onShorts = shortsRegex.test(currentUrl);
|
||||
if (onShorts && cache['normalize_shorts']) {
|
||||
const newUrl = currentUrl.replace('shorts', 'watch');
|
||||
location.replace(newUrl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Dynamic settings (i.e. js instead of css)
|
||||
document.addEventListener("DOMContentLoaded", event => {
|
||||
url = undefined;
|
||||
@@ -74,27 +55,17 @@ document.addEventListener("DOMContentLoaded", event => {
|
||||
originalPlayback = undefined;
|
||||
originalMuted = undefined;
|
||||
onResultsPage = resultsPageRegex.test(location.href);
|
||||
|
||||
// handleRedirects();
|
||||
// const observer = new MutationObserver(mutations => runDynamicSettings());
|
||||
// observer.observe(document.body, { subtree: true, childList: true });
|
||||
});
|
||||
|
||||
|
||||
let url;
|
||||
let counter = 0, theaterClicked = false, hyper = false;
|
||||
let originalPlayback, originalMuted;
|
||||
let onResultsPage = resultsPageRegex.test(location.href);
|
||||
|
||||
function runDynamicSettings() {
|
||||
|
||||
if ('global_enable' in cache && cache['global_enable'] !== true) {
|
||||
setTimeout(() => window.requestAnimationFrame(runDynamicSettings), 50);
|
||||
setTimeout(() => runDynamicSettings(), 50);
|
||||
return;
|
||||
}
|
||||
|
||||
// Give the browser time to breathe
|
||||
// if (counter++ % 2 === 0) return;
|
||||
|
||||
// Check if the URL has changed (YouTube is a Single-Page Application)
|
||||
if (url !== location.href) {
|
||||
url = location.href;
|
||||
theaterClicked = false;
|
||||
@@ -102,7 +73,7 @@ function runDynamicSettings() {
|
||||
originalPlayback = undefined;
|
||||
originalMuted = undefined;
|
||||
onResultsPage = resultsPageRegex.test(location.href);
|
||||
handleRedirects();
|
||||
handleUrlChange();
|
||||
}
|
||||
|
||||
// Hide shorts on the results page
|
||||
@@ -172,6 +143,30 @@ function runDynamicSettings() {
|
||||
// document.getElementsByTagName("video")[0].playbackRate = Number(cache['change_playback_speed']);
|
||||
// }
|
||||
|
||||
|
||||
setTimeout(() => window.requestAnimationFrame(runDynamicSettings), 50);
|
||||
setTimeout(() => runDynamicSettings(), 50);
|
||||
}
|
||||
|
||||
|
||||
function handleUrlChange() {
|
||||
if (cache['global_enable'] !== true) return;
|
||||
|
||||
const currentUrl = location.href;
|
||||
|
||||
// Mark whether or not we're on the "results" page
|
||||
const onResultsPage = resultsPageRegex.test(currentUrl);
|
||||
HTML.setAttribute('on_results_page', onResultsPage);
|
||||
|
||||
// Redirect the homepage
|
||||
const onHomepage = homepageRegex.test(currentUrl);
|
||||
if (onHomepage && !cache['redirect_off']) {
|
||||
if (cache['redirect_to_subs']) location.replace(REDIRECT_URLS['redirect_to_subs']);
|
||||
if (cache['redirect_to_wl']) location.replace(REDIRECT_URLS['redirect_to_wl']);
|
||||
}
|
||||
|
||||
// Redirect the shorts player
|
||||
const onShorts = shortsRegex.test(currentUrl);
|
||||
if (onShorts && cache['normalize_shorts']) {
|
||||
const newUrl = currentUrl.replace('shorts', 'watch');
|
||||
location.replace(newUrl);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-111
@@ -5,72 +5,18 @@ if (typeof browser === 'undefined') {
|
||||
|
||||
// Some global constants.
|
||||
const HTML = document.documentElement;
|
||||
const SETTINGS_LIST = {
|
||||
"dark_mode": { defaultValue: false },
|
||||
"global_enable": { defaultValue: true },
|
||||
|
||||
"remove_homepage": { defaultValue: true },
|
||||
"remove_sidebar": { defaultValue: true },
|
||||
"remove_end_of_video": { defaultValue: true },
|
||||
|
||||
"remove_all_but_one": { defaultValue: false },
|
||||
"remove_infinite_scroll": { defaultValue: false },
|
||||
"remove_extra_rows": { defaultValue: false },
|
||||
|
||||
"remove_logo_link": { defaultValue: false },
|
||||
"remove_home_link": { defaultValue: false },
|
||||
"remove_explore_link": { defaultValue: false },
|
||||
"remove_shorts_link": { defaultValue: false },
|
||||
|
||||
"normalize_shorts": { defaultValue: false },
|
||||
"auto_skip_ads": { defaultValue: false },
|
||||
"remove_entire_sidebar": { defaultValue: false },
|
||||
"disable_autoplay": { defaultValue: false },
|
||||
"remove_info_cards": { defaultValue: false },
|
||||
"remove_overlay_suggestions": { defaultValue: false },
|
||||
"remove_play_next_button": { defaultValue: false },
|
||||
"remove_menu_buttons": { defaultValue: false },
|
||||
"remove_comments": { defaultValue: false },
|
||||
"remove_chat": { defaultValue: false },
|
||||
"remove_embedded_more_videos": { defaultValue: false },
|
||||
|
||||
"remove_search_suggestions": { defaultValue: false },
|
||||
"remove_extra_results": { defaultValue: false },
|
||||
"remove_shorts_results": { defaultValue: false },
|
||||
"remove_thumbnail_mouseover_effect": { defaultValue: false },
|
||||
|
||||
"redirect_off": { defaultValue: true },
|
||||
"redirect_to_subs": { defaultValue: false },
|
||||
"redirect_to_wl": { defaultValue: false },
|
||||
};
|
||||
const VALID_SETTINGS = Object.keys(SETTINGS_LIST);
|
||||
|
||||
// Redirect setting constants.
|
||||
const REDIRECT_URLS = {
|
||||
"redirect_off": false,
|
||||
"redirect_to_subs": 'https://www.youtube.com/feed/subscriptions',
|
||||
"redirect_to_wl": 'https://www.youtube.com/playlist/?list=WL',
|
||||
};
|
||||
const REDIRECT_KEYS = VALID_SETTINGS.filter(id => id.includes('redirect'));
|
||||
const REDIRECT_OPTIONS_TEMPLATE = REDIRECT_KEYS.reduce((options, id) => {
|
||||
options[id] = false;
|
||||
return options;
|
||||
}, {});
|
||||
|
||||
|
||||
const OPTIONS_LIST = document.getElementById('primary_options');
|
||||
const TEMPLATE_FIELDSET = document.getElementById('template_fieldset');
|
||||
const TEMPLATE_OPTION = document.getElementById('template_option');
|
||||
|
||||
document.addEventListener("DOMContentLoaded", init);
|
||||
|
||||
|
||||
function init() {
|
||||
browser.runtime.sendMessage({ getFieldsets: true });
|
||||
document.addEventListener("keydown", handleEnter, false);
|
||||
}
|
||||
|
||||
// Load the options menu with our settings.
|
||||
document.addEventListener("DOMContentLoaded", init);
|
||||
|
||||
|
||||
function handleEnter(e) {
|
||||
const keycode = e.keyCode || e.which;
|
||||
@@ -119,61 +65,6 @@ function updateSetting(id, value) {
|
||||
}
|
||||
|
||||
|
||||
// Change settings with the options menu.
|
||||
Object.entries(SETTINGS_LIST).forEach(([id, value]) => {
|
||||
const settingElements = Array.from(document.getElementsByClassName(id));
|
||||
settingElements.forEach(button => button.addEventListener('click', async e => {
|
||||
|
||||
// Toggle on click: new value is opposite of old value.
|
||||
const value = !(String(HTML.getAttribute(id)).toLowerCase() === "true");
|
||||
|
||||
// Communicate changes (to local settings, content-script.js, etc.)
|
||||
let saveObj;
|
||||
|
||||
// Handle standard (non-redirect) settings.
|
||||
if (!id.includes('redirect')) {
|
||||
saveObj = { [id]: value };
|
||||
|
||||
// Update background script with globalEnable.
|
||||
if (id === 'global_enable') {
|
||||
browser && browser.runtime.sendMessage({ globalEnable: value });
|
||||
}
|
||||
|
||||
// Handle redirect settings
|
||||
} else {
|
||||
const redirect_url = REDIRECT_URLS[id];
|
||||
saveObj = {
|
||||
...REDIRECT_OPTIONS_TEMPLATE,
|
||||
[id]: true,
|
||||
redirect_url
|
||||
};
|
||||
|
||||
// Update background script with changed redirect_url.
|
||||
browser && browser.runtime.sendMessage({ redirect_url });
|
||||
}
|
||||
|
||||
// Update options page.
|
||||
Object.entries(saveObj).forEach(([id, value]) => HTML.setAttribute(id, value));
|
||||
if ('checked' in button) button.checked = value;
|
||||
|
||||
|
||||
// Update local storage.
|
||||
browser.storage.local.set(saveObj);
|
||||
|
||||
const settings = saveObj;
|
||||
|
||||
// Update running tabs.
|
||||
if (settings) {
|
||||
browser.tabs.query({}, tabs => {
|
||||
tabs.forEach(tab => {
|
||||
browser.tabs.sendMessage(tab.id, { settings });
|
||||
});
|
||||
});
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
function populateOptions(sections, settings={}) {
|
||||
|
||||
// Clear the options list
|
||||
|
||||
Reference in New Issue
Block a user