Skip to content

Commit

Permalink
Prettify code
Browse files Browse the repository at this point in the history
  • Loading branch information
aledeg committed Oct 19, 2020
1 parent 28033bb commit 0093d70
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 57 deletions.
98 changes: 59 additions & 39 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
const linkAddId = 'link-add';
const bookmarkAddId = 'bookmark-add';
const textReducer = (carry, item) => `${carry + item.url}\n`;
const markdownReducer = (carry, item) => `${carry}[${item.title}](${item.url})\n`;
const htmlReducer = (carry, item) => `${carry}<a href="${item.url}">${item.title}</a><br/>\n`;
const dokuwikiReducer = (carry, item) => `${carry}[[${item.url}|${item.title}]]\n`;
const phpbbReducer = (carry, item) => `${carry}[url=${item.url}]${item.title}[/url]\n`;
const markdownReducer = (carry, item) =>
`${carry}[${item.title}](${item.url})\n`;
const htmlReducer = (carry, item) =>
`${carry}<a href="${item.url}">${item.title}</a><br/>\n`;
const dokuwikiReducer = (carry, item) =>
`${carry}[[${item.url}|${item.title}]]\n`;
const phpbbReducer = (carry, item) =>
`${carry}[url=${item.url}]${item.title}[/url]\n`;
let downloadId = 0;

function notification(message) {
return browser.notifications
.create('download-notification', {
type: 'basic',
iconUrl: browser.extension.getURL('icons/linkdump-48.png'),
title: 'Linkdump',
message: browser.i18n.getMessage(message)
});
return browser.notifications.create('download-notification', {
type: 'basic',
iconUrl: browser.extension.getURL('icons/linkdump-48.png'),
title: 'Linkdump',
message: browser.i18n.getMessage(message)
});
}

async function updateBadge(items) {
Expand All @@ -28,9 +31,9 @@ async function updateBadge(items) {
count = '';
}

browser.browserAction.setBadgeText({text: count.toString()});
browser.browserAction.setBadgeTextColor({color: 'white'});
browser.browserAction.setBadgeBackgroundColor({color: '#007bc5'});
browser.browserAction.setBadgeText({ text: count.toString() });
browser.browserAction.setBadgeTextColor({ color: 'white' });
browser.browserAction.setBadgeBackgroundColor({ color: '#007bc5' });
}

async function addLink(link) {
Expand All @@ -48,9 +51,11 @@ async function addLink(link) {
urls.sort((a, b) => a.title.localeCompare(b.title));
}
if (obj.options.other.unique) {
urls = urls.filter((item, index, self) => self.findIndex(t => t.url === item.url) === index);
urls = urls.filter(
(item, index, self) => self.findIndex(t => t.url === item.url) === index
);
}
})
});

await browser.storage.local.set({ urls });
await updateBadge(urls.length);
Expand All @@ -60,9 +65,8 @@ function getLinks(bookmark, initialLinks = []) {
let links = [...initialLinks];

if (bookmark.url) {
links.push({ url: bookmark.url, title: bookmark.title});
}
else if (bookmark.children) {
links.push({ url: bookmark.url, title: bookmark.title });
} else if (bookmark.children) {
bookmark.children.forEach(child => {
links = getLinks(child, links);
});
Expand Down Expand Up @@ -107,7 +111,7 @@ function getDownloadOptions(format) {
reducer: textReducer,
filename: 'linkdump.txt',
type: 'text/plain'
}
};
}
}

Expand All @@ -120,7 +124,7 @@ function copy(downloadOptions) {
browser.runtime.sendMessage({
action: 'copy',
payload: content
})
});
});
}

Expand Down Expand Up @@ -148,7 +152,9 @@ async function deleteLink(link) {
if (!obj.urls) return;
let { urls } = obj;

urls = urls.filter((item) => item.url !== link.url && item.title !== link.title);
urls = urls.filter(
item => item.url !== link.url && item.title !== link.title
);

await browser.storage.local.set({ urls });
await updateBadge(urls.length);
Expand All @@ -164,12 +170,19 @@ function handleChanged(delta) {
return;
}
if (delta.state && delta.state.current === 'complete') {
browser.storage.local.get('options').then(async obj => {
if (obj.options !== undefined && obj.options.clear !== undefined && obj.options.clear.download) {
await browser.storage.local.remove('urls');
await updateBadge(0);
}
}).then(notification('notificationDownloadComplete'));
browser.storage.local
.get('options')
.then(async obj => {
if (
obj.options !== undefined &&
obj.options.clear !== undefined &&
obj.options.clear.download
) {
await browser.storage.local.remove('urls');
await updateBadge(0);
}
})
.then(notification('notificationDownloadComplete'));
}
}

Expand All @@ -186,12 +199,19 @@ function handleMessage(message) {
break;
}
case 'copied': {
browser.storage.local.get('options').then(async obj => {
if (obj.options !== undefined && obj.options.clear !== undefined && obj.options.clear.copy) {
await browser.storage.local.remove('urls');
await updateBadge(0);
}
}).then(notification('notificationStorageCopied'));
browser.storage.local
.get('options')
.then(async obj => {
if (
obj.options !== undefined &&
obj.options.clear !== undefined &&
obj.options.clear.copy
) {
await browser.storage.local.remove('urls');
await updateBadge(0);
}
})
.then(notification('notificationStorageCopied'));
break;
}
case 'delete':
Expand All @@ -201,7 +221,7 @@ function handleMessage(message) {
clear();
break;
default:
// Do nothing on purpose
// Do nothing on purpose
}
}

Expand All @@ -221,21 +241,21 @@ browser.menus.onClicked.addListener(info => {
switch (info.menuItemId) {
case linkAddId:
if (info.linkUrl !== undefined) {
addLink({ url: info.linkUrl, title: info.linkText});
addLink({ url: info.linkUrl, title: info.linkText });
} else {
addLink({ url: info.srcUrl, title: info.srcUrl});
addLink({ url: info.srcUrl, title: info.srcUrl });
}
break;
case bookmarkAddId:
addBookmark(info.bookmarkId);
break;
default:
// Do nothing on purpose
// Do nothing on purpose
}
});

browser.pageAction.onClicked.addListener(tab => {
addLink({ url: tab.url, title: tab.title});
addLink({ url: tab.url, title: tab.title });
});

browser.downloads.onChanged.addListener(handleChanged);
Expand Down
49 changes: 31 additions & 18 deletions popup/load.content.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function deleteItem({target}) {
const {href, text} = target.nextSibling;
function deleteItem({ target }) {
const { href, text } = target.nextSibling;
const container = target.parentNode;
const root = container.parentNode;

browser.runtime.sendMessage({
action: 'delete',
payload: {url: href, title: text}
payload: { url: href, title: text }
});

container.remove();
Expand All @@ -16,10 +16,13 @@ function deleteItem({target}) {

function translateContent() {
// Translate empty content
document.querySelector('#popup-content').dataset.empty = browser.i18n.getMessage('popupContentEmpty');
document.querySelector(
'#popup-content'
).dataset.empty = browser.i18n.getMessage('popupContentEmpty');
// Translate actions
document.querySelectorAll('[data-action]').forEach(item => {
const action = item.dataset.action[0].toUpperCase() + item.dataset.action.slice(1);
const action =
item.dataset.action[0].toUpperCase() + item.dataset.action.slice(1);
// eslint-disable-next-line no-param-reassign
item.title = browser.i18n.getMessage(`popupButtonAction${action}`);
});
Expand All @@ -37,21 +40,25 @@ function drawHiddenContent() {
}
Object.entries(formats).forEach(([format, value]) => {
if (!value) {
document.querySelector(`[data-action="${action}"][data-format="${format}"]`).parentNode.remove();
document
.querySelector(
`[data-action="${action}"][data-format="${format}"]`
)
.parentNode.remove();
}
});
});
}
document.querySelectorAll('nav ul').forEach(item => {
if (item.textContent.trim() === "") {
if (item.textContent.trim() === '') {
item.parentNode.remove();
}
});
});
}

function drawContentLinks(obj) {
obj.urls.forEach((item) => {
obj.urls.forEach(item => {
const listItem = document.createElement('p');
const itemLink = document.createElement('a');
itemLink.href = item.url;
Expand Down Expand Up @@ -85,19 +92,25 @@ function copyToClipboard(content) {
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
browser.runtime.sendMessage({
action: 'copied'
}).then(window.close());
browser.runtime
.sendMessage({
action: 'copied'
})
.then(window.close());
}

document.querySelector('[data-action="clear"]').addEventListener('click', () => {
browser.runtime.sendMessage({
action: 'clear'
}).then(window.close());
});
document
.querySelector('[data-action="clear"]')
.addEventListener('click', () => {
browser.runtime
.sendMessage({
action: 'clear'
})
.then(window.close());
});

document.querySelectorAll('[data-format]').forEach(item => {
item.addEventListener('click', ({target}) => {
item.addEventListener('click', ({ target }) => {
browser.runtime.sendMessage({
action: target.dataset.action,
payload: target.dataset.format
Expand All @@ -114,7 +127,7 @@ function handleMessage(message) {
copyToClipboard(message.payload);
break;
default:
// Do nothing on purpose
// Do nothing on purpose
}
}

Expand Down

0 comments on commit 0093d70

Please sign in to comment.