Skip to content

Commit

Permalink
Structural changes to content-scripts.js
Browse files Browse the repository at this point in the history
  • Loading branch information
adokseo committed Aug 4, 2021
1 parent f8ffad6 commit 4a213e4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 82 deletions.
148 changes: 67 additions & 81 deletions content-scripts.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
/*------------------------------------------------------------------------------
>>> TABLE OF CONTENTS:
--------------------------------------------------------------------------------
1.0 Empty
2.0 Isset
3.0 Camelize
4.0 Attributes
5.0 Injection
6.0 Storage
7.0 Messages
1.0 Camelize
2.0 Attributes
3.0 Injection
4.0 Storage
5.0 Messages
------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------
1.0 EMPTY
------------------------------------------------------------------------------*/

function empty(element) {
for (var i = element.childNodes.length - 1; i > -1; i--) {
element.childNodes[i].remove();
}
}


/*------------------------------------------------------------------------------
2.0 ISSET
------------------------------------------------------------------------------*/

function isset(variable) {
return !(typeof variable === 'undefined' || variable === null);
}


/*------------------------------------------------------------------------------
3.0 CAMELIZE
1.0 CAMELIZE
------------------------------------------------------------------------------*/

function camelize(string) {
Expand All @@ -46,7 +24,7 @@ function camelize(string) {


/*------------------------------------------------------------------------------
4.0 ATTRIBUTES
2.0 ATTRIBUTES
------------------------------------------------------------------------------*/

function attributes(items) {
Expand Down Expand Up @@ -111,7 +89,7 @@ function attributes(items) {


/*------------------------------------------------------------------------------
5.0 INJECTION
3.0 INJECTION
------------------------------------------------------------------------------*/

function injectScript(string) {
Expand All @@ -138,7 +116,52 @@ function injectStyles(string, id) {


/*------------------------------------------------------------------------------
6.0 STORAGE
4.0 STORAGE LISTENER
------------------------------------------------------------------------------*/

chrome.storage.onChanged.addListener(function (changes) {
for (var key in changes) {
var name = camelize(key.replace(/_/g, '-')),
value = changes[key].newValue;

document.documentElement.setAttribute('it-' + name, value);

injectScript('ImprovedTube.storage[\'' + key + '\']=' + (typeof value === 'boolean' ? value : '\'' + value + '\'') + ';');

if (typeof ImprovedTube[name] === 'function') {
injectScript('ImprovedTube.' + name + '();');
}
}
});


/*------------------------------------------------------------------------------
5.0 MESSAGE LISTENER
------------------------------------------------------------------------------*/

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === 'focus') {
injectScript('ImprovedTube.focus = true;');
} else if (request.action === 'blur') {
injectScript(`
ImprovedTube.focus = false;
document.dispatchEvent(new CustomEvent('improvedtube-blur'));
`);
} else if (request.action === 'improvedtube-pause') {
injectScript(`
if (ImprovedTube.elements.player) {
ImprovedTube.played_before_blur = ImprovedTube.elements.player.getPlayerState() === 1;
ImprovedTube.elements.player.pauseVideo();
}
`);
}

injectScript('ImprovedTube.pageOnFocus();');
});


/*------------------------------------------------------------------------------
6.0 INITIALIZATION
------------------------------------------------------------------------------*/

chrome.storage.local.get('youtube_home_page', function (items) {
Expand All @@ -155,6 +178,8 @@ chrome.storage.local.get('youtube_home_page', function (items) {
option === '/feed/library'
) {
location.replace(option);

return;
}
}
}
Expand All @@ -164,67 +189,28 @@ chrome.storage.local.get('youtube_home_page', function (items) {

ImprovedTube.storage = items;

// <HTML> attributes
attributes(items);

// Isset
textContent += 'isset:' + isset + ',';

// Empty
textContent += 'empty:' + empty + ',';

// Features
for (var key in ImprovedTube) {
if (key !== 'storage') {
textContent += key + ': ' + ImprovedTube[key] + ',';
var value = ImprovedTube[key];

if (typeof value === 'object') {
value = JSON.stringify(value);
}
}

// Storage
textContent += 'storage:' + JSON.stringify(items);
textContent += key + ': ' + value + ',';
}

// Initialization
textContent += '};ImprovedTube.init();';

document.documentElement.dataset.systemColorScheme = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';

injectScript(textContent);
});
});

chrome.storage.onChanged.addListener(function (changes) {
for (var key in changes) {
var name = camelize(key.replace(/_/g, '-')),
value = changes[key].newValue;

document.documentElement.setAttribute('it-' + name, value);

injectScript('ImprovedTube.storage[\'' + key + '\']=' + (typeof value === 'boolean' ? value : '\'' + value + '\'') + ';');
attributes(items);

if (typeof ImprovedTube[name] === 'function') {
injectScript('ImprovedTube.' + name + '();');
if (window.matchMedia) {
document.documentElement.dataset.systemColorScheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
}
});


/*------------------------------------------------------------------------------
7.0 MESSAGES
------------------------------------------------------------------------------*/

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === 'focus') {
injectScript('ImprovedTube.focus = true;');
} else if (request.action === 'blur') {
injectScript('ImprovedTube.focus = false;document.dispatchEvent(new CustomEvent("improvedtube-blur"));');
} else if (request.action === 'improvedtube-pause') {
injectScript('if (ImprovedTube.elements.player) {ImprovedTube.played_before_blur = ImprovedTube.elements.player.getPlayerState() === 1; ImprovedTube.elements.player.pauseVideo();}');
}

injectScript('ImprovedTube.pageOnFocus();');
});
});


chrome.runtime.sendMessage({
enabled: true
});
});
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "'Improve YouTube!' (Video & YouTube Tools)🎧",
"short_name": "ImprovedTube",
"description": "__MSG_description_ext__",
"version": "3.400",
"version": "3.405",
"default_locale": "en",
"icons": {
"128": "assets/icons/128.png",
Expand Down
10 changes: 10 additions & 0 deletions youtube-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,16 @@ ImprovedTube.reverse = function (parent) {
}
};

ImprovedTube.empty = function (element) {
for (var i = element.childNodes.length - 1; i > -1; i--) {
element.childNodes[i].remove();
}
};

ImprovedTube.isset = function (variable) {
return !(typeof variable === 'undefined' || variable === null);

This comment has been minimized.

Copy link
@raszpl

raszpl Aug 5, 2021

Contributor

as I mentioned in #1000

RESET removed variables from storage, but currently it sets them to null (or "null", probably in even older version).

some of the disabled/off functions in my pretty old install were set to a "null" string, those will result in isset returning true

};


/*------------------------------------------------------------------------------
1.0 GENERAL
Expand Down

0 comments on commit 4a213e4

Please sign in to comment.