<commit_before>songQueue = []
tabID = 0;
loaded = true;
var youtubeKey = "<KEY>"

//Creates url objects used in updating tab url
function makeNextSong(){
	var url = {
		url: songQueue[0]
	}
	return url;
}
//Creates download url object
function makeDwnObj(url){
	var obj = {
		url: url,
		saveAs: true,
		method: "GET"
	}
	return obj;
}
var HttpClient = function() {
    this.get = function(aUrl, aCallback) {
        var anHttpRequest = new XMLHttpRequest();
        anHttpRequest.onreadystatechange = function() {
            if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
                aCallback(anHttpRequest.responseText);
        }

        anHttpRequest.open( "GET", aUrl, true );
		// anHttpRequest.setRequestHeader("Referer", "http://www.youtubeinmp3.com/download/");
        anHttpRequest.send( null );
    }
}

function parseURL(json){
	url = json['link'];
	chrome.tabs.update(tabID, makeDwnObj(url));
}
function getID(url){
	id = url.substring(url.search("v=")+2);
	timeMarker = id.search("&t=");
	if(timeMarker != -1){
		id = id.substring(0,timeMarker);
	}
	console.log("ID of video to display: " + id);
	return id
}
//determines which context Menu clicked, acts accordingly
function onClickHandler(info, tab){
	tabID = tab.id;
	if(info.menuItemId == "addItem"){
		songQueue.push(info.linkUrl);
		console.log("added song tabid = " + tab.id);
		console.log(songQueue[songQueue.length-1] + " added to queue");
	}
	else if (info.menuItemId == "downloadItem"){
		//allows for downloading suggested videos or current video
		if(info.mediaType == "video"){
			dwnURL = info.pageUrl;
		}
		else{
			dwnURL = info.linkUrl;
		}
		var client = new HttpClient();
		client.get(("http://www.youtubeinmp3.com/fetch/?format=JSON&video=" + dwnURL), function(response){
			console.log("Response: " + response);
			var response = JSON.parse(response);
			link = response.link;
			chrome.downloads.download(makeDwnObj(link));
		});
	}
	else if (info.menuItemId == "clearItems"){
		songQueue = [];
	}
	else if (info.menuItemId == "showItems"){
		if(songQueue.length != 0){
			var queueItems = [];
			var songID;
			var videoData;
			var stringItems = "";
			var request = new HttpClient();
			for (var i = 0; i < songQueue.length; i++){
				songID = getID(songQueue[i]);
				request.get(("https://www.googleapis.com/youtube/v3/videos?id=" +
				getID(songQueue[i]) +
				"&key=" + youtubeKey +
				"&part=snippet"), function(response){
					var response = JSON.parse(response);
					queueItems.push('• ' + response["items"][0]["snippet"]["title"] + '\n');
					stringItems += '• ' + response["items"][0]["snippet"]["title"] + '\n';
				if(queueItems.length == songQueue.length){
					var notification = {
						type: "basic",
						iconUrl: "menu.png", //response["items"][0]["snippet"]["thumbnails"]["default"]["url"],
						title: "Queued Items",
						message: stringItems
					}
					chrome.notifications.create("qNoti", notification);
				}
				})
			}
		}
		else{
			var notification = {
				type: "basic",
				iconUrl: "menu.png",
				title: "Queued Items",
				message: "• No items queued"
			}
			chrome.notifications.create("qNoti", notification);
		}
	}
}

//Checks if the tab that signals update is
//	the tab user has queued a video in
function onUpdated(tabId, changeInfo, tab){
	console.log("updated tab id  = " + tabId);
	// Determines if tab should go to next song in queue
	if(!(changeInfo.audible)
	 	&& tabId == tabID
	 	&& changeInfo.status == "complete"
		&& loaded){
			console.log("Song has ended");
			chrome.tabs.update(tabID, makeNextSong());
			songQueue.shift();
			loaded = false;
	}// determines when the next tab has finished loading and is playing music
	else if(changeInfo.audible
		&& tabId == tabID){
			loaded = true;
			console.log("reset 'loaded' boolean")
		}
}

//creates the context Menu objects
var addQItemMenu = chrome.contextMenus.create({"title": "Add to queue", "id": "addItem",
 	"contexts": ["link"], "documentUrlPatterns": ["*://www.youtube.com/*"]});
var downloadItemMenu = chrome.contextMenus.create({"title": "Download Video", "id": "downloadItem",
 	"contexts": ["link", "video"], "documentUrlPatterns": ["*://www.youtube.com/*"]});
var clearQ = chrome.contextMenus.create({"title": "Clear Queue", "id": "clearItems",
	"contexts": ["link", "video", "page"], "documentUrlPatterns": ["*://www.youtube.com/*"]});
var showQ = chrome.contextMenus.create({"title": "Display Queue", "id": "showItems",
	"contexts": ["link", "video", "page"], "documentUrlPatterns": ["*://www.youtube.com/*"]});

chrome.contextMenus.onClicked.addListener(onClickHandler);
chrome.tabs.onUpdated.addListener(onUpdated);<commit_msg>Added 'next song' context item<commit_after>songQueue = []
tabID = 0;
loaded = true;
// iconSwitch = true;
// downloadIconID = 0;
var youtubeKey = "<KEY>"

//Creates url objects used in updating tab url
function makeNextSong(){
	var url = {
		url: songQueue[0]
	}
	return url;
}
//Creates download url object
function makeDwnObj(url, saveOption, name = ""){
	console.log("name" + name);
	if(name == ""){
		var obj = {
			url: url,
			saveAs: true,
			method: "GET"
		}
	}
	else{
		var obj = {
			url: url,
			filename:name,
			conflictAction: 'overwrite',
			saveAs: false,
			method: "GET"
		}
	}
	return obj;
}
var HttpClient = function() {
    this.get = function(aUrl, aCallback) {
        var anHttpRequest = new XMLHttpRequest();
        anHttpRequest.onreadystatechange = function() {
            if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
                aCallback(anHttpRequest.responseText);
        }

        anHttpRequest.open( "GET", aUrl, true );
        anHttpRequest.send( null );
    }
}

function parseURL(json){
	url = json['link'];
	chrome.tabs.update(tabID, makeDwnObj(url, true));
}
function getID(url){
	id = url.substring(url.search("v=")+2);
	timeMarker = id.search("&t=");
	if(timeMarker != -1){
		id = id.substring(0,timeMarker);
	}
	console.log("ID of video to display: " + id);
	return id
}
function makeNoti(strMessage){
	var notification = {
		type: "basic",
		iconUrl: "menu.png",//"C:\\Users\\Keith\\Music\\Downloads\\icon.png",
		title: "Queued Items",
		message: strMessage
	}
	// if(icon){
	// 	notification.iconUrl = "C:\\Users\\Keith\\Music\\Downloads\\icon.png"
	// }
	// else{
	// 	notification.iconUrl = "C:\\Users\\Keith\\Music\\Downloads\\icon1.png"
	// }
	// if(strMessage == "• No items queued"){
	// 	notification.iconUrl = "menu.png";
	// }
	chrome.notifications.create("qNoti", notification);
}
function checkIcon(downloadDelta){
	if(downloadDelta.id == downloadIconID){
		if(downloadDelta.state.current == 'complete'){
			options = {
				iconUrl: "C:\\Users\\Keith\\Music\\Downloads\\icon.png"
			}
			console.log("updated notification successfully");
			chrome.notifications.update("qNoti", options)
		}
	}
}
function updateIcon(){
	iconSwitch = !iconSwitch;
	if(iconSwitch){
		var name = "icon.png";
	}
	else{
		var name = "icon1.png";
	}
	console.log("Update Icon called");
	iconUrl = "";
	var video = getVideoObj(getID(songQueue[0]),
	function(response){
		response = JSON.parse(response);
		iconUrl = response["items"][0]["snippet"]["thumbnails"]["default"]["url"]
		chrome.downloads.download(makeDwnObj(iconUrl, false, name),
		function(id){
			downloadIconID = id;
		})
			// var toErase = {
			// 	id: id
			// }
			// chrome.downloads.erase(toErase)
	});
}
function getVideoObj(id,callback){
	var request = new HttpClient();
	request.get(("https://www.googleapis.com/youtube/v3/videos?id=" +
	id +
	"&key=" + youtubeKey +
	"&part=snippet"), function(response){
		callback(response);
	})
}
//determines which context Menu clicked, acts accordingly
function onClickHandler(info, tab){
	tabID = tab.id;
	if(info.menuItemId == "addItem"){
		songQueue.push(info.linkUrl);
		//calls updateIcon if songQueue goes from empty to having an item
		// if(songQueue.length == 1){
		// 	updateIcon();
		// }
	}
	else if (info.menuItemId == "downloadItem"){
		//allows for downloading suggested videos or current video
		if(info.mediaType == "video"){
			dwnURL = info.pageUrl;
		}
		else{
			dwnURL = info.linkUrl;
		}

		var client = new HttpClient();
		client.get(("http://www.youtubeinmp3.com/fetch/?format=JSON&video=" + dwnURL),
		function(response){
			console.log("Response: " + response);
			var response = JSON.parse(response);
			link = response['link'];
			chrome.downloads.download(makeDwnObj(link, true));
		});
	}
	else if (info.menuItemId == "clearItems"){
		songQueue = [];
	}
	else if (info.menuItemId == "showItems"){
		if(songQueue.length != 0){
			var queueItems = [];
			var songID;
			var videoData;
			var stringItems = "";
			var request = new HttpClient();
			for (var i = 0; i < songQueue.length; i++){
				songID = getID(songQueue[i]);
				request.get(("https://www.googleapis.com/youtube/v3/videos?id=" +
				songID +
				"&key=" + youtubeKey +
				"&part=snippet"),
				function(response){
					var response = JSON.parse(response);
					queueItems.push('• ' + response["items"][0]["snippet"]["title"] + '\n');
					stringItems += '• ' + response["items"][0]["snippet"]["title"] + '\n';
					if(queueItems.length == songQueue.length){
						makeNoti(stringItems);					//.get() is asynch, can't figure out
					}											// callbacks to make notification after for loop completes
				})
			}
		}
		else{
			makeNoti("• No items queued")
		}
	}
	else if (info.menuItemId == "nxtQ"){
		chrome.tabs.update(tabID, makeNextSong());
	}
}

//Checks if the tab that signals update is
//	the tab user has queued a video in
function onUpdated(tabId, changeInfo, tab){
	console.log("updated tab id  = " + tabId);
	// Determines if tab should go to next song in queue
	if(!(changeInfo.audible)
	 	&& tabId == tabID
	 	&& changeInfo.status == "complete"
		&& loaded){
			console.log("Song has ended");
			chrome.tabs.update(tabID, makeNextSong());
			songQueue.shift();
			loaded = false;
	}// determines when the next tab has finished loading and is playing music
	else if(changeInfo.audible && tabId == tabID){
			loaded = true;
			console.log("reset 'loaded' boolean");
			// if(songQueue.length > 0){
			// 	updateIcon();
			// }
		}
}

//creates the context Menu objects
var addQItemMenu = chrome.contextMenus.create({"title": "Add to queue", "id": "addItem",
 	"contexts": ["link"], "documentUrlPatterns": ["*://www.youtube.com/*"]});
var downloadItemMenu = chrome.contextMenus.create({"title": "Download Video", "id": "downloadItem",
 	"contexts": ["link", "video"], "documentUrlPatterns": ["*://www.youtube.com/*"]});
var clearQ = chrome.contextMenus.create({"title": "Clear Queue", "id": "clearItems",
	"contexts": ["page"], "documentUrlPatterns": ["*://www.youtube.com/*"]});
var showQ = chrome.contextMenus.create({"title": "Display Queue", "id": "showItems",
	"contexts": ["page"], "documentUrlPatterns": ["*://www.youtube.com/*"]});
var next = chrome.contextMenus.create({"title": "Next in queue", "id": "nxtQ",
	"contexts": ["page"], "documentUrlPatterns": ["*://www.youtube.com/*"]});

chrome.contextMenus.onClicked.addListener(onClickHandler);
chrome.tabs.onUpdated.addListener(onUpdated);
// chrome.downloads.onChanged.addListener(checkIcon);
{'fraction_non_alphanumeric': 0.1790459206419973, 'fraction_numerical': 0.0036558181007579136, 'mean_word_length': 3.8116688116688118, 'pattern_counts': {'":': 36, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 3, 'lorem ipsum': 0, 'www.': 15, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4471226', 'n_tokens_mistral': 4224, 'n_tokens_neox': 3796, 'n_words': 835}