Skip to content

Instantly share code, notes, and snippets.

@joaofnds
Last active August 28, 2019 12:55
Show Gist options
  • Select an option

  • Save joaofnds/3b895d8998d4b421d5d4fb84b8a31be3 to your computer and use it in GitHub Desktop.

Select an option

Save joaofnds/3b895d8998d4b421d5d4fb84b8a31be3 to your computer and use it in GitHub Desktop.
Small tool that helps to find out duplicates in a bookmark export file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="file">
<table>
<thead>
<tr>
<th>name</th>
<th>link</th>
<th>#matches</th>
</tr>
</thead>
<tbody id="tbody"></tbody>
</table>
<script>
const input = document.querySelector('input');
const tbody = document.querySelector('#tbody');
input.addEventListener('change', () => {
const reader = new FileReader();
reader.onerror = console.error;
reader.onload =
({ target: { result } }) =>
parseAndPresent(result);
reader.readAsText(input.files[0], "UTF-8");
});
const parseAndPresent = result =>
Object.entries(groupMatches(
Array.from(stringToHTML(result).querySelectorAll('a'))
))
.filter(([_, { matches }]) => matches > 1)
.map(([_, v]) => createRow(v))
.forEach(row => tbody.appendChild(row));
const groupMatches =
anchorsTags =>
anchorsTags
.map(anchorToLinkObject)
.reduce((acc, linkObject) => {
if (acc.hasOwnProperty(linkObject.link)) {
acc[linkObject.link].matches++
} else {
acc[linkObject.link] = linkObject
}
return acc;
}, {})
const anchorToLinkObject = a => ({ link: a.href, name: a.innerText, matches: 1 })
const createRow =
({ name, link, matches }, elType = 'tr') => {
const el = document.createElement(elType)
el.innerHTML = `
<td>${name}</td>
<td><a href="${link}">${link}</a></td>
<td>${matches}</td>
`
return el
}
const stringToHTML =
str =>
(new DOMParser()).parseFromString(str, 'text/html')
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment