KgBot / YT Saver

// ==UserScript==
// @name         YT Saver
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Save link and name of YouTube videos you watch for later use
// @author       Stefan Ninic
// @match        https://www.youtube.com/watch?v=*
// @match        https://www.m.youtube.com/watch?v=*
// @match        http://www.youtube.com/watch?v=*
// @match        http://www.m.youtube.com/watch?v=*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
// @grant        GM_registerMenuCommand
// @grant        GM_listValues
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// ==/UserScript==
/* jshint -W097 */
'use strict';

$(document).ready(function(){
    var videoLink = $("link[itemprop='url']").attr("href");
    var videoTitle = $("meta[itemprop='name']").attr("content");
    var videoDescription = $("meta[itemprop='description']").attr("content");
    var videoThumb = $("link[itemprop='thumbnailUrl']").attr("href");
    var videoViews = $("meta[itemprop='interactionCount']").attr("content");
    var videoPublished = $("meta[itemprop='datePublished']").attr("content");
    var videoGenre = $("meta[itemprop='genre']").attr("content");
    
    function saveVideo(){
        var savedVideos = GM_getValue(videoTitle);
        if(savedVideos != null && savedVideos != undefined){
           alert("Video is already in the database. NOTE: Try to reload page if you think this is not correct.");
        }
        else{
            var videoInfo = {
                "videoLink": videoLink,
                "videoTitle": videoTitle,
                "videoDescription": videoDescription,
                "videoThumb": videoThumb,
                "videoViews": videoViews,
                "videoPublished": videoPublished,
                "videoGenre": videoGenre
            };
            GM_setValue(videoTitle, videoInfo);
            alert("Video saved to database.");
        }
    }
    
    function listAllVideos(){
        var videos = GM_listValues();
        if(videos.length != 0){
            var table = $("<ul id='com-kgbot-table'></ul>");
            var header = $("<li id='com-kgbot-header'><span class='com-kgbot-cell'>Title</span>\
                          <span class='com-kgbot-cell'>Description</span><span class='com-kgbot-cell'>Thumbnail</span>\
                          <span class='com-kgbot-cell'>Views</span><span class='com-kgbot-cell'>Published</span><span class='com-kgbot-cell'>Genre</span>\
                          </li>");
            $(table).append(header);
            $.each(videos, function(key, item){
                var video = GM_getValue(item);
                var info = $("<li class='com-kgbot-row'><span class='com-kgbot-cell'><a href='" + video["videoLink"] + "'>" + video["videoTitle"] + "</a></span>\
                             <span class='com-kgbot-cell'>" + video["videoDescription"] + "</span><span class='com-kgbot-cell'><img width='60' height='60' src='" + video["videoThumb"] + "'></img></span>\
                             <span class='com-kgbot-cell'>" + video["videoViews"] + "</span><span class='com-kgbot-cell'>" + video["videoPublished"] + "</span>\
                             <span class='com-kgbot-cell'>" + video["videoGenre"] + "</span></li>");
                $(table).append(info);
            })
            var style = $("<link id='com-kgbot-ytstyle' rel='stylesheet' href='https://a4bab336e6b626b3e4202cf18eb54affdd39eeb1.googledrive.com/host/0B9Uj-nqky_xPMzhCRDAyRnp4bFk/ytstyle.css'>");
            $("head").append(style);
            $("body").prepend(table);
        }
        else{
            alert("There are no videos in database.");
        }
    }
    
    function clearVideos(){
        var videos = GM_listValues();
        $.each(videos, function(key, item){
            GM_deleteValue(item);
        })
        alert("Database cleared.");
    }
    
    function hideVideos(){
        $("#com-kgbot-table").remove();
        $("#com-kgbot-ytstyle").remove();
    }
    
    GM_registerMenuCommand("Save video", saveVideo);
    GM_registerMenuCommand("List All Videos", listAllVideos);
    GM_registerMenuCommand("Delete All Videos", clearVideos);
    GM_registerMenuCommand("Hide All Videos", hideVideos);
})