-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First shot at refactoring out common code
See #137 When this is actually done, js/zap-common.js will be moved into a bower module and end up moving to something like: bower_components/zap-common/index.js
- Loading branch information
Shakeel Mohamed
committed
Jan 20, 2016
1 parent
12d6bf4
commit 150fa2e
Showing
4 changed files
with
114 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*eslint-disable no-unused-vars*/ | ||
function getParameterByName(url, name) { | ||
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); | ||
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), | ||
results = regex.exec(url); | ||
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | ||
} | ||
|
||
function getSearchResults(query, youTubeDataApiKey, onData, onFail) { | ||
$.getJSON("https://www.googleapis.com/youtube/v3/search", { | ||
key: youTubeDataApiKey, | ||
part: "snippet", | ||
q: query, | ||
type: "video" | ||
}, onData).fail(onFail); | ||
} | ||
|
||
function getAutocompleteSuggestions(query, callback) { | ||
$.getJSON("https://suggestqueries.google.com/complete/search?callback=?", { | ||
q: query, | ||
client: "youtube", | ||
ds: "yt" | ||
}, callback); | ||
} | ||
|
||
// The url parameter could be the video ID | ||
function parseYoutubeVideoID(url) { | ||
var videoInfo = { | ||
format: "other", | ||
id: null | ||
}; | ||
var shortUrlDomain = "youtu.be"; | ||
var longUrlDomain = "youtube.com"; | ||
|
||
if (url && url.length > 0) { | ||
// youtube.com format | ||
if (url.indexOf(longUrlDomain) !== -1) { | ||
videoInfo.format = longUrlDomain; | ||
videoInfo.id = getParameterByName(url, "v"); | ||
} | ||
// youtu.be format | ||
else if (url.indexOf(shortUrlDomain) !== -1) { | ||
videoInfo.format = shortUrlDomain; | ||
var endPosition = url.indexOf("?") === -1 ? url.length : url.indexOf("?"); | ||
var offset = url.indexOf(shortUrlDomain) + shortUrlDomain.length + 1; // Skip over the slash also | ||
videoInfo.id = url.substring(offset, endPosition); | ||
} | ||
// Assume YouTube video ID string | ||
else { | ||
videoInfo.format = "video ID"; | ||
videoInfo.id = url; | ||
} | ||
|
||
var slashPos = videoInfo.id.indexOf("/"); | ||
// We found a slash in the video ID (ex: real id is ABC123, but saw ABC123/zen) | ||
// So, only keep what's before the slash | ||
if (slashPos !== -1) { | ||
videoInfo.id = videoInfo.id.substring(0, slashPos); | ||
} | ||
|
||
return videoInfo; | ||
} | ||
} | ||
/*eslint-enable */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters