Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find duplicates links in OneTab
(function findDuplicatesInOnetab() {
const isOneTab = document
.querySelector("title")
?.innerHTML.includes("OneTab"); // cheap OneTab check
if (!isOneTab) {
alert("Run this script in a OneTab tab");
return;
}
const getOneTabLinks = () => [
...document.querySelectorAll("#contentAreaDiv [href]"),
];
const getDuplicateLinks = (links) => {
let linkList = links || getOneTabLinks();
linkList = linkList.map((linkEl) => ({
link: linkEl.getAttribute("href"),
linkEl,
}));
const duplicateLinks = [];
const groupedDuplicates = [];
linkList.forEach(({ link, linkEl }, index) => {
const alreadyFound = duplicateLinks.includes(link);
if (alreadyFound) {
return;
}
const foundDuplicateEls = linkList
.filter(({ link: linkCompare }, indexCompare) => {
const alreadyFound = duplicateLinks.includes(linkCompare);
if (index === indexCompare || alreadyFound || link !== linkCompare) {
return false;
}
return true;
})
.map(({ linkEl }) => linkEl);
if (foundDuplicateEls.length) {
duplicateLinks.push(link);
groupedDuplicates.push({
link,
linkEls: [linkEl, ...foundDuplicateEls],
});
}
});
return groupedDuplicates;
};
const scrollToEl = (el) => {
el.scrollIntoView({ behavior: "smooth", block: "center" });
};
const markLink = (el, text) => {
el.style.backgroundColor = "#DE5918";
el.style.color = "#fff";
if (text) {
const duplicateMarker = document.createElement("span");
duplicateMarker.innerHTML = text;
duplicateMarker.classList.add("duplicate-marker");
duplicateMarker.style.marginRight = "20px";
el.insertAdjacentElement("afterbegin", duplicateMarker);
}
};
const unMarkLinks = (linkEls = getOneTabLinks()) => {
linkEls.forEach((el) => {
el.style.backgroundColor = undefined;
el.style.color = undefined;
const duplicateMarker = el.querySelector(".duplicate-marker");
if (duplicateMarker) duplicateMarker.remove();
});
};
const findDuplicates = (links) => {
unMarkLinks(links);
const groupedDuplicates = getDuplicateLinks(links);
if (!groupedDuplicates.length) {
alert("No Duplicates Found");
} else {
groupedDuplicates.forEach((duplicate, groupIndex) =>
duplicate.linkEls.forEach((el, duplicateIndex) =>
markLink(
el,
`${groupIndex + 1}/${groupedDuplicates.length} | ${
duplicateIndex + 1
}/${duplicate.linkEls.length}`
)
)
);
alert(`${groupedDuplicates.length} Duplicates Found`);
scrollToEl(groupedDuplicates[0].linkEls[0]);
}
};
findDuplicates(getOneTabLinks());
})();
@dpschen

This comment has been minimized.

Copy link
Owner Author

@dpschen dpschen commented Apr 13, 2020

Open Browser Console in OneTab paste script above.
After that run findDuplicatesInOnetab() in a new line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment