Skip to content

Commit

Permalink
🐛 Fix button not loading on navigation to video
Browse files Browse the repository at this point in the history
When navigating from an index page (e.g. /results) to a video page
(i.e. /watch), the toggle button was not loading.

Listen to a more reliable event for triggering the injection, one that
fires on any video page regardless of the user journey.
  • Loading branch information
dideler committed Sep 2, 2017
1 parent 154e697 commit 0dc8bfb
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ function toggleComments() {

function inject(e) {
if (e.type == 'spfdone') {
injected = false; // reset for old UI navigation
injected = false; // old UI requires re-injection on navigation
}
if (injected) {
return; // guard clause because event 'yt-register-action' fires many times
return; // guard clause because we're using a generic event that fires many times
}

if (isOldInterface()) {
addClass();
addButton();
} else {
if (e.target.id != 'comments') {
return; // do not inject if triggering too early
if (e.target.tagName != 'YTD-VIDEO-SECONDARY-INFO-RENDERER') {
return;
}

const oldTriggerEvents = ['DOMContentLoaded', 'spfdone'];
if (oldTriggerEvents.includes(e.type)) {
return; // do not inject for old UI events on new UI
return; // do not inject on old UI events
}

addNewClass();
Expand Down Expand Up @@ -110,7 +110,8 @@ function showReadMore() {
}

(function() {
document.addEventListener('DOMContentLoaded', inject); // Static navigation (i.e. initial page load)
document.addEventListener('spfdone', inject); // Dynamic navigation (i.e. subsequent page loads)
document.addEventListener('yt-register-action', inject); // Polymer event for new UI
document.addEventListener('DOMContentLoaded', inject); // Old: Static navigation (i.e. initial page load)
document.addEventListener('spfdone', inject); // Old: Dynamic navigation (i.e. subsequent page loads)

document.addEventListener('yt-visibility-refresh', inject); // New: Inject on info panel render.
})();

0 comments on commit 0dc8bfb

Please sign in to comment.