From cd5572bef7d854a63eb112e8494b6941db3882d9 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 18 Dec 2017 17:51:16 +0300 Subject: [PATCH] Added filtering on text Added extraction of links from selection Added message "No links" Updated locale ru Added filteringDomains (addNodes) --- _locales/en/messages.json | 5 +++++ _locales/ru/messages.json | 46 +++++++++++++++++++++++++++++++++++++++ browser/js/linkgopher.js | 32 +++++++++++++++++++++------ content-script.js | 17 +++++++++++++-- 4 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 _locales/ru/messages.json diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 26f1ecf..9ad3deb 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -9,6 +9,11 @@ "description": "none" }, + "nolinks": { + "message": "No links", + "description": "none" + }, + "links": { "message": "Links", "description": "none" diff --git a/_locales/ru/messages.json b/_locales/ru/messages.json new file mode 100644 index 0000000..ec1f957 --- /dev/null +++ b/_locales/ru/messages.json @@ -0,0 +1,46 @@ +{ + "askPattern": { + "message": "Введите строку символов для поиска в ссылке. Ссылки без этой строки будут игнорироваться.", + "description": "none" + }, + + "pleaseWait": { + "message": "Пожалуйста, подождите...", + "description": "none" + }, + + "nolinks": { + "message": "Нет ссылок", + "description": "none" + }, + + "links": { + "message": "Ссылки", + "description": "none" + }, + + "domains": { + "message": "Домены", + "description": "none" + }, + + "noMatches": { + "message": "Нет совпадений", + "description": "none" + }, + + "extractAll": { + "message": "Извлечение всех ссылок", + "description": "none" + }, + + "extractSome": { + "message": "Извлечение ссылок по фильтру", + "description": "none" + }, + + "aboutLinkGopher": { + "message": "О Link Gopher", + "description": "none" + } +} diff --git a/browser/js/linkgopher.js b/browser/js/linkgopher.js index 111f3ec..334b59c 100644 --- a/browser/js/linkgopher.js +++ b/browser/js/linkgopher.js @@ -33,14 +33,19 @@ chrome.tabs.sendMessage(tabId, {action: 'extract'}, links => { * @param {string} pattern -- Pattern for filtering. */ function handler(links, pattern) { + if (!links) { + return message.dataset.content = chrome.i18n.getMessage('noLinks'); + } + if (chrome.runtime.lastError) { return window.alert(chrome.runtime.lastError); } // To filter links like: javascript:void(0) - const resLinks = links.filter(link => link.lastIndexOf('://', 10) > 0); - // Remove duplicate, sorting of links. - const items = [...(new Set(resLinks))].sort(); + const resLinks = links.filter(link => link['href'].lastIndexOf('://', 10) > 0); + // Remove duplicate of links, sorting of links. + const items = [...(new Set(resLinks.filter((resLinks, index, self) => self.findIndex(link => link['href'] === resLinks['href']) === index)))] + .sort(function(a,b){ return a['href'].localeCompare(b['href']); }); const re = pattern ? new RegExp(pattern, 'g') : null; const added = items.filter(link => addNodes(link, containerLinks, re)); @@ -48,7 +53,7 @@ function handler(links, pattern) { return message.dataset.content = chrome.i18n.getMessage('noMatches'); } // Extract base URL from link, remove duplicate, sorting of domains. - const domains = [...(new Set(added.map(link => getBaseURL(link))))].sort(); + const domains = [...(new Set(added.map(link => getBaseURL(link['href']))))].sort(); const reDomains = filteringDomains ? re : null; domains.forEach(domain => addNodes(domain, containerDomains, reDomains)); }; @@ -63,12 +68,25 @@ function handler(links, pattern) { * @return {boolean} -- Whether link added into document. */ function addNodes(url, container, re) { - if (re && !url.match(re)) return false; + if (typeof(url) == "string") { + // filteringDomains + if (re && !url.match(re)) return false; + } else { + if (re && !(url['href'].match(re) || + url['text'].match(re))) return false; + } const br = document.createElement('br'); const a = document.createElement('a'); - a.href = url; - a.innerText = url; + if (typeof(url) == "string") { + // filteringDomains + a.href = url; + a.innerText = url; + } else { + a.href = url['href']; + a.innerText = url['href']; + a.title = url['text']; + } container.appendChild(a); container.appendChild(br); diff --git a/content-script.js b/content-script.js index 7d89306..5bb9a04 100644 --- a/content-script.js +++ b/content-script.js @@ -24,9 +24,22 @@ function onMessage(message, sender, sendResponse) { */ function extractLinks() { const links = []; + var doc_links; - for (let index = 0; index < document.links.length; index++) { - links.push(decodeURI(document.links[index].href)); + const sel = document.getSelection(); + + switch(sel.type) { + case 'Range': + doc_links = Array.from(document.links).filter(link => sel.containsNode(link, true)); + break; + default: + doc_links = document.links; + } + + for (let index = 0; index < doc_links.length; index++) { + links.push({'href': decodeURI(doc_links[index].href), + 'text': doc_links[index].textContent + }); } return links.length ? links : null;