From 7e9b34ca0bda3a0c9fd44b4a80b856966bad9862 Mon Sep 17 00:00:00 2001 From: kaos Date: Tue, 23 Jun 2015 22:55:39 +0200 Subject: [PATCH] added function copy permalink --- gif.js => background.js | 13 +++++++-- main.js | 61 ++++++++++++++++++++++++++++------------- manifest.json | 2 +- 3 files changed, 54 insertions(+), 22 deletions(-) rename gif.js => background.js (81%) diff --git a/gif.js b/background.js similarity index 81% rename from gif.js rename to background.js index bf66dd3..f813aab 100644 --- a/gif.js +++ b/background.js @@ -1,12 +1,13 @@ function onClickHandler(info, tab) { chrome.tabs.getSelected(null, function(tab) { switch( info.menuItemId ){ + case "copyPostPermalink": case "copyGifUrl": copyToClipboard( info, tab ); - break; + break; case "downloadGif": chrome.tabs.sendMessage(tab.id, {command: info.menuItemId}); - break; + break; } }); @@ -42,6 +43,14 @@ chrome.contextMenus.create({ "title": "Download Gif", }, confirmMenuCreation ); +chrome.contextMenus.create({ "title": "Copy post permalink", + "contexts":["link"], + "id": "copyPostPermalink", + "documentUrlPatterns": ["*://9gag.com/*"] + + }, + confirmMenuCreation ); + function confirmMenuCreation(){ // console.log("Created context menu"); } diff --git a/main.js b/main.js index 7e2a61c..bb9317e 100644 --- a/main.js +++ b/main.js @@ -19,11 +19,14 @@ function setLongPostListener(){ sidebar.html(""); var image = jQuery("#sidebar-longPost img"); - image.on('error', function(){ - image.attr("src", post.attr('href').replace("/gag/", "http://img-9gag-fun.9cache.com/photo/") + "_700b.jpg"); - }); - - image.attr("src", post.attr('href').replace("/gag/", "http://img-9gag-fun.9cache.com/photo/") + "_700b_v1.jpg"); +// image.on('error', function(){ +// image.attr("src", post.attr('href').replace("/gag/", "http://img-9gag-fun.9cache.com/photo/") + "_700b_v1.jpg"); +// }); +// +// image.attr("src", post.attr('href').replace("/gag/", "http://img-9gag-fun.9cache.com/photo/") + "_700b.jpg"); + jQuery.get(post.attr('href'), function(content) { + image.attr("src", jQuery(content).find('.badge-item-img').attr("src")); + }) // post.remove(); }); @@ -53,6 +56,18 @@ function setVideoListener(){ currentVideo.gifUrl = currentVideo.target.parent().data("image").replace("a.gif", ".gif"); currentVideo.name = currentVideo.target.parents("article").find("h2").text().trim() + ".gif"; }); + jQuery("#list-view-2").on('contextmenu', "a", function(e) { + currentVideo.target = jQuery(event.target) ; + previous = currentVideo.target.parents("article").prev(); + if (previous.length > 0){ + currentVideo.permalink = "http://9gag.com/?id=" + previous.data("entry-id"); + }else{ + currentVideo.permalink = "http://9gag.com/?id=" + currentVideo.target.parents("article").data("entry-id"); + } + if (currentVideo.permalink == undefined){ + console.log("Warning: unable to find the permalink for this post") + } + }); } @@ -119,20 +134,25 @@ jQuery(document).ready(function() { nightMode(); console.log("9gag Mod Successfully Loaded!"); - // hash = window.location.hash.substring(1); - // if (hash){ - // count = 0; - // while (count < 15){ - // count++; - // article = jQuery("article[data-entry-id='" + window.location.hash +"']"); - // if (article.length){ - // jQuery("html, body").animate({ scrollTop: jQuery("article[data-entry-id='" + hash +"']").offset().top}, 1000); - // break; - // }else{ - // jQuery("html, body").animate({ scrollTop: jQuery("div.loading").offset().top}, 1000); - // } - // } - // } + hash = window.location.hash.substring(1); + if (hash){ + console.log(jQuery(document)); + jQuery(document).ajaxSuccess(function() { + console.log("An individual AJAX call has completed successfully"); + }); + count = 0; + while (count < 150){ + count++; +// console.log(count); + article = jQuery("article[data-entry-id='" + window.location.hash +"']"); + if (article.length){ + jQuery("html, body").animate({ scrollTop: jQuery("article[data-entry-id='" + hash +"']").offset().top}, 1); + break; + }else{ + jQuery("html, body").animate({ scrollTop: jQuery("div.loading").offset().top}, 1); + } + } + } }); @@ -143,6 +163,9 @@ chrome.extension.onMessage.addListener( case "copyGifUrl": sendResponse({ url: currentVideo.gifUrl }); break; + case "copyPostPermalink": + sendResponse({ url: currentVideo.permalink }); + break; case "downloadGif": downloadURI( currentVideo.gifUrl , currentVideo.name); break; diff --git a/manifest.json b/manifest.json index 9654411..0eb4b90 100644 --- a/manifest.json +++ b/manifest.json @@ -16,7 +16,7 @@ }], "background": { "persistent": false, - "scripts": ["gif.js"] + "scripts": ["background.js"] }, "manifest_version": 2 }