Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace var with let and convert to arrow functions #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions lib/list.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
(function(){

var storedKeys = Object.keys(localStorage);
let storedKeys = Object.keys(localStorage);

function buttonTemplate(key){
var html = '<small><span class="btn-group"><span class="btn-mini btn-success btn-inline text-xs tb" id="edit-{tmp}">edit</span>'
let buttonTemplate = (key) => {
let html = '<small><span class="btn-group"><span class="btn-mini btn-success btn-inline text-xs tb" id="edit-{tmp}">edit</span>'
html += '<span class="btn-mini btn-danger btn-inline text-xs tb" id="delete-{tmp}">delete</span></span></small>'
html = html.replace(/{tmp}/g, key);
return html;
}

function newTableRow(url, shortcut) {
var tableBody= document.getElementById("table-body");
var table = document.getElementById('table');
var ahref = "<a href=" + url + ">";
var row = "<tr><td>" + buttonTemplate(shortcut) + "&nbsp";
let newTableRow = (url, shortcut) => {
let tableBody= document.getElementById("table-body");
let table = document.getElementById('table');
let ahref = "<a href=" + url + ">";
let row = "<tr><td>" + buttonTemplate(shortcut) + "&nbsp";
row += shortcut + "</td>";
row += "<td>" + ahref + url + "</a></td></tr>";

tableBody.innerHTML += row;
return false
}

function attachEdit(key) {
var edit = document.getElementById('edit-' + key);
let attachEdit = (key) => {
let edit = document.getElementById('edit-' + key);
if (edit) {
edit.addEventListener('click', goListen, false);
}
}

for (i=0; i<localStorage.length; i++) {
var key = storedKeys[i];
var url = localStorage[key];
let key = storedKeys[i];
let url = localStorage[key];
newTableRow(url, key, attachEdit );
}

var doEdit = function(){
var shortcutInput = document.getElementById('shortcut');
var urlInput = document.getElementById('url');
var key = this.id.split('-')[1];
var url = localStorage[key];
let doEdit = () => {
let shortcutInput = document.getElementById('shortcut');
let urlInput = document.getElementById('url');
let key = this.id.split('-')[1];
let url = localStorage[key];
shortcutInput.value = key;
urlInput.value = url;
urlInput.style.width = "200px;";
window.scrollTo(0, 0);
}

var doDelete = function() {
var key = this.id.split('-')[1];
let doDelete = () => {
let key = this.id.split('-')[1];
if ( confirm('Are you sure?') ) {
delete localStorage[key];
if(chrome.storage){
Expand All @@ -56,35 +56,35 @@
}
}

function checkExample(){
var tr = document.getElementsByTagName('tr');
checkExample = () => {
let tr = document.getElementsByTagName('tr');
if(tr.length > 1) {
var example = document.getElementById('example');
let example = document.getElementById('example');
example.remove();
}
}

window.addEventListener('load', function(){
window.addEventListener('load', () => {
checkExample();
for (i=0; i<localStorage.length; i++) {
var key = storedKeys[i];
var url = localStorage[key];
let key = storedKeys[i];
let url = localStorage[key];
document.getElementById("edit-" + key).addEventListener("click", doEdit, false);
document.getElementById("delete-" + key).addEventListener("click", doDelete, false);
}
});

//search function
document.getElementById("search").addEventListener('keyup',function() {
document.getElementById("search").addEventListener('keyup',() => {

var input, filter, table, tr, td, i, txtValue;
let input, filter, table, tr, td, i, txtValue;
input = document.getElementById("search");
filter = input.value.toLowerCase();
table = document.getElementById("table-body");
tr = table.getElementsByTagName("tr");

//loop through all rows, and hide those that don't match search query
for (var i=0; i<tr.length; i++) {
for (let i=0; i<tr.length; i++) {
td = tr[i].getElementsByTagName("td")[1];
if (td) {
txtValue = td.textContent || td.innerText;
Expand Down
66 changes: 33 additions & 33 deletions lib/popup.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
(function () {
var url
var shortcut
var formatUrl
var preTabUrl = false
var preTabId = false
var currentUrl = false
var currentTabId = false
let url
let shortcut
let formatUrl
let preTabUrl = false
let preTabId = false
let currentUrl = false
let currentTabId = false

document.getElementById('shortcut').focus();

// wrap this in if to debug popup.html without extension
if (window.location.href.indexOf('file://') < 0) {
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, (tabs) => {
if (!preTabUrl) {
preTabUrl = tabs[0].url
preTabId = tabs[0].id
Expand All @@ -21,42 +21,42 @@
})
}

var localKeys = function() {
var keys = Object.keys(localStorage)
let localKeys = () => {
let keys = Object.keys(localStorage)
return keys.filter(function (k) { return k !== 'lastSynced'})
}

function checkSyncData () {
var lastChecked = localStorage['lastSynced']
let checkSyncData = () => {
let lastChecked = localStorage['lastSynced']
if (!lastChecked || minsSinceSync(lastChecked) > 5) {
console.log('you should sync')
var now = new Date()
let now = new Date()


for (var i = 0; i < localStorage.length; i++){
for (let i = 0; i < localStorage.length; i++){
localStorage[i]
}
setSyncStorage()
localStorage['lastSynced'] = now
}
}

function minsSinceSync (lastTime) {
var now = new Date()
let minsSinceSync = (lastTime) => {
let now = new Date()
return Math.round((now - lastTime) / 1000 / 60)
}

document.body.onload = function () {
document.body.onload = () => {
checkSyncData()
}

function setSyncStorage (key, value) {
let setSyncStorage = (key, value) => {
if (chrome.storage) {
// chrome.storage.sync.set({lastSynced: now})
}
}

var setShortcut = function (shortcut, url) {
let setShortcut = (shortcut, url) => {
if (shortcut && url) {
if (localStorage[shortcut] !== url) {
localStorage[shortcut] = url
Expand All @@ -67,34 +67,34 @@
return localStorage[shortcut] !== undefined && localStorage[shortcut] !== ''
}

formatUrl = function (input) {
var url = input
formatUrl = (input) => {
let url = input
if (url.indexOf('http') === -1) {
url = 'http://' + url
}
return url
}

document.getElementById('myform').onclick = function (evt) {
document.getElementById('myform').onclick = (evt) => {
evt.preventDefault()
linkify()
}

document.onkeypress = function (e) {
document.onkeypress = (e) => {
if (e.keyCode === 13) {
linkify()
}
}

var showLinks = document.querySelector('#show-links')
let showLinks = document.querySelector('#show-links')

if (showLinks) {
showLinks.addEventListener('click', function (event) {
showLinks.addEventListener('click', (event) => {
chrome.tabs.create({url: 'list.html'})
})
}

function openUrl (url) {
let openUrl = (url) => {
if (preTabUrl !== 'chrome://newtab/') {
window.open(url, '_blank')
} else {
Expand All @@ -103,29 +103,29 @@
}
}

function linkify () {
let linkify = () => {
url = document.getElementById('url').value
shortcut = document.getElementById('shortcut').value
var isSet = setShortcut(shortcut, url)
let isSet = setShortcut(shortcut, url)

if (isSet) {
var short = localStorage[shortcut]
let short = localStorage[shortcut]
url = formatUrl(short)
} else {
url = formatUrl(url)
}
openUrl(url)
}

function grabUrl () {
var urlInput = document.getElementById('url')
let grabUrl = () => {
let urlInput = document.getElementById('url')
urlInput.value = currentUrl
}

var grab = document.getElementById('grab')
let grab = document.getElementById('grab')

if (grab) {
grab.addEventListener('click', function (e) {
grab.addEventListener('click', (e) => {
grabUrl()
e.preventDefault()
})
Expand Down