@iktakahiro/youtube-url-parser
v1.0.2
Published
Parser for YouTube URL
Downloads
528
Readme
youtube-url-parser
Install
npm install @iktakahiro/youtube-url-parser
How to use
import { YouTubeURLParser } from "@iktakahiro/youtube-url-parser"
const parser = new YouTubeURLParser("https://youtu.be/7lmCu8wz8ro?t=2m10s")
console.log(parser.isValid())
// -> true
console.log(parser.getId())
// -> "7lmCu8wz8ro"
console.log(parser.getCanonicalURL())
// -> "https://www.youtube.com/watch?v=7lmCu8wz8ro&t=2m10s"
console.log(parser.getShortURL())
// -> "https://youtu.be/7lmCu8wz8ro&t=2m10s"
console.log(parser.getThumbnailURL())
// -> "https://img.youtube.com/vi/7lmCu8wz8ro/0.jpg"
console.log(parser.getStartAtSecond())
// -> 130 (2m * 60s + 10s)
getIframe()
:
console.log(parser.getIframe())
getIframe()
method returns HTML strings containing an iframe element for embedding.
<div class="embed-responsive embed-responsive-16by9">
<iframe
class="embed-responsive-item" type="text/html"
src="https://www.youtube.com/embed/7lmCu8wz8ro?rel=0&start=4830"
frameborder="0" allowfullscreen></iframe>
</div>
with options:
const options = {
allowFullScreen: false,
frameBorder: 1,
noCookie: true,
}
console.log(parser.getIframe(options))
When the params is set, the following HTML strings are returned.
<div class="embed-responsive embed-responsive-16by9">
<iframe
class="embed-responsive-item" type="text/html"
src="https://www.youtube-nocookie.com/embed/7lmCu8wz8ro?rel=0&start=4830"
frameborder="1"></iframe>
</div>
Test
npm test