Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YouTube copy at time & start from time #184

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions chrome/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@
"perms_page_breakdown_perm5r1": {
"message": "Sending sources to players from the background script."
},
"perms_page_breakdown_perm6h": {
"message": "Access the contextMenus API"
},
"perms_page_breakdown_perm6d": {
"message": "FastStream needs to be able to create context menu entries."
},
"perms_page_breakdown_perm6r1": {
"message": "Copy the video URL at the current time for YouTube."
},
"perms_page_granted": {
"message": "Granted"
},
Expand Down Expand Up @@ -938,5 +947,8 @@
},
"subtitles_settings_defaultLanguage": {
"message": "Language"
},
"context_copy_video_url": {
"message": "Copy video URL at current time"
}
}
36 changes: 36 additions & 0 deletions chrome/background/background.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
requestId: -1,
customHeaders: headers,
}, frame, mode);
} else if (msg.type === 'create_context_menu') {
tryCreateContextMenu();
sendResponse('ok');
return true;
} else {
return;
}
Expand Down Expand Up @@ -996,6 +1000,38 @@ chrome.tabs.onRemoved.addListener((tabid, removed) => {
delete CachedTabs[tabid];
});

chrome.tabs.onActivated.addListener(() => {
tryCreateContextMenu();
});

function tryCreateContextMenu() {
chrome.tabs.query({
active: true,
}, (tabs) => {
const [tab] = tabs;
if (!tab) return;

chrome.contextMenus.removeAll();
if (!URLUtils.is_url_yt_watch(tab.url) || !CachedTabs[tab.id]) return;

chrome.contextMenus.create({
id: 'fast-stream-copy-video-url',
title: chrome.i18n.getMessage('context_copy_video_url'),
contexts: ['video'],
});

chrome.contextMenus.onClicked.addListener((event) => {
const videoId = URLUtils.get_yt_identifier(event.pageUrl);
if (!videoId) return;

chrome.tabs.sendMessage(tab.id, {
type: 'copy_current_time',
videoId,
});
});
});
}

chrome.tabs.onUpdated.addListener((tabid, changeInfo, tab) => {
if (CachedTabs[tabid]) {
if (changeInfo.url) {
Expand Down
3 changes: 3 additions & 0 deletions chrome/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ window.addEventListener('message', (e) => {
}
}
break;
case 'copy_clipboard':
navigator.clipboard.writeText(dt.text);
break;
}
});

Expand Down
3 changes: 2 additions & 1 deletion chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"storage",
"tabs",
"webRequest",
"declarativeNetRequest"
"declarativeNetRequest",
"contextMenus"
],
"host_permissions": [
"<all_urls>"
Expand Down
10 changes: 10 additions & 0 deletions chrome/perms.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ <h4><span data-i18n="perms_page_breakdown_perm5h"></span> <span class="permstatu
<br><a href="https://github.com/Andrews54757/FastStream/blob/5c0b31a4e75aacffb0f2d3b2166a0b58fa4c1440/chrome/background/background.mjs#L640-L653" target="_blank">background.mjs L640-L653</a>
</li>
</ul>

<h4><span data-i18n="perms_page_breakdown_perm6h"></span> <span class="permstatus" data-perm="contextMenus"></span></h4>
<p data-i18n="perms_page_breakdown_perm6d"></p>
<ul>
<li>
<span data-i18n="perms_page_breakdown_perm6r1"></span>
<!--TODO: update to a valid URL-->
<br><a href="https://github.com/Andrews54757/FastStream/blob/5c0b31a4e75aacffb0f2d3b2166a0b58fa4c1440/chrome/background/background.mjs#L1006-L1020" target="_blank">background.mjs L1006-L1020</a>
</li>
</ul>
</div>
<div class="center" style="font-size: 8px;color: rgba(255, 255, 255, 0.4)">Copyright © 2017-2024 Andrew S
([email protected])</div>
Expand Down
34 changes: 26 additions & 8 deletions chrome/player/FastStreamClient.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {DefaultToolSettings} from './options/defaults/ToolSettings.mjs';
import {AudioAnalyzer} from './modules/analyzer/AudioAnalyzer.mjs';
import {PreviewFrameExtractor} from './modules/analyzer/PreviewFrameExtractor.mjs';
import {ReferenceTypes} from './enums/ReferenceTypes.mjs';
import {PlayerModes} from './enums/PlayerModes.mjs';

const SET_VOLUME_USING_NODE = !EnvUtils.isSafari();

Expand Down Expand Up @@ -511,24 +512,42 @@ export class FastStreamClient extends EventEmitter {
console.error(e);
}

let newTime = 0;
if (source.mode === PlayerModes.ACCELERATED_YT) {
const url = new URL(source.url);
const timeString = url.searchParams.get('t')?.replace('s', '') ?? url.searchParams.get('start') ?? '0';
newTime = Number(timeString);

if (this.progressMemory && this.options.storeProgress) {
if (newTime > 0) {
await this.loadProgressData(true, newTime);
}
}

if (newTime === 0 && this.progressMemory && this.options.storeProgress) {
await this.loadProgressData(true);
}

this.emit('setsource', this);
}

async loadProgressData(changeTime = false) {
async loadProgressData(changeTime = false, targetTime = undefined) {
if (this.progressDataLoading || this.progressData) {
return;
}

this.progressDataLoading = true;
this.progressHashesCache = await this.progressMemory.getHashes(this.player.getSource().identifier);
this.progressData = (await this.progressMemory.getFile(this.progressHashesCache)) || {
lastTime: 0,
};

let lastTime = 0;
if (targetTime) {
lastTime = targetTime;
} else {
this.progressHashesCache = await this.progressMemory.getHashes(this.player.getSource().identifier);
this.progressData = (await this.progressMemory.getFile(this.progressHashesCache)) || {
lastTime: 0,
};

lastTime = this.progressData.lastTime;
}

if (!changeTime) {
this.progressDataLoading = false;
Expand All @@ -542,10 +561,9 @@ export class FastStreamClient extends EventEmitter {
clearInterval(interval);
this.context.off(DefaultPlayerEvents.DURATIONCHANGE, changeTimeFn);

if (!this.progressDataLoading || !this.progressData) return;
if (!this.progressDataLoading) return;
this.progressDataLoading = false;

const lastTime = this.progressData.lastTime;
if (lastTime && lastTime < this.duration - 5) {
this.setSeekSave(false);
this.currentTime = lastTime;
Expand Down
8 changes: 8 additions & 0 deletions chrome/player/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ if (EnvUtils.isExtension()) {
} else if (request.type === 'sources' && window.fastStream) {
recieveSources(request, sendResponse);
return true;
} else if (request.type === 'copy_current_time') {
window.parent.postMessage({
type: 'copy_clipboard',
text: `https://youtu.be/${request.videoId}?t=${Math.floor(window.fastStream.persistent.currentTime)}`,
}, '*');

sendResponse('ok');
return true;
} else {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions chrome/player/ui/InterfaceController.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export class InterfaceController {
this.optionsWindow = new OptionsWindow();

this.setupDOM();

chrome.runtime.sendMessage({
type: 'create_context_menu',
});
}

updateToolVisibility() {
Expand Down