diff --git a/background.js b/background.js index f7233cb..dd84719 100644 --- a/background.js +++ b/background.js @@ -575,6 +575,7 @@ function checkTab(id, isBeforeNav, isRepeat) { let allowOverLock = gOptions[`allowOverLock${set}`]; let showTimer = gOptions[`showTimer${set}`]; let allowKeywords = gOptions[`allowKeywords${set}`]; + let youtubeResume = gOptions[`youtubeResume${set}`]; updateRolloverTime(timedata, limitMins, limitPeriod, periodStart); @@ -683,19 +684,46 @@ function checkTab(id, isBeforeNav, isRepeat) { } else { gTabs[id].keyword = keyword; - if (!gIsAndroid && addHistory && !isInternalPage) { - // Add blocked page to browser history - browser.history.addUrl({ url: pageURLWithHash }); + function finalizeBlockPageRedirection() { + if (!gIsAndroid && addHistory && !isInternalPage) { + // Add blocked page to browser history + browser.history.addUrl({ url: pageURLWithHash }); + } + + // Get final URL for block page + blockURL = getLocalizedURL(blockURL) + .replace(/\$K/g, keyword ? keyword : "") + .replace(/\$S/g, set) + .replace(/\$U/g, pageURLWithHash); + + // Redirect page + browser.tabs.update(id, { url: blockURL }); } - // Get final URL for block page - blockURL = getLocalizedURL(blockURL) - .replace(/\$K/g, keyword ? keyword : "") - .replace(/\$S/g, set) - .replace(/\$U/g, pageURLWithHash); - - // Redirect page - browser.tabs.update(id, { url: blockURL }); + const youtubeRegex = /^http(s)?:\/\/www\.youtube\.com\/watch\?v=.*$/; + if (youtubeRegex.test(pageURLWithHash) && youtubeResume) { + browser.scripting.executeScript({ + target: { tabId: id }, + func: () => { + // Get the video stream from youtube and return the current time + const videoElement = document.querySelector('.video-stream'); + return videoElement ? Math.trunc(videoElement.currentTime) : 'Video element not found on the page.'; + } + }).then((results) => { + const currentTime = results[0].result; + if (typeof currentTime === 'number') { + // Modify the URL to add or modify the "t" parameter with the time + const parsedUrl = new URL(pageURLWithHash); + const searchParams = new URLSearchParams(parsedUrl.search); + searchParams.set('t', currentTime); + parsedUrl.search = searchParams.toString(); + pageURLWithHash = parsedUrl.toString(); + } + finalizeBlockPageRedirection(); + }); + } else { + finalizeBlockPageRedirection(); + } } } diff --git a/common.js b/common.js index 87d29a7..9b1af4b 100644 --- a/common.js +++ b/common.js @@ -69,6 +69,7 @@ const PER_SET_OPTIONS = { regexpBlock: { type: "string", def: "", id: "regexpBlock" }, regexpAllow: { type: "string", def: "", id: "regexpAllow" }, ignoreHash: { type: "boolean", def: true, id: "ignoreHash" }, + youtubeResume: { type: "boolean", def: false, id: "youtubeResume" }, }; const GENERAL_OPTIONS = { diff --git a/manifest.json b/manifest.json index 67d57ce..0076d46 100644 --- a/manifest.json +++ b/manifest.json @@ -69,7 +69,8 @@ "storage", "tabs", "unlimitedStorage", - "webNavigation" + "webNavigation", + "scripting" ], "version": "1.6.4" diff --git a/options.html b/options.html index 37ce565..223d129 100644 --- a/options.html +++ b/options.html @@ -230,6 +230,10 @@

+

+ + +