Skip to content

Commit

Permalink
chrome extension
Browse files Browse the repository at this point in the history
  • Loading branch information
onlylemi committed Jun 2, 2016
1 parent bbba055 commit 321f036
Show file tree
Hide file tree
Showing 13 changed files with 464 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Jianbin Qi
Copyright (c) 2016 onlylemi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Download file/floder For GitHub. Chrome Extension

![](https://raw.githubusercontent.com/onlylemi/res/master/download-any-for-github-icon.png)

It's a chrome extension. When we look up some resources in `GitHub`, and usually we need to look them in local. At this time we must use `git clone`, `Download ZIP` or `Open in Desktop`. But soemtimes we don't need to download the whole repository and only to look some floders or some files. So this chrome extension can help you achieve your demand.

It can download any files or floders from repository in github.

## Preview

Let we look named [butterknife](https://github.com/JakeWharton/butterknife) repository from [JakeWharton](https://github.com/JakeWharton).

![](https://raw.githubusercontent.com/onlylemi/res/master/download-any-for-github-preview.png)

## Thanks

* [gitzip](https://github.com/KinoLien/gitzip)

## About

Welcome to pull requests.

If you have any new idea about this project, feel free to [contact me](mailto:[email protected]). :smiley:
18 changes: 18 additions & 0 deletions css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
table.files td.download {
width: 16px;
text-align: right;
padding-right: 6px;
color: #6DBE51;
}

.display-none{
display: none;
}

.loading{
position: relative;
top: 3px;
display: inline-block;
margin-top: -3px;
margin-left: -2px;
}
Binary file added icon/icon-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon/icon-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon/icon-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
273 changes: 273 additions & 0 deletions js/API.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
(function(scope){
function fn(){};

var repoExp = new RegExp("^https://github.com/([^/]+)/([^/]+)(/(tree|blob)/([^/]+)(/(.*))?)?");
var githubProvidedUrl = new RegExp("^https://api.github.com/.*");
var isBusy = false;

var statusHandle = function(status){
if(status == 'error' || status == 'done') isBusy = false;
else isBusy = true;
};

/**
* @typedef ResolvedURL
* @type Object
* @property {string} author - The project owner
* @property {string} project - The project name
* @property {string} branch - The default branch or other branches
* @property {string} type - The type of url link, values: tree, blob, link?
* @property {string} path - The path of target file/dir based on root repo url.
* @property {string} inputUrl - The input url
* @property {string} rootUrl - The root dir url
*/

/**
* This callback would call by each progress changes.
* @callback progressCallback
* @param {string} status - indicates the status description like 'error', 'prepare', 'processing', 'done'
* @param {string} message - the messages of the above status.
* @param {number} percent - from 0 to 100, indicates the progress percentage.
*/
var progressCallback = function(status, message, percent){};

/**
* Resolve the github repo url for recognize author, project name, branch name, and so on.
* @private
* @param {string} repoUrl - The github repo url.
* @param {ResolvedURL}
*/
function resolveUrl(repoUrl){
if(typeof repoUrl != 'string') return;
var matches = repoUrl.match(repoExp);
if(matches && matches.length > 0){
var root = (matches[5])?
"https://github.com/" + matches[1] + "/" + matches[2] + "/tree/" + matches[5] :
repoUrl;
return {
author: matches[1],
project: matches[2],
branch: matches[5],
type: matches[4],
path: matches[7] || '',
inputUrl: repoUrl,
rootUrl: root
};
}
}

/**
* Force to trigger download dialog for any mine-type files using Native A Element.
* @param {string} url - The URL.
* @param {object|undefined} callbackScope - The scope of the progressCallback function.
*/
function downloadZipUseElement(url, callbackScope){
var down = document.createElement('a');
down.setAttribute('download', true);
down.href = url;
down.addEventListener('click', function(e){
progressCallback.call(callbackScope, 'done', 'Saving File.');
});
setTimeout(function(){
// link has to be in the page DOM for it to work with Firefox
document.body.appendChild(down);
down.click();
down.parentNode.removeChild(down);
},100);
}

/**
* Force to trigger download dialog for any mine-type files.
* @param {string} url - The URL.
* @param {object|undefined} callbackScope - The scope of the progressCallback function.
*/
function downloadZip(url, callbackScope){
callbackScope = callbackScope || scope;
if(url){
progressCallback.call(callbackScope, 'processing', 'Fetching target url: ' + url);

$.get(url)
.fail(function(jqXHR, textStatus, errorThrown){
console.error('downloadZip > $.get fail:', textStatus);
if (errorThrown) throw errorThrown;
})

.done(function(data, textStatus, jqXHR){
var blob = new Blob([data], {
type: jqXHR.getResponseHeader('Content-Type') ||
'application/octet-stream'
});

var down = document.createElement('a');
down.download = url.substring(url.lastIndexOf('/') + 1);
down.href = URL.createObjectURL(blob);

down.addEventListener('click', function(e){
progressCallback.call(callbackScope, 'done', 'Saving File.');
});

setTimeout(function(){
// link has to be in the page DOM for it to work with Firefox
document.body.appendChild(down);
down.click();
down.parentNode.removeChild(down);
}, 100);
});
}
}

/**
* Download zip file from github api url.
* @param {string} zipName - The zip file name.
* @param {string} url - The github api url.
* @param {object|undefined} callbackScope - The scope of the progressCallback function.
*/
function zipIt(zipName, url, callbackScope){
callbackScope = callbackScope || scope;
if(url && githubProvidedUrl.test(url)){
progressCallback.call(callbackScope, 'prepare', 'Fetching list of Dir contains files.');
$.ajax({
url: url + "?recursive=1",
success: function(results){
var promises = [];
var fileContents = [];
if(results.truncated){
progressCallback.call(callbackScope, 'error', 'The tree travels is over than API limitation (500 files)');
throw ("The tree travels is over than API limitation (500 files)");
};
progressCallback._idx = 0;
progressCallback._len = 0;
results.tree.forEach(function(item){
if(item.type == "blob") progressCallback._len++;
});
results.tree.forEach(function(item){
if(item.type == "blob"){
promises.push(Promise.resolve(
$.ajax({
url: item.url,
success: (function(path){
return function(results){
fileContents.push({path:path,content:results.content});
progressCallback.call(callbackScope, 'processing', 'Fetched ' + path,
++progressCallback._idx / (progressCallback._len * 2) * 100);
};
})(item.path)
})
));
}
});

Promise.all(promises).then(function() {
var zip = new JSZip();
fileContents.forEach(function(item){
progressCallback.call(callbackScope, 'processing', 'Compressing ' + item.path,
++progressCallback._idx / (progressCallback._len * 2) * 100);
zip.file(item.path, item.content, {createFolders:true,base64:true});
});
saveAs(zip.generate({type:"blob"}), zipName + ".zip");
progressCallback.call(callbackScope, 'done', 'Saving ' + zipName + '.zip');
},function(item){
if(item){
progressCallback.call(callbackScope, 'error', 'Error: ' + JSON.stringify(item));
throw (JSON.stringify(item) + " ERROR");
}
});
},
error:function(e){
progressCallback.call(callbackScope, 'error', 'Error: ' + e);
throw (e);
}
});
}
}

/**
* Download zip for single file from input repo URL.
* @param {string} pathToFolder - The URL of the Github repository.
* @param {object|undefined} callbackScope - The scope of the progressCallback function.
*/
function createURL(pathToFolder, callbackScope){
if(isBusy) throw "GitZip is busy...";
callbackScope = callbackScope || scope;
progressCallback.call(callbackScope, 'prepare', 'Resolving URL');
var resolved = resolveUrl(pathToFolder);
if(!resolved){
progressCallback.call(callbackScope, 'error', 'Invalid URL: value is [' + pathToFolder.toString() + ']');
throw "INVALID URL";
}
if(!resolved.path){
// root
var durl = [
"https://github.com", resolved.author, resolved.project,
"archive", (resolved.branch || 'master')
].join('/');
var gitURL = durl + ".zip";
// downloadZip(gitURL, callbackScope);
downloadZipUseElement(gitURL, callbackScope);
} else{
// get up level url
var originInput = resolved.inputUrl;
if(resolved.type == "tree"){
var news = originInput.split('/');
news.pop();
resolved = resolveUrl(news.join('/'));
}
progressCallback.call(callbackScope, 'prepare', 'Finding file/dir content path from resolved URL');
$.ajax({
url: "https://api.github.com/repos/"+ resolved.author +
"/" + resolved.project + "/contents/" + resolved.path +
(resolved.branch? ("?ref=" + resolved.branch) : ""),
success: function(results) {
var templateText = '';
if(!Array.isArray(results)){
if(results.message){
progressCallback.call(callbackScope, 'error', 'Github said: '+results.message);
throw ("Error: " + results.message);
}else downloadZip(results.download_url, callbackScope);
return;
}
for(var i = 0, len = results.length; i < len; i++){
var item = results[i];
// target has found
if(item.type == "dir" && item.html_url == originInput){
var valueText = item.path;
var pathText = valueText.split('/').pop();
var urlText = item.git_url;
zipIt(pathText, urlText, callbackScope);
break;
}
if(i + 1 == len){
progressCallback.call(callbackScope, 'error', 'File/Dir content not found.');
}
}
},
error: function(results){
progressCallback.call(callbackScope, 'error', 'Github said: ' + JSON.stringify(results));
throw (JSON.stringify(results));
}
});
}
}

/**
* Register the progress callback for handleing the progress is changing.
* @param {progressCallback} inputFn - The progress callback.
*/
function registerCallback(inputFn){
if(typeof inputFn == 'function'){
// progressCallback = callback;
progressCallback = function(){
inputFn.apply(this, arguments);
statusHandle.apply(this, arguments);
};
}
}

fn.zipRepo = createURL;
fn.zipFromApiUrl = zipIt;
fn.downloadFile = downloadZip;
fn.registerCallback = registerCallback;

scope.GitZip = fn;

})(window);
2 changes: 2 additions & 0 deletions js/FileSaver.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

chrome.runtime.onInstalled.addListener(function (details) {
console.log('previousVersion', details.previousVersion);
});

chrome.tabs.onUpdated.addListener(function (tabId) {
chrome.pageAction.show(tabId);
});
Loading

0 comments on commit 321f036

Please sign in to comment.