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

getYouTubeVideoID() - support youtube-local pathnames #521

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,17 +802,16 @@ function getYouTubeVideoID(url: string) {
}

//Get ID from searchParam
if (urlObject.searchParams.has("v") && ["/watch", "/watch/"].includes(urlObject.pathname) || urlObject.pathname.startsWith("/tv/watch")) {
let match = null;
if (urlObject.searchParams.has("v") && urlObject.pathname.match(/^\/((youtube\.com\/)?watch\/?$|tv\/watch)/)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is (youtube\.com\/)? for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

youtube-local routes https://youtube.com/watch?v=<vid> to http://your-domain.org/youtube.com/watch?v=<vid>
It does this because it has to route multiple domains, and it probably makes things simpler.

So if youtube-local were to soon also support another video platform (e.g. dailymotion), and they also use /watch?v=, this addon will correctly not transmit the id.
Also note that only ^\/watch would not match youtube-local's routes.

const id = urlObject.searchParams.get("v");
return id.length == 11 ? id : false;
} else if (urlObject.pathname.startsWith("/embed/")) {
try {
return urlObject.pathname.substr(7, 11);
} catch (e) {
console.error("[SB] Video ID not valid for " + url);
return false;
}
}
} else if ((match = urlObject.pathname.match(/^\/(?:youtube.com\/)?(?:embed|youtu.be)\/([^&\/]{11})/))) {
return match[1];
} else if (urlObject.pathname.includes("/tv/") && (match = urlObject.hash.match(/^#\/watch\?(?:.*)&v=([^&\/]{11})/))) {
return match[1];
}

return false;
}

Expand Down