Skip to content

Commit

Permalink
Remove old annotations when switching videos
Browse files Browse the repository at this point in the history
  • Loading branch information
isaackd committed Jan 14, 2020
1 parent e0555f0 commit 8a855b9
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const annotationParser = new AnnotationParser();
let renderer;
let adPlaying = false;

waitForControls().then(el => {
waitForElement(".ytp-right-controls").then(el => {
const progressButton = document.createElement("button");
progressButton.classList.add("ytp-button", "ytp-settings-button");

Expand Down Expand Up @@ -65,10 +65,6 @@ function formatSeconds(sec) {
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === "video_change") {

if (renderer && renderer.annotations) {
renderer.annotations = [];
}

window.dispatchEvent(new CustomEvent("ar-status-change", {
detail: "Video changing..."
}));
Expand All @@ -77,9 +73,15 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
const videoId = currentUrl.searchParams.get("v");

const isYoutubeWatchUrl = document.URL.startsWith("https://www.youtube.com/watch?");

if (videoId && videoId.length === 11 && videoId !== currentVideoID && isYoutubeWatchUrl) {
currentVideoID = videoId;
sendResponse(videoId);

if (renderer) {
renderer.removeAnnotationElements();
renderer.annotations = [];
}
}
else {
sendResponse(false);
Expand All @@ -99,7 +101,6 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
foundAnnotations: true
});
}).catch(e => {
console.info(e);
console.info(`Retrieving annotations for the current video...`);
window.dispatchEvent(new CustomEvent("ar-status-change", {
detail: "Retrieving annotations for the current video...\nThis may sometimes take a little while."
Expand Down Expand Up @@ -143,7 +144,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
}
// popup annotation loading
else if (request.type === "popup_load_youtube" && request.data) {
console.info("loading youtube data");
console.info("Loading youtube data");
const annotationDom = annotationParser.xmlToDom(request.data);
const annotationElements = annotationDom.getElementsByTagName("annotation");
const annotations = annotationParser.parseYoutubeAnnotationList(annotationElements);
Expand All @@ -155,7 +156,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
}
}
else if (request.type === "popup_load_converted" && request.data) {
console.info("loading converted data");
console.info("Loading converted data");
const annotations = annotationParser.deserializeAnnotationList(request.data);
if (!renderer) {
startNewAnnotationRenderer(annotations);
Expand Down Expand Up @@ -259,23 +260,21 @@ function changeAnnotationData(annotations) {
renderer.start();
}

function waitForControls() {
function waitForElement(selector, maxRetries=10, intervalAmount=200, intervalStep=200) {
return new Promise((resolve, reject) => {
const maxRetries = 10;
let currentRetries = 0;
let intervalAmount = 200;

const progInt = setInterval(() => {
currentRetries++;
intervalAmount += 200;
intervalAmount += intervalStep;

if (currentRetries > maxRetries) {
reject();
clearInterval(progInt);
return;
}

const el = document.querySelector(".ytp-right-controls");
const el = document.querySelector(selector);
if (el) {
resolve(el);
clearInterval(progInt);
Expand All @@ -284,3 +283,4 @@ function waitForControls() {
}, intervalAmount);
});
}

0 comments on commit 8a855b9

Please sign in to comment.