From af1a930d34fd4651244c6f86f8ae3192050538f9 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Fri, 25 Mar 2016 18:56:57 +0400 Subject: Add options page, token caching etc --- css/main.css | 11 ++ githunt.html | 360 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ img/icon128.png | Bin 0 -> 5438 bytes img/icon16.png | Bin 0 -> 536 bytes img/icon48.png | Bin 0 -> 16848 bytes index.html | 372 -------------------------------------------------------- js/main.js | 66 ++++++++-- js/options.js | 113 +++++++++++++++++ manifest.json | 36 ++++++ options.html | 39 ++++++ 10 files changed, 614 insertions(+), 383 deletions(-) create mode 100644 githunt.html create mode 100644 img/icon128.png create mode 100644 img/icon16.png create mode 100644 img/icon48.png delete mode 100644 index.html create mode 100644 js/options.js create mode 100644 manifest.json create mode 100644 options.html 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/githunt.html b/githunt.html new file mode 100644 index 0000000..5a6d50e --- /dev/null +++ b/githunt.html @@ -0,0 +1,360 @@ + + + + + Githunt + + + + + +
+ +
+ +
+ +
+
+

Githunt

+

Find the awesome things being built with the technologies of your like

+
+ +
+ + + + +
+
+ +
+
+ +
+

Wait, hunting them down

+
+ + + + + + + diff --git a/img/icon128.png b/img/icon128.png new file mode 100644 index 0000000..33eb7cf Binary files /dev/null and b/img/icon128.png differ diff --git a/img/icon16.png b/img/icon16.png new file mode 100644 index 0000000..11394ec Binary files /dev/null and b/img/icon16.png differ diff --git a/img/icon48.png b/img/icon48.png new file mode 100644 index 0000000..b2b8837 Binary files /dev/null and b/img/icon48.png differ diff --git a/index.html b/index.html deleted file mode 100644 index ad5722f..0000000 --- a/index.html +++ /dev/null @@ -1,372 +0,0 @@ - - - - - Githunt - - - - - - - - - - - - -
- -
- -
- -
-
-

Githunt

-

Find what people are building with the technologies of your like

-
- -
- - - - -
-
- -
-
- -
-

Wait, hunting them down

-
- - - - - diff --git a/js/main.js b/js/main.js index 5d5ffdc..eea322b 100644 --- a/js/main.js +++ b/js/main.js @@ -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('

Oops! Seems to be a problem with your API token. Could you verify the token in `Githunt Options`.

'); + + // Reset the token + filterStorage.getStorage().removeItem(tokenStorageKey); + } else if (message && (message.indexOf('rate limit') !== -1)) { + $('.main-content').replaceWith('

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.

'); + } 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 @@ + + + + + Githunt + + + + + +
+
+ +
+
+
+

Githunt Options

+

Find the awesome things being built with the technologies of your like

+
+
+ +
+ +

To make sure that you keep hunting smoothly, you may generate and add a personal API token to override the Rate Limit imposed by Github

+ +

Add API Token

+ + Save +
+
+ +
+ + + + + + + -- cgit v1.3