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

Patch 3 #15

Open
wants to merge 2 commits 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
5 changes: 5 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"description": "none"
},

"nolinks": {
"message": "No links",
"description": "none"
},

"links": {
"message": "Links",
"description": "none"
Expand Down
5 changes: 5 additions & 0 deletions _locales/ru/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"description": "none"
},

"nolinks": {
"message": "Нет ссылок",
"description": "none"
},

"links": {
"message": "Ссылки",
"description": "none"
Expand Down
32 changes: 25 additions & 7 deletions browser/js/linkgopher.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,27 @@ 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));

if (!added.length) {
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));
};
Expand All @@ -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);

Expand Down
17 changes: 15 additions & 2 deletions content-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down