Skip to content

Commit

Permalink
some minor fixes to the proposed PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Adnan Ahmed committed Aug 9, 2016
1 parent 9463933 commit 3cb0991
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Like on Git",
"short_name": "LikeOnGit",
"description": "Use this extension to save your links to your GitHub repo.",
"version": "1.0.2",
"version": "1.1.0",
"background": {
"scripts": [
"scripts/widget.js"
Expand Down
44 changes: 21 additions & 23 deletions scripts/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@
chrome.browserAction.onClicked.addListener(function (tab) {

// get current selected tab
chrome.tabs.query({ active: true, currentWindow: true }, function (arrayOfTabs) {
chrome.tabs.query({active: true, currentWindow: true}, function (arrayOfTabs) {

const activeTab = arrayOfTabs[0];

const base_url = 'https://api.github.com/repos'
const base_url = 'https://api.github.com/repos';

const url = `${base_url}/${get('owner')}/${get('repo')}/contents/${get('path')}?access_token=${get('token')}`

console.log(url)
const url = `${base_url}/${get('owner')}/${get('repo')}/contents/${get('path')}?access_token=${get('token')}`;

fetch(url)
.then(response => response.json())
.then(response => {
let sha = response.sha,
encodedContent = response.content,
decodedContent = window.atob(encodedContent);
decodedContent = decodeURIComponent(escape(window.atob(encodedContent)));

// If the file is empty
if (decodedContent.trim().length === 0 )
decodedContent += '# today-i-liked \nContent that I liked. Saved using https://goo.gl/Wj595G \n'
if (decodedContent.trim().length === 0)
decodedContent += '# today-i-liked \nContent that I liked. Saved using https://goo.gl/Wj595G \n';

// append header
if (!isCurrentDateExists(decodedContent))
if (!isCurrentDateExists(decodedContent))
decodedContent += getDateHeader();

// append url
decodedContent += `- [${activeTab.title}](${activeTab.url}) \n`
decodedContent += `- [${activeTab.title}](${activeTab.url}) \n`;

// decode content
encodedContent = window.btoa(unescape(encodeURIComponent(decodedContent)));
Expand All @@ -44,14 +42,14 @@ chrome.browserAction.onClicked.addListener(function (tab) {
}
}
}).then(commit => fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(commit)
}))
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(commit)
}))
.then(success => setSuccessIcon())
.catch(error => setErrorIcon())
.catch(error => setErrorIcon());

/**
* Get value from storage
Expand All @@ -60,7 +58,7 @@ chrome.browserAction.onClicked.addListener(function (tab) {
* @returns {string}
*/
function get(val) {
return localStorage.getItem(val) ? localStorage.getItem(val) : ''
return localStorage.getItem(val) ? localStorage.getItem(val) : '';
}

/**
Expand Down Expand Up @@ -89,7 +87,7 @@ chrome.browserAction.onClicked.addListener(function (tab) {
*/
function getCurrentDate() {
const date = new Date();
return `${monthNames()[date.getMonth()]} ${pad(date.getDate())}, ${date.getFullYear()}`;
return `${monthNames()[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()}`;
}

/**
Expand Down Expand Up @@ -118,22 +116,22 @@ chrome.browserAction.onClicked.addListener(function (tab) {
* Set default icon
*/
function setDefaultIcon() {
sleep(1000).then(() => chrome.browserAction.setIcon({ path: 'icons/standard-16.png' }));
sleep(1000).then(() => chrome.browserAction.setIcon({path: 'icons/standard-16.png'}));
}

/**
* Set success icon
*/
function setSuccessIcon() {
chrome.browserAction.setIcon({ path: 'icons/check-mark.png' });
setDefaultIcon()
chrome.browserAction.setIcon({path: 'icons/check-mark.png'});
setDefaultIcon();
}

/**
* Set error icon
*/
function setErrorIcon() {
chrome.browserAction.setIcon({ path: 'icons/cross-mark.png' });
chrome.browserAction.setIcon({path: 'icons/cross-mark.png'});
setDefaultIcon();
}

Expand Down

0 comments on commit 3cb0991

Please sign in to comment.