diff --git a/gif.js b/gif.js index a9c0b7e..5057329 100644 --- a/gif.js +++ b/gif.js @@ -19,10 +19,10 @@ function onClickHandler(info, tab) { chrome.contextMenus.onClicked.addListener(onClickHandler); chrome.runtime.onInstalled.addListener(function() { - chrome.contextMenus.create({"title": "Copy gif url to clipboard", "contexts": ["video"], + chrome.contextMenus.create({"title": "Copy Gif Url", "contexts": ["video"], "id": "copyGifUrl", "documentUrlPatterns": ["*://9gag.com/"]}); - chrome.contextMenus.create({"title": "Download gif file", "contexts": ["video"], + chrome.contextMenus.create({"title": "Download Gif", "contexts": ["video"], "id": "downloadGif", "documentUrlPatterns": ["*://9gag.com/"]}); }); diff --git a/main.js b/main.js index bfbbfad..fc8c155 100644 --- a/main.js +++ b/main.js @@ -1,14 +1,56 @@ -var currentVideo = null; +function setupVideoObject(){ + return { + target: null, + gifUrl: null, + name: null + }; -jQuery("#list-view-2").on( "click", ".badge-evt.post-read-more", function( event ) { - actual = jQuery(event.target); - actual.parent().find("img").attr("src", actual.attr('href').replace("/gag/", "http://img-9gag-fun.9cache.com/photo/") + "_700b_v1.jpg"); - actual.remove(); - event.preventDefault(); -}); +} -jQuery(document).ready(function() { +//roll out long post in home page: +function setLongPostListener(){ + + jQuery("#list-view-2").on( "click", ".badge-evt.post-read-more", function( event ) { + event.preventDefault(); + actual = jQuery(event.target); + actual.parent().find("img").attr("src", actual.attr('href').replace("/gag/", "http://img-9gag-fun.9cache.com/photo/") + "_700b_v1.jpg"); + actual.remove(); + + }); + +} +//clean wake up overlay +function cleanWakeUp(){ jQuery("#overlay-container").remove(); +} + +//set a listener to videos right click +function setVideoListener(){ + jQuery("#list-view-2").on('contextmenu', "video", function(e) { + currentVideo.target = jQuery(event.target) ; + currentVideo.gifUrl = currentVideo.target.parent().data("image").replace("a.gif", ".gif"); + currentVideo.name = currentVideo.target.parents("article").find("h2").text().trim() + ".gif"; + }); +} + + +//download url +function downloadURI(uri, name){ + var link = document.createElement("a"); + link.download = name; + link.href = uri; + link.click(); +} + +//init everything +jQuery(document).ready(function() { + + setLongPostListener(); + cleanWakeUp(); + currentVideo = setupVideoObject(); + setVideoListener(); + console.log("9gag Mod Successfully Loaded!"); + hash = window.location.hash.substring(1); if (hash){ count = 0; @@ -23,30 +65,22 @@ jQuery(document).ready(function() { } } } -}); -jQuery("#list-view-2").on('contextmenu', "video", function(e) { - currentVideo = jQuery(event.target); }); -function downloadURI(uri, name) -{ - var link = document.createElement("a"); - link.download = "dsfsd"; - link.href = uri; - link.click(); -} chrome.extension.onMessage.addListener( function(request, sender, sendResponse) { - if (request.command == "copyGifUrl" || request.command == "downloadGif"){ - url = currentVideo.parent().data("image").replace("a.gif", ".gif") - } - if (request.command == "copyGifUrl"){ - sendResponse({url: url}); - }else if (request.command == "downloadGif"){ - console.log(currentVideo.parents("article").find("h2").text().trim()); - downloadURI(url, currentVideo.parents("article").find("h2").text().trim() + ".gif"); + switch (request.command){ + case "copyGifUrl": + sendResponse({ url: currentVideo.gifUrl }); + break; + case "downloadGif": + downloadURI( currentVideo.gifUrl , currentVideo.name); + break; } } ); + + +