Skip to content

Instantly share code, notes, and snippets.

@Kelsin33
Created May 4, 2018 15:40
Show Gist options
  • Save Kelsin33/e9694a26735c7ae30d4c53faf0156789 to your computer and use it in GitHub Desktop.
Save Kelsin33/e9694a26735c7ae30d4c53faf0156789 to your computer and use it in GitHub Desktop.
$(document).ready(function(){
$("#player").hide();
$('#questionForm').submit(function(ev) {
ev.preventDefault(); // to stop the form from submitting
// Patterns defined using the RegEx syntax JS uses (a subset of PCRE)
var patterns = [
[/.*personal information.*/, 'personal info'],
[/.*data.*/, 'data'],
[/.*(risk)|(threat).*security.*/, 'security threat'],
[/test/, 'test']
]
//var vid = document.getElementById("myVideo");
var userInput = ev.target['userInput']
console.log("Input3:", userInput.value);
// For every pattern we have defined
for (var i = patterns.length - 1; i >= 0; i--) {
// Test it against the string to see if it matches
var res = userInput.value.search(patterns[i][0])
if ( res != -1 ){
console.log("Pattern matched:", patterns[i][1])
// Trigger a video playback here using patterns[i][1] as the key
// 2. This code loads the IFrame Player API code asynchronously.
//$("#player").show();
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'wD14_8GMkpc',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
break
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment