Skip to content

Commit

Permalink
forbidFocus() (youtube scrolls when expanding the description)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImprovedTube committed Jul 28, 2024
1 parent 1a99851 commit 162d6a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
7 changes: 6 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@
});
*/
/*---------------------------
# IMPORTING OLD SETTINGS
# IMPORTING OLD (renamed) SETTINGS (Each one is mostly needed once, but fine to stay unlimited. Legacy.)
-----------------------------*/
chrome.runtime.onInstalled.addListener(function (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) {
if (result.description === 'classic_expanded') {
chrome.storage.local.set({description: 'expanded'});
}
});

Check warning on line 37 in background.js

View workflow job for this annotation

GitHub Actions / lint-and-test

Trailing spaces not allowed
// 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
Expand Down
22 changes: 17 additions & 5 deletions js&css/web-accessible/www.youtube.com/appearance.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,26 @@ ImprovedTube.improvedtubeYoutubeButtonsUnderPlayer = function () {
EXPAND DESCRIPTION
------------------------------------------------------------------------------*/
ImprovedTube.expandDescription = function (el) {
if (this.storage.description === "expanded" || this.storage.description === "classic_expanded") {
if (el) {el.click(); setTimeout(function () {ImprovedTube.elements.player.focus();}, 1200); }
else { var tries = 0; var intervalMs = 210; if (location.href.indexOf('/watch?') !== -1) {var maxTries = 10;} else {var maxTries = 0;}
// ...except when it is an embedded player?
if (this.storage.description === "expanded") {
ImprovedTube.forbidFocus = function (ms) {

Check warning on line 426 in js&css/web-accessible/www.youtube.com/appearance.js

View workflow job for this annotation

GitHub Actions / lint-and-test

Multiple spaces found before 'function'
const originalFocus = HTMLElement.prototype.focus; // Backing up default method - other methods: Element.prototype.scrollIntoView window.scrollTo window.scrollBy
// Override YouTube's scroll method:
HTMLElement.prototype.focus = function() {console.log("Preventing YouTube's scripted scrolling, when expanding the video description for you"); }

Check warning on line 429 in js&css/web-accessible/www.youtube.com/appearance.js

View workflow job for this annotation

GitHub Actions / lint-and-test

Missing space before function parentheses
setTimeout(() => { HTMLElement.prototype.focus = originalFocus;}, ms); // Restoring JS's "focus()"

Check warning on line 430 in js&css/web-accessible/www.youtube.com/appearance.js

View workflow job for this annotation

GitHub Actions / lint-and-test

Trailing spaces not allowed
}
if (el) {

Check warning on line 432 in js&css/web-accessible/www.youtube.com/appearance.js

View workflow job for this annotation

GitHub Actions / lint-and-test

Trailing spaces not allowed
try { ImprovedTube.forbidFocus(2000); } catch { setTimeout(function () {ImprovedTube.elements.player.focus();}, 1200); }

Check warning on line 433 in js&css/web-accessible/www.youtube.com/appearance.js

View workflow job for this annotation

GitHub Actions / lint-and-test

Trailing spaces not allowed
el.click();
}
else { // this rest will be unnecessary with proper timing:
var tries = 0; var intervalMs = 210; if (location.href.indexOf('/watch?') !== -1) {var maxTries = 10;} else {var maxTries = 0;} // ...except when it is an embedded player?
var waitForDescription = setInterval(() => {
if (++tries >= maxTries) {
if (el) {
try { ImprovedTube.forbidFocus(2000); } catch { setTimeout(function () {ImprovedTube.elements.player.focus();}, 1000); }

Check warning on line 441 in js&css/web-accessible/www.youtube.com/appearance.js

View workflow job for this annotation

GitHub Actions / lint-and-test

Trailing spaces not allowed
el.click();

Check warning on line 442 in js&css/web-accessible/www.youtube.com/appearance.js

View workflow job for this annotation

GitHub Actions / lint-and-test

Trailing spaces not allowed
}
el = document.querySelector('#description-inline-expander')
if ( el) { el.click(); setTimeout(function () {ImprovedTube.elements.player.focus(); }, 1200); clearInterval(waitForDescription); }
intervalMs *= 1.11; }}, intervalMs);
}
}
Expand Down

0 comments on commit 162d6a4

Please sign in to comment.