Browse Source

general clean up and refactoring

master
edoardoo 10 years ago
parent
commit
1d37d7cf91
2 changed files with 62 additions and 28 deletions
  1. +2
    -2
      gif.js
  2. +60
    -26
      main.js

+ 2
- 2
gif.js View File

@ -19,10 +19,10 @@ function onClickHandler(info, tab) {
chrome.contextMenus.onClicked.addListener(onClickHandler); chrome.contextMenus.onClicked.addListener(onClickHandler);
chrome.runtime.onInstalled.addListener(function() { 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", "id": "copyGifUrl",
"documentUrlPatterns": ["*://9gag.com/"]}); "documentUrlPatterns": ["*://9gag.com/"]});
chrome.contextMenus.create({"title": "Download gif file", "contexts": ["video"],
chrome.contextMenus.create({"title": "Download Gif", "contexts": ["video"],
"id": "downloadGif", "id": "downloadGif",
"documentUrlPatterns": ["*://9gag.com/"]}); "documentUrlPatterns": ["*://9gag.com/"]});
}); });

+ 60
- 26
main.js View File

@ -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(); 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); hash = window.location.hash.substring(1);
if (hash){ if (hash){
count = 0; 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( chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) { 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;
} }
} }
); );

Loading…
Cancel
Save