diff options
| author | Kamran Ahmed <kamranahmed.se@gmail.com> | 2016-03-25 18:56:57 +0400 |
|---|---|---|
| committer | Kamran Ahmed <kamranahmed.se@gmail.com> | 2016-03-25 18:56:57 +0400 |
| commit | af1a930d34fd4651244c6f86f8ae3192050538f9 (patch) | |
| tree | 2a2de59d23bbb8ddb1e9061692664869a8870fc5 | |
| parent | Add gitignore (diff) | |
| download | githunt-af1a930d34fd4651244c6f86f8ae3192050538f9.tar.gz | |
Add options page, token caching etc
| -rw-r--r-- | css/main.css | 11 | ||||
| -rw-r--r-- | githunt.html (renamed from index.html) | 20 | ||||
| -rw-r--r-- | img/icon128.png | bin | 0 -> 5438 bytes | |||
| -rw-r--r-- | img/icon16.png | bin | 0 -> 536 bytes | |||
| -rw-r--r-- | img/icon48.png | bin | 0 -> 16848 bytes | |||
| -rw-r--r-- | js/main.js | 66 | ||||
| -rw-r--r-- | js/options.js | 113 | ||||
| -rw-r--r-- | manifest.json | 36 | ||||
| -rw-r--r-- | options.html | 39 |
9 files changed, 258 insertions, 27 deletions
diff --git a/css/main.css b/css/main.css index 36cdec4..defbebe 100644 --- a/css/main.css +++ b/css/main.css @@ -44,6 +44,7 @@ a { margin: 0 5px; background: white; box-shadow: 2px 2px black; + outline: none; } .active { @@ -223,3 +224,13 @@ div.footer span.footer-stat .fa { white-space: nowrap; overflow: hidden; } + +.muted { + color: #BFB7B7; +} + +.quote-item { + margin: 60px 0 20px; + padding: 20px 10px; + background: #ccc; +} diff --git a/index.html b/githunt.html index ad5722f..5a6d50e 100644 --- a/index.html +++ b/githunt.html @@ -4,18 +4,6 @@ <meta charset=UTF-8> <title>Githunt</title> <link rel=stylesheet href=css/main.css> - - - <script src="js/jquery.js"></script> - <script src="js/moment.min.js"></script> - <script src=js/main.js></script> - <script> - $(function () { - var hubTab = new HubTab(); - hubTab.init(); - }); - </script> - <link href='https://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="css/font-awesome.min.css"/> </head> @@ -29,7 +17,7 @@ <div class="top-header"> <div class="brand"> <h1>Githunt</h1> - <p>Find what people are building with the technologies of your like</p> + <p>Find the awesome things being built with the technologies of your like</p> </div> <div class="top-nav"> @@ -364,9 +352,9 @@ <h3>Wait, hunting them down</h3> </div> + <script src="js/jquery.js"></script> + <script src="js/moment.min.js"></script> <script src="js/particles.js"></script> - <script> - particlesJS.load('particles-js', 'js/particles.json', function () {}); - </script> + <script src=js/main.js></script> </body> </html> diff --git a/img/icon128.png b/img/icon128.png Binary files differnew file mode 100644 index 0000000..33eb7cf --- /dev/null +++ b/img/icon128.png diff --git a/img/icon16.png b/img/icon16.png Binary files differnew file mode 100644 index 0000000..11394ec --- /dev/null +++ b/img/icon16.png diff --git a/img/icon48.png b/img/icon48.png Binary files differnew file mode 100644 index 0000000..b2b8837 --- /dev/null +++ b/img/icon48.png @@ -5,7 +5,7 @@ * @returns {{persistFilters: persistFilters, populateFilters: populateFilters}} * @constructor */ -function FilterStorage() { +function HubStorage() { /** * Returns the storage object if available, otherwise @@ -18,8 +18,10 @@ function FilterStorage() { } return { - setItem: function () {}, - getItem: function () {} + setItem: function () { + }, + getItem: function () { + } } }; @@ -30,7 +32,7 @@ function FilterStorage() { var persistFilters = function (selector) { var storage = getStorage(); - $(selector).each(function(index, input){ + $(selector).each(function (index, input) { var $input = $(input), name = $input.attr('name'); @@ -46,7 +48,7 @@ function FilterStorage() { var populateFilters = function (selector) { var storage = getStorage(); - $(selector).each(function(index, input){ + $(selector).each(function (index, input) { var $input = $(input), name = $input.attr('name'), value = storage.getItem(name); @@ -72,6 +74,14 @@ function FilterStorage() { */ populateFilters: function (selector) { return populateFilters(selector); + }, + + /** + * Returns the localStorageObject + * @returns {{setItem, getItem}} + */ + getStorage: function () { + return getStorage(); } }; } @@ -85,15 +95,16 @@ function HubTab() { var trendingRequest = false, // To make sure that there are no parallel requests repoGroupSelector = '.content-batch', // Batch of repositories filterSelector = '.repos-filter', // Selector that matches every repo filter on page - mainContainer= '.main-content', // Main container div + mainContainer = '.main-content', // Main container div dateHead = '.date-head', // Heading item for the batch of repositories dateAttribute = 'date', // Date attribute on the date head of batch - token = 'be604a0cd14b81bf3523a1a420cbad0a4d3eccda', // API token. Don't grin, it's a dummy one ¯\_(ツ)_/¯ + // token = 'a1a420cbad0a4d3eccda', // API token. Don't grin, it's a dummy one ¯\_(ツ)_/¯ languageFilter = '#language', // Filter for repositories language dateFilter = '#date-jump', // Date jump filter i.e. weekly, monthly or yearly + tokenStorageKey = 'githunt_token', // Storage key for the github token reposApiUrl = 'https://api.github.com/search/repositories'; // URL for the repos - var filterStorage = new FilterStorage(); + var filterStorage = new HubStorage(); /** * Generates the HTML for batch of repositories @@ -173,8 +184,17 @@ function HubTab() { langCondition = 'language:' + language + ' '; } + // If user has set the github token in storage pass that + // alongside the request. + var token = $.trim(filterStorage.getStorage().getItem(tokenStorageKey)), + apiToken = ''; + + if (token) { + apiToken = '&access_token=' + token; + } + return { - queryParams: '?sort=stars&order=desc&q=' + langCondition + 'created:"' + dateRange.lower + ' .. ' + dateRange.upper + '"&access_token=' + token, + queryParams: '?sort=stars&order=desc&q=' + langCondition + 'created:"' + dateRange.lower + ' .. ' + dateRange.upper + '"' + apiToken, dateRange: dateRange }; }; @@ -185,8 +205,9 @@ function HubTab() { */ var fetchTrendingRepos = function () { - // If there is some request, already in progress - if (trendingRequest !== false) { + // If there is some request, already in progress or there was + // an error, do not allow further requests. + if ((trendingRequest !== false) || ($('.error-quote').length !== 0)) { return false; } @@ -203,6 +224,21 @@ function HubTab() { var finalHtml = generateReposHtml(data.items, filters.dateRange.lower, filters.dateRange.upper); $(mainContainer).append(finalHtml); }, + error: function(xhr, status, error) { + var error = JSON.parse(xhr.responseText), + message = error.message || ''; + + if (message && message.toLowerCase() == 'bad credentials') { + $('.main-content').replaceWith('<h3 class="quote-item error-quote">Oops! Seems to be a problem with your API token. Could you verify the token in `Githunt Options`.</h3>'); + + // Reset the token + filterStorage.getStorage().removeItem(tokenStorageKey); + } else if (message && (message.indexOf('rate limit') !== -1)) { + $('.main-content').replaceWith('<h3 class="quote-item error-quote">Oops! Seems like you did not set the API token. Wait an hour for github to refresh your rate limit or better add a token in `Githunt Options` to hunt more.</h3>'); + } else { + $('.main-content').replaceWith('Oops! Could you please refresh the page.'); + } + }, complete: function () { trendingRequest = false; $('.loading-more').addClass('hide'); @@ -252,3 +288,11 @@ function HubTab() { } }; } + +$(function () { + var hubTab = new HubTab(); + hubTab.init(); + + particlesJS.load('particles-js', 'js/particles.json', function () { + }); +}); diff --git a/js/options.js b/js/options.js new file mode 100644 index 0000000..331b05e --- /dev/null +++ b/js/options.js @@ -0,0 +1,113 @@ +/** + * Filter Storage to assist in storing the filter to localStorage + * and retrieving to populate back + * + * @returns {{persistFilters: persistFilters, populateFilters: populateFilters}} + * @constructor + */ +function HubStorage() { + + /** + * Returns the storage object if available, otherwise + * a polyfill + * @returns {*} + */ + var getStorage = function () { + if (window.localStorage) { + return window.localStorage; + } + + return { + setItem: function () {}, + getItem: function () {} + } + }; + + /** + * Persists the values of the provided input input selector + * @param selector` + */ + var persistFilters = function (selector) { + var storage = getStorage(); + + $(selector).each(function(index, input){ + var $input = $(input), + name = $input.attr('name'); + + storage.setItem(name, $input.val()); + }); + }; + + /** + * Retrieves the values for the provided selector from localstorage + * and populates the fields back + * @param selector + */ + var populateFilters = function (selector) { + var storage = getStorage(), + anyPopulated = false; + + $(selector).each(function(index, input){ + var $input = $(input), + name = $input.attr('name'), + value = storage.getItem(name); + + if (value) { + $input.val(value); + anyPopulated = true; + } + }); + + return anyPopulated; + }; + + return { + /** + * Persist the fields matching the provided selector to local storage + * @param selector + */ + persistFilters: function (selector) { + persistFilters(selector); + }, + + /** + * Populates the filters using the values stored in the localstorage + * @param selector + */ + populateFilters: function (selector) { + return populateFilters(selector); + } + }; +} + +function HubOptions() { + + var hubStorage = new HubStorage(); + + function bindUI() { + $(document).on('click', '.save-token', function (e) { + e.preventDefault(); + + hubStorage.persistFilters('.githunt_token'); + $('.quote-item').html('Woohoo! Token saved, happy hunting.'); + }); + } + + return { + init: function () { + var tokenPopulated = hubStorage.populateFilters('.githunt_token'); + if (tokenPopulated) { + $('.quote-item').html('Token already saved. Better go for the hunt!'); + } + + bindUI(); + } + }; +} + +$(function () { + var hubOptions = new HubOptions(); + hubOptions.init(); + + particlesJS.load('particles-js', 'js/particles.json', function () {}); +}); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..ac77dac --- /dev/null +++ b/manifest.json @@ -0,0 +1,36 @@ +{ + "manifest_version": 2, + "name": "Githunt", + "short_name": "Githunt", + "description": "Find the awesome things being built with the technologies of your like", + "version": "1", + "permissions": [ + "tabs", + "storage", + "webNavigation", + "*://*.github.com/*" + ], + "icons": { + "16": "img/icon16.png", + "48": "img/icon48.png", + "128": "img/icon128.png" + }, + "chrome_url_overrides": { + "newtab": "githunt.html" + }, + "homepage_url": "http://kamranahmed.info/", + "options_page": "options.html", + "browser_action": { + "default_title": "Githunt", + "default_icon": "img/icon128.png" + }, + "background": { + "scripts": [ + "js/jquery.js", + "js/main.js", + "js/particles.js", + "js/moment.min.js" + ], + "persistent": false + } +} diff --git a/options.html b/options.html new file mode 100644 index 0000000..a28b950 --- /dev/null +++ b/options.html @@ -0,0 +1,39 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Githunt</title> + <link rel="stylesheet" href="css/main.css"/> + <link href='https://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'> + <link rel="stylesheet" href="css/font-awesome.min.css"/> +</head> +<body> +<div class=main> + <div id="particles-js"></div> + + <div class="container"> + <div class="top-header"> + <div class="brand"> + <h1>Githunt <span class="muted">Options</span></h1> + <p>Find the awesome things being built with the technologies of your like</p> + </div> + </div> + + <div class="options-content"> + + <p class="quote-item">To make sure that you keep hunting smoothly, you may <a href="https://github.com/settings/tokens" target="_blank">generate and add a personal API token</a> to override the Rate Limit imposed by Github</p> + + <h3 style="margin: 30px 5px 10px;">Add API Token</h3> + <input type="text" name="githunt_token" class="githunt_token btn" style="width: 300px;" /> + <a href="#" class="btn save-token">Save</a> + </div> + </div> + +</div> + +<script src="js/jquery.js"></script> +<script src="js/moment.min.js"></script> +<script src="js/particles.js"></script> +<script src=js/options.js></script> +</body> +</html> |
