Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality (no-nonsense JS) #2650

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
aacda82
Remove `=== true` and `!== false`
4yman-0 Nov 2, 2024
2b80db5
less nonsense
4yman-0 Nov 2, 2024
b84a0ec
remove ESLint errors
4yman-0 Nov 2, 2024
63edbd5
remove ESLint errors #2
4yman-0 Nov 2, 2024
df9b610
fix ident etc
4yman-0 Nov 2, 2024
1f0e708
Use vscode format (can make code ugly)
4yman-0 Nov 3, 2024
c96a201
Merge branch 'master' into code-quality-experiment
4yman-0 Nov 9, 2024
1463228
Update
4yman-0 Nov 10, 2024
6ee1683
Update
4yman-0 Nov 11, 2024
fd05b18
Fix menu size
4yman-0 Nov 12, 2024
9388443
satus - Use grid for section
4yman-0 Nov 15, 2024
e313e06
satus - Attempt fixing
4yman-0 Nov 15, 2024
6abc84e
satus - Attempt fixing (again)
4yman-0 Nov 15, 2024
2a4ae1b
Add margin to grid
4yman-0 Nov 15, 2024
a9fff72
Go back to flex (w/ %)
4yman-0 Nov 18, 2024
e5c2f3c
Remove more CI Warnings
4yman-0 Nov 18, 2024
7ac6299
Merge branch 'master' into options-max-size
4yman-0 Dec 1, 2024
632cead
Merge branch 'master' into options-max-size
4yman-0 Dec 5, 2024
0271800
Merge branch 'master' into code-quality-experiment
4yman-0 Dec 5, 2024
9493130
New README
4yman-0 Dec 5, 2024
baf359a
Fix links. Add browser stores
4yman-0 Dec 5, 2024
63ab124
Add development stuff
4yman-0 Dec 5, 2024
32e2968
Fix EXxtensions section
4yman-0 Dec 5, 2024
369ea36
Merge branch 'master' into new-readme-experiment
4yman-0 Dec 6, 2024
1dd23ef
Update README headings
4yman-0 Dec 6, 2024
7f0967c
Update README.md
4yman-0 Dec 9, 2024
a60612c
Merge branch 'master' into code-quality-experiment
4yman-0 Dec 10, 2024
631a229
Merge branch 'code-charity:master' into options-max-size
4yman-0 Dec 12, 2024
60b6f95
Fix ESlint
4yman-0 Dec 12, 2024
128e0b6
Fix undefined globals
4yman-0 Dec 12, 2024
f640989
Add PrettierJS
4yman-0 Dec 12, 2024
3d66340
Move Dependencies to DevDependencies
4yman-0 Dec 12, 2024
6b9e14c
Merge branch 'master' into options-max-size
4yman-0 Dec 13, 2024
369da9b
Merge branch 'code-charity:master' into prettier-eslint
4yman-0 Dec 13, 2024
8681561
Use Prettier and ESLint
4yman-0 Dec 13, 2024
04e5dbc
Options max size
4yman-0 Dec 13, 2024
a1028bf
New README
4yman-0 Dec 13, 2024
632d4fc
Merge branch 'v5' into code-quality-experiment
4yman-0 Dec 13, 2024
27f3e44
Merge branch 'master' into code-quality-experiment
4yman-0 Dec 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 58 additions & 55 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,30 @@
// const keepAliveInterval = setInterval(() => chrome.runtime.sendMessage({ status: 'keep-alive' }), 29.5 * 1000);

/* Sidepanel Option
chrome.storage.local.get('improvedTubeSidePanel', function (result) {
if ( result.improvedTubeSidePanel && result.improvedTubeSidePanel === true) {
chrome.storage.local.get('improvedTubeSidePanel', result => {
if (result.improvedTubeSidePanel) {
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true })
} else {chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: false }) }
} else {
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: false })
}
});
*/
/*---------------------------
# IMPORTING OLD (renamed) SETTINGS (Each one is mostly needed once, but fine to stay unlimited. Legacy.)
-----------------------------*/
chrome.runtime.onInstalled.addListener(function (installed) {
chrome.runtime.onInstalled.addListener(installed => {
if (installed.reason == 'update') {
// var thisVersion = chrome.runtime.getManifest().version;
// console.log("Updated from " + installed.previousVersion + " to " + thisVersion + "!");
chrome.storage.local.get('description', function (result) {
//var thisVersion = chrome.runtime.getManifest().version;
//console.log("Updated from " + installed.previousVersion + " to " + thisVersion + "!");
chrome.storage.local.get('description', result => {
if (result.description === 'classic_expanded') {
chrome.storage.local.set({description: 'expanded'});
}
});
});
// Shortcut renames:
chrome.storage.local.get(['shortcut_auto', 'shortcut_144p', 'shortcut_240p', 'shortcut_360p', 'shortcut_480p', 'shortcut_720p', 'shortcut_1080p', 'shortcut_1440p', 'shortcut_2160p', 'shortcut_2880p', 'shortcut_4320p'], function (result) {
// validate and move to new name
for (let [name, keys] of Object.entries(result)) {
// Validate and move to new name
for (const [name, keys] of Object.entries(result)) {
if (!keys) continue;
let newKeys = {},
newName = name.replace('shortcut_', 'shortcut_quality_');
Expand All @@ -54,44 +56,44 @@
chrome.storage.local.remove(Object.keys(result));
});
chrome.storage.local.get(['volume_step', 'playback_speed_step'], function (result) {
for (let [name, value] of Object.entries(result)) {
let newName = 'shortcuts_' + name;
for (const [name, value] of Object.entries(result)) {
const newName = `shortcuts_${name}`;
chrome.storage.local.set({[newName]: value});
}
chrome.storage.local.remove(Object.keys(result));
});
chrome.storage.local.get('player_autoplay', function (result) {
if (result.player_autoplay === false) {
chrome.storage.local.get('player_autoplay', result => {
if (!result.player_autoplay) {
chrome.storage.local.set({player_autoplay_disable: true});
chrome.storage.local.remove(['player_autoplay']);
}
});
chrome.storage.local.get('channel_default_tab', function (result) {
chrome.storage.local.get('channel_default_tab', result => {
if (result.channel_default_tab === '/home') {
chrome.storage.local.set({channel_default_tab: '/'});
}
});
chrome.storage.local.get('player_quality', function (result) {
chrome.storage.local.get('player_quality', result => {
if (result.player_quality === 'auto') {
chrome.storage.local.get('player_quality_auto', function (result) {
chrome.storage.local.get('player_quality_auto', result => {
if (result.player_quality_auto !== 'migrated') {
chrome.storage.local.set({player_quality: 'disabled'});
chrome.storage.local.set({player_quality_auto: 'migrated'});
}
});
}
});
chrome.storage.local.get('hideSubscribe', function (result) {
if (result.hideSubscribe === true) {
chrome.storage.local.get('hideSubscribe', result => {
if (result.hideSubscribe) {
chrome.storage.local.set({subscribe: 'hidden'});
chrome.storage.local.remove(['hideSubscribe']);
}
});
chrome.storage.local.get('limit_page_width', function (result) {
if (result.limit_page_width === false) {
chrome.storage.local.get('limit_page_width', result => {
if (!result.limit_page_width) {
chrome.storage.local.set({no_page_margin: true});
chrome.storage.local.remove(['limit_page_width']);
chrome.storage.local.get('player_size', function (r) {
chrome.storage.local.get('player_size', r => {
if (r.player_size == 'full_window' || r.player_size == 'fit_to_window') {
chrome.storage.local.set({player_size: 'max_width'});
}
Expand All @@ -105,9 +107,9 @@
if (navigator.userAgent.indexOf('Safari') !== -1
&& (!/Windows|Chrom/.test(navigator.userAgent)
|| /Macintosh|iPhone/.test(navigator.userAgent))) {
chrome.storage.local.set({below_player_pip: false})
chrome.storage.local.set({below_player_pip: false});
// still needed? (are screenshots broken in Safari?):
chrome.storage.local.set({below_player_screenshot: false})
chrome.storage.local.set({below_player_screenshot: false});
}
// console.log('Thanks for installing!');
}
Expand All @@ -117,20 +119,20 @@
--------------------------------------------------------------*/
function getLocale (language, callback) {
language = language.replace('-', '_');
fetch('_locales/' + language.substring(0, 2) + '/messages.json').then(function (response) {
fetch('_locales/' + language.substring(0, 2) + '/messages.json').then(response => {
if (response.ok) {
response.json().then(callback);
} else {
fetch('_locales/' + language.substring(0, 2) + '/messages.json').then(function (response) {
fetch('_locales/' + language.substring(0, 2) + '/messages.json').then(response => {
if (response.ok) {
response.json().then(callback);
} else {
getLocale('en', callback);
}
}).catch(function () { getLocale('en', callback); });
}).catch(() => {getLocale('en', callback);});
getLocale('en', callback);
}
}).catch(function () {
}).catch(() => {
getLocale('en', callback);
});
}
Expand All @@ -139,7 +141,7 @@
--------------------------------------------------------------*/
function updateContextMenu (language) {
if (!language || language === 'default') language = chrome.i18n.getUILanguage();
getLocale(language, function (response) {
getLocale(language, response => {
const items = [
'donate',
'rateMe',
Expand All @@ -157,7 +159,7 @@
// contexts: ['browser_action'] //manifest2
});
}
chrome.contextMenus.onClicked.addListener(function (info) {
chrome.contextMenus.onClicked.addListener(info => {
const links = [
'https://www.improvedtube.com/donate',
'https://chrome.google.com/webstore/detail/improve-youtube-video-you/bnomihfieiccainjcjblhegjgglakjdd',
Expand All @@ -168,15 +170,17 @@
});
});
}
chrome.runtime.onInstalled.addListener(function () {
chrome.storage.local.get(function (items) {
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.get(items => {
updateContextMenu(items.language);
});
});

chrome.storage.onChanged.addListener(function (changes) {
if (changes?.language) updateContextMenu(changes.language.newValue);
if (changes?.improvedTubeSidebar) chrome.sidePanel.setPanelBehavior({openPanelOnActionClick: changes.language.newValue});
chrome.storage.onChanged.addListener(changes => {
if (changes?.language)
updateContextMenu(changes.language.newValue);
if (changes?.improvedTubeSidebar)
chrome.sidePanel.setPanelBehavior({openPanelOnActionClick: changes.language.newValue});
});
/*--------------------------------------------------------------
# TAB Helper, prune stale connected tabs
Expand All @@ -187,31 +191,31 @@
windowId;

function tabPrune (callback) {
chrome.tabs.query({ url: 'https://www.youtube.com/*' }).then(function (tabs) {
chrome.tabs.query({ url: 'https://www.youtube.com/*' }).then(tabs => {
let tabIds = [];
for (let tab of tabs) {
for (const tab of tabs) {
if (!tab.discarded && tabConnected[tab.id]) {
tabIds.push(tab.id);
}
}
for (let id in tabConnected) {
for (const id in tabConnected) {
if (!tabIds.includes(Number(id))) {
delete tabConnected[id];
}
}
callback();
}, function () { console.log("Error querying Tabs") });
}, () => { console.log("Error querying Tabs") });
};
/*--------------------------------------------------------------
# TAB FOCUS/BLUR
commented out console.log left intentionally, to help understand
https://issues.chromium.org/issues/41116352
--------------------------------------------------------------*/
chrome.tabs.onActivated.addListener(function (activeInfo) {
chrome.tabs.onActivated.addListener(activeInfo => {
tabPrev = tab;
tab = activeInfo;
//console.log('activeInfo', windowId, tabPrev, tab);
tabPrune(function () {
tabPrune(() => {
if (windowId == tabPrev.windowId) {
if (tabConnected[tabPrev.tabId]) {
chrome.tabs.sendMessage(tabPrev.tabId, {action: 'blur'});
Expand All @@ -224,10 +228,9 @@
}
});
});
chrome.windows.onFocusChanged.addListener(function (wId) {
windowId = wId;
chrome.windows.onFocusChanged.addListener(windowId => {
//console.log('onFocusChanged', windowId, tabPrev, tab);
tabPrune(function () {
tabPrune(() => {
if (windowId != tab.windowId && tab.tabId && !tab.blur && tabConnected[tab.tabId]) {
tab.blur = true;
chrome.tabs.sendMessage(tab.tabId, {action: 'blur'});
Expand All @@ -242,13 +245,13 @@
/*--------------------------------------------------------------
# MESSAGE LISTENER
--------------------------------------------------------------*/
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
//console.log(message);
//console.log(sender);

switch (message.action || message.name || message) {
case 'play':
tabPrune(function () {
tabPrune(() => {
for (let id in tabConnected) {
id = Number(id);
if (id != sender.tab.id) {
Expand Down Expand Up @@ -279,15 +282,15 @@
active: true
}, ts => {
const tID = ts[0]?.id,
data = { type: 'popup',
state: w.state,
width: parseInt(message.width, 10),
height: parseInt(message.height, 10),
left: 0,
top: 20
}

if (tID) {data.tabId = tID;}
data = {
type: 'popup',
state: w.state,
width: parseInt(message.width, 10),
height: parseInt(message.height, 10),
left: 0,
top: 20
};

Check failure on line 292 in background.js

View workflow job for this annotation

GitHub Actions / lint-and-test

Expected indentation of 6 tabs but found 24 spaces
if (tID) data.tabId = tID;
chrome.windows.create(data);

//append to title?
Expand Down
41 changes: 17 additions & 24 deletions js&css/extension/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# GLOBAL VARIABLE
--------------------------------------------------------------*/

var extension = {
let extension = {
domReady: false,
events: {
listeners: {}
Expand All @@ -42,15 +42,14 @@
--------------------------------------------------------------*/

extension.camelize = function (string) {
var result = '';
let result = '';

for (var i = 0, l = string.length; i < l; i++) {
var character = string[i];
for (let i = 0, l = string.length; i < l; i++) {
const character = string[i];

if (character === '_' || character === '-') {
result += character.toUpperCase();
i++;

result += string[i].toUpperCase();
} else {
result += character;
}
Expand All @@ -68,21 +67,21 @@
--------------------------------------------------------------*/

extension.events.on = function (type, listener, options = {}) {
var listeners = extension.events.listeners;
let listeners = extension.events.listeners;

if (!listeners[type]) {
listeners[type] = [];
}

if (options.async === true) {
if (options.async) {
listener = (function (original) {
return async function () {
return new Promise(original);
};
})(listener);
}

if (options.prepend === true) {
if (options.prepend) {
listeners[type].unshift(listener);
} else {
listeners[type].push(listener);
Expand All @@ -94,14 +93,14 @@
--------------------------------------------------------------*/

extension.events.trigger = async function (type, data) {
var listeners = extension.events.listeners[type];
let listeners = extension.events.listeners[type];

if (listeners) {
for (var i = 0, l = listeners.length; i < l; i++) {
var listener = listeners[i];
for (let i = 0, l = listeners.length; i < l; i++) {
let listener = listeners[i];

if (typeof listener === 'function') {
if (listener instanceof(async function () {}).constructor === true) {
if (listener instanceof (async function () { }).constructor) {
await listener(data);
} else {
listener(data);
Expand All @@ -113,8 +112,6 @@

/*--------------------------------------------------------------
# INJECT
----------------------------------------------------------------

--------------------------------------------------------------*/

extension.inject = function (paths, callback) {
Expand All @@ -124,12 +121,10 @@

if (path.indexOf('.css') !== -1) {
element = document.createElement('link');

element.rel = 'stylesheet';
element.href = path;
} else {
element = document.createElement('script');

element.src = path;
}

Expand Down Expand Up @@ -188,9 +183,7 @@

extension.messages.create = function () {
this.element = document.createElement('div');

this.element.id = 'it-messages-from-extension';

this.element.style.display = 'none';

document.documentElement.appendChild(this.element);
Expand Down Expand Up @@ -242,15 +235,15 @@
if (key.indexOf('/') === -1) {
return this.data[key];
} else {
var target = this.data,
path = key.split('/').filter(function (value) {
return value != '';
});
var target = this.data;
/*path = key.split('/').filter(function (value) {
return value != '';
});*/

for (var i = 0, l = key.length; i < l; i++) {
var part = key[i];

if (target.hasOwnProperty(part)) {

Check failure on line 246 in js&css/extension/core.js

View workflow job for this annotation

GitHub Actions / lint-and-test

Do not access Object.prototype method 'hasOwnProperty' from target object
target = target[part];
} else {
return undefined;
Expand Down Expand Up @@ -303,7 +296,7 @@
// initialize theme in case YT is in Dark cookie mode
if (!extension.storage.data['theme'] && document.documentElement.hasAttribute('dark')) {
extension.storage.data['theme'] = 'dark';
chrome.storage.local.set({theme: 'dark'});
chrome.storage.local.set({ theme: 'dark' });
}

for (const key in items) {
Expand Down
Loading
Loading