Merge pull request #72 from OfficialNoob/patch-10

Added Utils file + Updated Parser
This commit is contained in:
Ajay Ramachandran
2019-08-10 19:57:11 -04:00
committed by GitHub
6 changed files with 41 additions and 38 deletions

View File

@@ -265,11 +265,5 @@ function sendRequestToServer(type, address, callback) {
xmlhttp.send(); xmlhttp.send();
} }
function getYouTubeVideoID(url) { // Return video id or false
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
return (match && match[7].length == 11) ? match[7] : false;
}
//uuid generator function from https://gist.github.com/jed/982883 //uuid generator function from https://gist.github.com/jed/982883
function generateUUID(a){return a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,generateUUID)} function generateUUID(a){return a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,generateUUID)}

View File

@@ -1143,26 +1143,3 @@ function sendRequestToCustomServer(type, fullAddress, callback) {
//submit this request //submit this request
xmlhttp.send(); xmlhttp.send();
} }
function getYouTubeVideoID(url) { // Returns with video id else returns false
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
var id = new URL(url).searchParams.get("v");
if (url.includes("/embed/")) {
//it is an embed, don't search for v
id = match[7];
}
return (match && match[7].length == 11) ? id : false;
}
//returns the start time of the video if there was one specified (ex. ?t=5s)
function getYouTubeVideoStartTime(url) {
let searchParams = new URL(url).searchParams;
var startTime = searchParams.get("t");
if (startTime == null) {
startTime = searchParams.get("time_continue");
}
return startTime;
}

View File

@@ -11,6 +11,7 @@
"all_frames": true, "all_frames": true,
"js": [ "js": [
"config.js", "config.js",
"utils.js",
"content.js", "content.js",
"popup.js" "popup.js"
], ],
@@ -46,6 +47,7 @@
}, },
"background": { "background": {
"scripts":[ "scripts":[
"utils.js",
"config.js", "config.js",
"background.js" "background.js"
], ],

View File

@@ -193,5 +193,6 @@
<!-- Scripts that need to load after the html --> <!-- Scripts that need to load after the html -->
<script src="config.js"></script> <script src="config.js"></script>
<script src="utils.js"></script>
<script src="popup.js"></script> <script src="popup.js"></script>
</html> </html>

View File

@@ -1127,13 +1127,6 @@ function runThePopup() {
xmlhttp.send(); xmlhttp.send();
} }
function getYouTubeVideoID(url) { // Returns with video id else returns false
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
var id = new URL(url).searchParams.get("v");
return (match && match[7].length == 11) ? id : false;
}
//end of function //end of function
} }
@@ -1143,4 +1136,4 @@ if (chrome.tabs != undefined) {
//this means it is actually opened in the popup //this means it is actually opened in the popup
runThePopup(); runThePopup();
} }

36
utils.js Normal file
View File

@@ -0,0 +1,36 @@
function getYouTubeVideoID(url) {
//Attempt to parse url
try {
let urlObject = new URL(url);
} catch (e) {
console.error("[SB] Unable to parse URL: " + url);
return false
}
//Check if valid hostname
if(!["www.youtube.com","www.youtube-nocookie.com"].includes(urlObject.host)) return false;
//Get ID from searchParam
if ((urlObject.pathname == "/watch" || urlObject.pathname == "/watch/") && urlObject.searchParams.has("v")) {
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;
}
}
}
//returns the start time of the video if there was one specified (ex. ?t=5s)
function getYouTubeVideoStartTime(url) {
let searchParams = new URL(url).searchParams;
let startTime = searchParams.get("t");
if (startTime == null) {
startTime = searchParams.get("time_continue");
}
return startTime;
}