2019-06-29 19:54:25 +02:00
|
|
|
var browser: Browser = browser || chrome;
|
2018-09-16 12:27:42 +02:00
|
|
|
|
|
|
|
var hostname = typeof (location) != 'undefined' ? location.hostname : '';
|
|
|
|
if (hostname.startsWith('www.')) {
|
|
|
|
hostname = hostname.substring(4);
|
|
|
|
}
|
|
|
|
if (hostname.endsWith('.reddit.com')) hostname = 'reddit.com';
|
2019-02-25 23:44:34 +01:00
|
|
|
if (hostname.endsWith('.facebook.com')) hostname = 'facebook.com';
|
2019-03-03 21:27:45 +01:00
|
|
|
if (hostname.endsWith('.youtube.com')) hostname = 'youtube.com';
|
2018-09-16 12:27:42 +02:00
|
|
|
|
2019-06-29 19:54:25 +02:00
|
|
|
var myself: string = null;
|
2018-11-16 21:51:31 +01:00
|
|
|
|
2018-12-16 17:36:19 +01:00
|
|
|
function fixupSiteStyles() {
|
2019-06-29 19:47:05 +02:00
|
|
|
if (hostname == 'facebook.com') {
|
|
|
|
let m = document.querySelector("[id^='profile_pic_header_']")
|
|
|
|
if (m) myself = 'facebook.com/' + captureRegex(m.id, /header_(\d+)/);
|
|
|
|
} else if (hostname == 'medium.com') {
|
|
|
|
addStyleSheet(`
|
|
|
|
a.show-thread-link, a.ThreadedConversation-moreRepliesLink {
|
|
|
|
color: inherit !important;
|
|
|
|
}
|
|
|
|
.fullname,
|
|
|
|
.stream-item a:hover .fullname,
|
|
|
|
.stream-item a:active .fullname
|
|
|
|
{color:inherit;}
|
|
|
|
`);
|
|
|
|
} else if (domainIs(hostname, 'tumblr.com')) {
|
|
|
|
addStyleSheet(`
|
|
|
|
.assigned-label-transphobic { outline: 2px solid #991515 !important; }
|
|
|
|
.assigned-label-t-friendly { outline: 1px solid #77B91E !important; }
|
|
|
|
`);
|
2019-06-29 19:54:25 +02:00
|
|
|
} else if (hostname.indexOf('wiki') != -1) {
|
2019-06-29 19:47:05 +02:00
|
|
|
addStyleSheet(`
|
|
|
|
.assigned-label-transphobic { outline: 1px solid #991515 !important; }
|
|
|
|
.assigned-label-t-friendly { outline: 1px solid #77B91E !important; }
|
|
|
|
`);
|
|
|
|
} else if (hostname == 'twitter.com') {
|
|
|
|
myself = getIdentifier(<HTMLAnchorElement>document.querySelector('.DashUserDropdown-userInfo a'));
|
|
|
|
addStyleSheet(`
|
|
|
|
.pretty-link b, .pretty-link s {
|
|
|
|
color: inherit !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
a.show-thread-link, a.ThreadedConversation-moreRepliesLink {
|
|
|
|
color: inherit !important;
|
|
|
|
}
|
|
|
|
.fullname,
|
|
|
|
.stream-item a:hover .fullname,
|
|
|
|
.stream-item a:active .fullname
|
|
|
|
{color:inherit;}
|
|
|
|
`);
|
|
|
|
} else if (hostname == 'reddit.com') {
|
|
|
|
myself = getIdentifier(<HTMLAnchorElement>document.querySelector('#header-bottom-right .user a'));
|
2018-11-16 21:51:31 +01:00
|
|
|
if (!myself) {
|
2019-06-29 19:47:05 +02:00
|
|
|
let m = document.querySelector('#USER_DROPDOWN_ID');
|
2018-11-16 21:51:31 +01:00
|
|
|
if (m) {
|
2019-06-29 19:47:05 +02:00
|
|
|
let username = [...m.querySelectorAll('*')].filter(x => x.childNodes.length == 1 && x.firstChild.nodeType == 3).map(x => x.textContent)[0]
|
|
|
|
if (username) myself = 'reddit.com/user/' + username;
|
2018-11-16 21:51:31 +01:00
|
|
|
}
|
2018-09-16 12:27:42 +02:00
|
|
|
}
|
2019-06-29 19:47:05 +02:00
|
|
|
addStyleSheet(`
|
|
|
|
.author { color: #369 !important;}
|
|
|
|
`);
|
2018-09-16 12:27:42 +02:00
|
|
|
}
|
2019-06-29 19:47:05 +02:00
|
|
|
}
|
2018-11-16 21:51:31 +01:00
|
|
|
|
2019-06-29 19:54:25 +02:00
|
|
|
function addStyleSheet(css: string) {
|
2019-06-29 19:47:05 +02:00
|
|
|
const style = document.createElement('style');
|
|
|
|
style.textContent = css;
|
|
|
|
document.head.appendChild(style);
|
2018-10-30 18:10:57 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 17:36:19 +01:00
|
|
|
function maybeDisableCustomCss() {
|
2019-06-29 19:54:25 +02:00
|
|
|
var shouldDisable: (s: { ownerNode: HTMLElement }) => boolean = null;
|
2018-12-16 17:36:19 +01:00
|
|
|
if (hostname == 'twitter.com') shouldDisable = x => x.ownerNode && x.ownerNode.id && x.ownerNode.id.startsWith('user-style');
|
|
|
|
else if (hostname == 'medium.com') shouldDisable = x => x.ownerNode && x.ownerNode.className && x.ownerNode.className == 'js-collectionStyle';
|
|
|
|
else if (hostname == 'disqus.com') shouldDisable = x => x.ownerNode && x.ownerNode.id && x.ownerNode.id.startsWith('css_');
|
2018-09-16 12:27:42 +02:00
|
|
|
|
2018-12-16 17:36:19 +01:00
|
|
|
if (shouldDisable)
|
2019-06-29 19:47:05 +02:00
|
|
|
[...document.styleSheets].filter(<any>shouldDisable).forEach(x => x.disabled = true);
|
2018-11-16 20:35:31 +01:00
|
|
|
}
|
|
|
|
|
2018-09-16 12:27:42 +02:00
|
|
|
function init() {
|
2018-11-16 21:51:31 +01:00
|
|
|
fixupSiteStyles();
|
2018-12-16 17:36:19 +01:00
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
if (domainIs(hostname, 'youtube.com')) {
|
2018-11-16 21:51:31 +01:00
|
|
|
setInterval(updateYouTubeChannelHeader, 300);
|
2019-05-12 16:07:51 +02:00
|
|
|
setInterval(updateAllLabels, 6000);
|
2018-11-16 21:51:31 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 16:54:01 +01:00
|
|
|
console.log('Self: ' + myself)
|
2018-11-16 21:51:31 +01:00
|
|
|
|
|
|
|
maybeDisableCustomCss();
|
2018-09-16 12:27:42 +02:00
|
|
|
updateAllLabels();
|
2018-12-16 17:36:19 +01:00
|
|
|
|
2018-09-16 12:27:42 +02:00
|
|
|
var observer = new MutationObserver(mutationsList => {
|
2018-11-16 21:51:31 +01:00
|
|
|
maybeDisableCustomCss();
|
2019-06-29 15:41:37 +02:00
|
|
|
for (const mutation of mutationsList) {
|
2018-09-16 12:27:42 +02:00
|
|
|
if (mutation.type == 'childList') {
|
2019-06-29 15:41:37 +02:00
|
|
|
for (const node of mutation.addedNodes) {
|
|
|
|
if (node instanceof HTMLAnchorElement) {
|
2018-09-16 12:27:42 +02:00
|
|
|
initLink(node);
|
|
|
|
}
|
2019-06-29 15:41:37 +02:00
|
|
|
if (node instanceof HTMLElement) {
|
|
|
|
for (const subnode of node.querySelectorAll('a')) {
|
2018-09-16 12:27:42 +02:00
|
|
|
initLink(subnode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
solvePendingLabels();
|
|
|
|
});
|
|
|
|
|
|
|
|
observer.observe(document.body, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
|
|
|
|
2018-12-16 16:42:20 +01:00
|
|
|
document.addEventListener('contextmenu', evt => {
|
2019-06-29 19:47:05 +02:00
|
|
|
lastRightClickedElement = <HTMLElement>evt.target;
|
2018-12-16 16:42:20 +01:00
|
|
|
}, true);
|
2018-09-16 12:27:42 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 19:54:25 +02:00
|
|
|
var lastRightClickedElement: HTMLElement = null;
|
|
|
|
var lastAppliedYouTubeUrl: string = null;
|
|
|
|
var lastAppliedYouTubeTitle: string = null;
|
2018-10-30 18:10:57 +01:00
|
|
|
|
2018-12-16 17:36:19 +01:00
|
|
|
function updateYouTubeChannelHeader() {
|
2018-10-30 18:10:57 +01:00
|
|
|
var url = window.location.href;
|
|
|
|
var title = document.getElementById('channel-title');
|
2018-12-16 17:36:19 +01:00
|
|
|
if (title && title.tagName == 'H3') title = null; // search results, already a link
|
2018-10-30 18:10:57 +01:00
|
|
|
var currentTitle = title ? title.textContent : null;
|
|
|
|
|
2018-12-16 17:36:19 +01:00
|
|
|
if (url == lastAppliedYouTubeUrl && currentTitle == lastAppliedYouTubeTitle) return;
|
2018-10-30 18:10:57 +01:00
|
|
|
lastAppliedYouTubeUrl = url;
|
|
|
|
lastAppliedYouTubeTitle = currentTitle;
|
2018-12-16 17:36:19 +01:00
|
|
|
|
|
|
|
if (currentTitle) {
|
2019-06-29 15:41:37 +02:00
|
|
|
var replacement = <HTMLAnchorElement>document.getElementById('channel-title-replacement');
|
2018-12-16 17:36:19 +01:00
|
|
|
if (!replacement) {
|
2019-06-29 15:41:37 +02:00
|
|
|
replacement = <HTMLAnchorElement>document.createElement('A');
|
2018-10-30 18:10:57 +01:00
|
|
|
replacement.id = 'channel-title-replacement'
|
|
|
|
replacement.className = title.className;
|
|
|
|
title.parentNode.insertBefore(replacement, title.nextSibling);
|
|
|
|
title.style.display = 'none';
|
|
|
|
replacement.style.fontSize = '2.4rem';
|
|
|
|
replacement.style.fontWeight = '400';
|
|
|
|
replacement.style.lineHeight = '3rem';
|
|
|
|
replacement.style.textDecoration = 'none';
|
2019-04-30 21:59:12 +02:00
|
|
|
replacement.style.color = 'black';
|
2018-10-30 18:10:57 +01:00
|
|
|
}
|
|
|
|
replacement.textContent = lastAppliedYouTubeTitle;
|
|
|
|
replacement.href = lastAppliedYouTubeUrl;
|
|
|
|
}
|
|
|
|
updateAllLabels();
|
2019-03-03 21:27:45 +01:00
|
|
|
setTimeout(updateAllLabels, 2000);
|
2019-05-12 16:07:51 +02:00
|
|
|
setTimeout(updateAllLabels, 4000);
|
2018-10-30 18:10:57 +01:00
|
|
|
}
|
|
|
|
|
2019-06-29 15:41:37 +02:00
|
|
|
function updateAllLabels(refresh?: boolean) {
|
2018-09-16 12:27:42 +02:00
|
|
|
if (refresh) knownLabels = {};
|
2019-06-29 19:47:05 +02:00
|
|
|
for (const a of document.getElementsByTagName('a')) {
|
2018-09-16 12:27:42 +02:00
|
|
|
initLink(a);
|
|
|
|
}
|
|
|
|
solvePendingLabels();
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
var knownLabels: LabelMap = {};
|
2018-09-16 12:27:42 +02:00
|
|
|
|
2019-06-29 19:54:25 +02:00
|
|
|
var labelsToSolve: LabelToSolve[] = [];
|
2018-09-16 12:27:42 +02:00
|
|
|
function solvePendingLabels() {
|
|
|
|
if (!labelsToSolve.length) return;
|
|
|
|
var uniqueIdentifiers = Array.from(new Set(labelsToSolve.map(x => x.identifier)));
|
|
|
|
var tosolve = labelsToSolve;
|
|
|
|
labelsToSolve = [];
|
2019-06-29 19:47:05 +02:00
|
|
|
browser.runtime.sendMessage<ShinigamiEyesCommand, LabelMap>({ ids: uniqueIdentifiers, myself: <string>myself }, (response: LabelMap) => {
|
2019-06-29 15:41:37 +02:00
|
|
|
for (const item of tosolve) {
|
2018-09-16 12:27:42 +02:00
|
|
|
var label = response[item.identifier];
|
|
|
|
knownLabels[item.identifier] = label || '';
|
|
|
|
applyLabel(item.element, item.identifier);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
function applyLabel(a: HTMLAnchorElement, identifier: string) {
|
2018-09-16 12:27:42 +02:00
|
|
|
if (a.assignedCssLabel) {
|
|
|
|
a.classList.remove('assigned-label-' + a.assignedCssLabel);
|
|
|
|
a.classList.remove('has-assigned-label');
|
|
|
|
}
|
2018-12-03 22:19:33 +01:00
|
|
|
|
|
|
|
a.assignedCssLabel = knownLabels[identifier] || '';
|
|
|
|
|
2018-09-16 12:27:42 +02:00
|
|
|
if (a.assignedCssLabel) {
|
|
|
|
a.classList.add('assigned-label-' + a.assignedCssLabel);
|
|
|
|
a.classList.add('has-assigned-label');
|
2018-12-03 22:19:54 +01:00
|
|
|
if (hostname == 'twitter.com')
|
|
|
|
a.classList.remove('u-textInheritColor');
|
2018-09-16 12:27:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-29 15:41:37 +02:00
|
|
|
function initLink(a: HTMLAnchorElement) {
|
2018-09-16 12:27:42 +02:00
|
|
|
var identifier = getIdentifier(a);
|
2019-06-29 19:54:25 +02:00
|
|
|
if (!identifier) {
|
|
|
|
if (hostname == 'youtube.com')
|
2019-03-03 21:27:45 +01:00
|
|
|
applyLabel(a, '');
|
|
|
|
return;
|
|
|
|
}
|
2018-09-16 12:27:42 +02:00
|
|
|
|
|
|
|
var label = knownLabels[identifier];
|
|
|
|
if (label === undefined) {
|
|
|
|
labelsToSolve.push({ element: a, identifier: identifier });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
applyLabel(a, identifier);
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
function domainIs(host: string, baseDomain: string) {
|
|
|
|
if (baseDomain.length > host.length) return false;
|
|
|
|
if (baseDomain.length == host.length) return baseDomain == host;
|
|
|
|
var k = host.charCodeAt(host.length - baseDomain.length - 1);
|
|
|
|
if (k == 0x2E /* . */) return host.endsWith(baseDomain);
|
2018-09-16 12:27:42 +02:00
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
function getPartialPath(path: string, num: number) {
|
2018-09-16 12:27:42 +02:00
|
|
|
var m = path.split('/')
|
|
|
|
m = m.slice(1, 1 + num);
|
|
|
|
if (m.length && !m[m.length - 1]) m.length--;
|
2019-01-13 17:17:01 +01:00
|
|
|
if (m.length != num) return '!!'
|
2018-09-16 12:27:42 +02:00
|
|
|
return '/' + m.join('/');
|
|
|
|
}
|
2019-06-29 19:47:05 +02:00
|
|
|
function getPathPart(path: string, index: number) {
|
|
|
|
return path.split('/')[index + 1] || null;
|
2018-12-16 11:32:37 +01:00
|
|
|
}
|
2018-09-16 12:27:42 +02:00
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
function captureRegex(str: string, regex: RegExp) {
|
2018-12-16 17:36:19 +01:00
|
|
|
if (!str) return null;
|
2018-10-22 18:56:44 +02:00
|
|
|
var match = str.match(regex);
|
2018-12-16 17:36:19 +01:00
|
|
|
if (match && match[1]) return match[1];
|
2018-10-22 18:56:44 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-16 12:27:42 +02:00
|
|
|
function getCurrentFacebookPageId() {
|
|
|
|
|
|
|
|
// page
|
2019-06-29 15:41:37 +02:00
|
|
|
var elem = <HTMLAnchorElement>document.querySelector("a[rel=theater][aria-label='Profile picture']");
|
2018-09-16 12:27:42 +02:00
|
|
|
if (elem) {
|
2018-10-22 18:56:44 +02:00
|
|
|
var p = captureRegex(elem.href, /facebook\.com\/(\d+)/)
|
|
|
|
if (p) return p;
|
2018-09-16 12:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// page (does not work if page is loaded directly)
|
|
|
|
elem = document.querySelector("[ajaxify^='/page_likers_and_visitors_dialog']")
|
2018-10-22 18:56:44 +02:00
|
|
|
if (elem) return captureRegex(elem.getAttribute('ajaxify'), /\/(\d+)\//);
|
2018-09-16 12:27:42 +02:00
|
|
|
|
|
|
|
// group
|
|
|
|
elem = document.querySelector("[id^='headerAction_']");
|
2018-10-22 18:56:44 +02:00
|
|
|
if (elem) return captureRegex(elem.id, /_(\d+)/);
|
2018-09-16 12:27:42 +02:00
|
|
|
|
|
|
|
// profile
|
|
|
|
elem = document.querySelector('#pagelet_timeline_main_column');
|
|
|
|
if (elem && elem.dataset.gt) return JSON.parse(elem.dataset.gt).profile_owner;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
function getIdentifier(link: string | HTMLAnchorElement) {
|
2018-12-16 17:36:19 +01:00
|
|
|
try {
|
2019-06-29 19:47:05 +02:00
|
|
|
var k = link instanceof Node ? getIdentifierFromElementImpl(link) : getIdentifierFromURLImpl(tryParseURL(link));
|
2019-02-08 20:43:06 +01:00
|
|
|
if (!k || k.indexOf('!') != -1) return null;
|
2019-01-14 23:11:06 +01:00
|
|
|
return k.toLowerCase();
|
2018-12-16 17:36:19 +01:00
|
|
|
} catch (e) {
|
2019-06-29 19:47:05 +02:00
|
|
|
console.warn("Unable to get identifier for " + link);
|
2018-10-22 18:56:44 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
function getIdentifierFromElementImpl(element: HTMLAnchorElement): string {
|
|
|
|
if (!element) return null;
|
2018-09-16 12:27:42 +02:00
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
const dataset = element.dataset;
|
2019-02-08 21:00:04 +01:00
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
if (hostname == 'reddit.com') {
|
|
|
|
const parent = element.parentElement;
|
|
|
|
if (parent && parent.classList.contains('domain') && element.textContent.startsWith('self.')) return null;
|
|
|
|
} else if (hostname == 'disqus.com') {
|
|
|
|
if (element.classList && element.classList.contains('time-ago')) return null;
|
|
|
|
} else if (hostname == 'facebook.com') {
|
|
|
|
const parent = element.parentElement;
|
2018-09-16 12:27:42 +02:00
|
|
|
if (parent && (parent.tagName == 'H1' || parent.id == 'fb-timeline-cover-name')) {
|
2019-06-29 15:41:37 +02:00
|
|
|
const id = getCurrentFacebookPageId();
|
2018-09-16 12:27:42 +02:00
|
|
|
//console.log('Current fb page: ' + id)
|
|
|
|
if (id)
|
|
|
|
return 'facebook.com/' + id;
|
|
|
|
}
|
2019-03-01 00:04:11 +01:00
|
|
|
|
|
|
|
// comment timestamp
|
2019-06-29 19:47:05 +02:00
|
|
|
if (element.firstChild && (<HTMLElement>element.firstChild).tagName == 'ABBR' && element.lastChild == element.firstChild) return null;
|
2019-06-29 19:54:25 +02:00
|
|
|
|
2019-03-01 00:04:11 +01:00
|
|
|
// post 'see more'
|
2019-06-29 19:47:05 +02:00
|
|
|
if (element.classList.contains('see_more_link')) return null;
|
2019-03-01 00:04:11 +01:00
|
|
|
|
|
|
|
// post 'continue reading'
|
|
|
|
if (parent && parent.classList.contains('text_exposed_link')) return null;
|
|
|
|
|
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
if (dataset) {
|
|
|
|
const hovercard = dataset.hovercard;
|
2018-09-16 12:27:42 +02:00
|
|
|
if (hovercard) {
|
2019-06-29 15:41:37 +02:00
|
|
|
const id = captureRegex(hovercard, /id=(\d+)/);
|
2018-09-16 12:27:42 +02:00
|
|
|
if (id)
|
|
|
|
return 'facebook.com/' + id;
|
|
|
|
}
|
2019-03-01 00:04:11 +01:00
|
|
|
|
|
|
|
// post Comments link
|
2019-06-29 19:47:05 +02:00
|
|
|
if (dataset.testid == 'UFI2CommentsCount/root') return null;
|
2019-06-29 19:54:25 +02:00
|
|
|
|
2019-03-01 00:04:11 +01:00
|
|
|
// post Comments link
|
2019-06-29 19:47:05 +02:00
|
|
|
if (dataset.commentPreludeRef) return null;
|
2019-03-01 00:04:11 +01:00
|
|
|
|
|
|
|
// page left sidebar
|
2019-06-29 19:47:05 +02:00
|
|
|
if (dataset.endpoint) return null;
|
2019-03-01 00:04:11 +01:00
|
|
|
|
2019-03-03 21:02:57 +01:00
|
|
|
// profile tabs
|
2019-06-29 19:47:05 +02:00
|
|
|
if (dataset.tabKey) return null;
|
2019-03-03 21:02:57 +01:00
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
const gt = dataset.gt;
|
2018-09-16 12:27:42 +02:00
|
|
|
if (gt) {
|
2019-06-29 15:41:37 +02:00
|
|
|
const gtParsed = JSON.parse(gt);
|
2018-09-16 12:27:42 +02:00
|
|
|
if (gtParsed.engagement && gtParsed.engagement.eng_tid) {
|
|
|
|
return 'facebook.com/' + gtParsed.engagement.eng_tid;
|
|
|
|
}
|
|
|
|
}
|
2019-03-01 00:04:11 +01:00
|
|
|
|
|
|
|
// comment interaction buttons
|
2019-06-29 19:47:05 +02:00
|
|
|
if (dataset.sigil) return null;
|
2019-03-01 00:04:11 +01:00
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
let p = <HTMLElement>element;
|
2018-09-16 12:27:42 +02:00
|
|
|
while (p) {
|
2019-06-29 15:41:37 +02:00
|
|
|
const bt = p.dataset.bt;
|
2018-09-16 12:27:42 +02:00
|
|
|
if (bt) {
|
2019-06-29 15:41:37 +02:00
|
|
|
const btParsed = JSON.parse(bt);
|
2018-09-16 12:27:42 +02:00
|
|
|
if (btParsed.id) return 'facebook.com/' + btParsed.id;
|
|
|
|
}
|
|
|
|
p = p.parentElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-29 19:47:05 +02:00
|
|
|
if (dataset && dataset.expandedUrl) return getIdentifierFromURLImpl(tryParseURL(dataset.expandedUrl));
|
|
|
|
const href = element.href;
|
2019-06-29 19:54:25 +02:00
|
|
|
if (href && !href.endsWith('#')) return getIdentifierFromURLImpl(tryParseURL(href));
|
2019-06-29 19:47:05 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:54:25 +02:00
|
|
|
function tryParseURL(urlstr: string) {
|
2018-09-16 12:27:42 +02:00
|
|
|
if (!urlstr) return null;
|
|
|
|
try {
|
2019-06-29 19:47:05 +02:00
|
|
|
const url = new URL(urlstr);
|
|
|
|
if (url.protocol != 'http:' && url.protocol != 'https:') return null;
|
|
|
|
return url;
|
2019-06-29 19:54:25 +02:00
|
|
|
} catch (e) {
|
2018-09-16 12:27:42 +02:00
|
|
|
return null;
|
|
|
|
}
|
2019-06-29 19:47:05 +02:00
|
|
|
}
|
2018-09-16 12:27:42 +02:00
|
|
|
|
2019-06-29 19:54:25 +02:00
|
|
|
function getIdentifierFromURLImpl(url: URL): string {
|
|
|
|
if (!url) return null;
|
2019-03-01 00:04:11 +01:00
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
// nested urls
|
2018-09-16 12:27:42 +02:00
|
|
|
if (url.href.indexOf('http', 1) != -1) {
|
2019-06-29 19:47:05 +02:00
|
|
|
if (url.pathname.startsWith('/intl/')) return null; // facebook language switch links
|
|
|
|
|
|
|
|
// const values = url.searchParams.values()
|
|
|
|
// HACK: values(...) is not iterable on facebook (babel polyfill?)
|
|
|
|
const values = url.search.split('&').map(x => {
|
|
|
|
const eq = x.indexOf('=');
|
|
|
|
return eq == -1 ? '' : decodeURIComponent(x.substr(eq + 1));
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const value of values) {
|
|
|
|
if (value.startsWith('http:') || value.startsWith('https:')) {
|
|
|
|
return getIdentifierFromURLImpl(tryParseURL(value));
|
2018-09-16 12:27:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-29 19:47:05 +02:00
|
|
|
const newurl = tryParseURL(url.href.substring(url.href.indexOf('http', 1)));
|
2019-06-29 19:54:25 +02:00
|
|
|
if (newurl) return getIdentifierFromURLImpl(newurl);
|
2018-09-16 12:27:42 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
// fb group member badge
|
|
|
|
if (url.pathname.includes('/badge_member_list/')) return null;
|
|
|
|
|
2019-06-29 15:41:37 +02:00
|
|
|
let host = url.hostname;
|
2019-06-29 19:47:05 +02:00
|
|
|
if (domainIs(host, 'web.archive.org')) {
|
2019-06-29 15:41:37 +02:00
|
|
|
const match = captureRegex(url.href, /\/web\/\w+\/(.*)/);
|
2018-09-16 12:27:42 +02:00
|
|
|
if (!match) return null;
|
2019-06-29 19:47:05 +02:00
|
|
|
return getIdentifierFromURLImpl(tryParseURL('http://' + match));
|
2018-09-16 12:27:42 +02:00
|
|
|
}
|
2019-06-29 19:47:05 +02:00
|
|
|
|
2018-09-16 12:27:42 +02:00
|
|
|
if (host.startsWith('www.')) host = host.substring(4);
|
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
if (domainIs(host, 'facebook.com')) {
|
|
|
|
const fbId = url.searchParams.get('id');
|
2019-06-29 15:41:37 +02:00
|
|
|
const p = url.pathname.replace('/pg/', '/');
|
2019-06-29 19:47:05 +02:00
|
|
|
return 'facebook.com/' + (fbId || getPartialPath(p, p.startsWith('/groups/') ? 2 : 1).substring(1));
|
|
|
|
} else if (domainIs(host, 'reddit.com')) {
|
2019-06-29 15:41:37 +02:00
|
|
|
const pathname = url.pathname.replace('/u/', '/user/');
|
2019-01-13 18:25:47 +01:00
|
|
|
if (!pathname.startsWith('/user/') && !pathname.startsWith('/r/')) return null;
|
2019-06-29 19:54:25 +02:00
|
|
|
if (pathname.includes('/comments/') && hostname == 'reddit.com') return null;
|
2019-06-29 19:47:05 +02:00
|
|
|
return 'reddit.com' + getPartialPath(pathname, 2);
|
|
|
|
} else if (domainIs(host, 'twitter.com')) {
|
|
|
|
return 'twitter.com' + getPartialPath(url.pathname, 1);
|
|
|
|
} else if (domainIs(host, 'youtube.com')) {
|
2019-06-29 15:41:37 +02:00
|
|
|
const pathname = url.pathname.replace('/c/', '/user/');
|
2019-01-13 18:25:47 +01:00
|
|
|
if (!pathname.startsWith('/user/') && !pathname.startsWith('/channel/')) return null;
|
2019-06-29 19:47:05 +02:00
|
|
|
return 'youtube.com' + getPartialPath(pathname, 2);
|
|
|
|
} else if (domainIs(host, 'disqus.com') && url.pathname.startsWith('/by/')) {
|
|
|
|
return 'disqus.com' + getPartialPath(url.pathname, 2);
|
|
|
|
} else if (domainIs(host, 'medium.com')) {
|
|
|
|
return 'medium.com' + getPartialPath(url.pathname.replace('/t/', '/'), 1);
|
|
|
|
} else if (domainIs(host, 'tumblr.com')) {
|
2018-12-16 17:36:19 +01:00
|
|
|
if (url.pathname.startsWith('/register/follow/')) {
|
2019-06-29 19:47:05 +02:00
|
|
|
const name = getPathPart(url.pathname, 2);
|
2018-12-16 11:32:37 +01:00
|
|
|
return name ? name + '.tumblr.com' : null;
|
|
|
|
}
|
2018-12-16 17:36:19 +01:00
|
|
|
if (host != 'www.tumblr.com' && host != 'assets.tumblr.com' && host.indexOf('.media.') == -1) {
|
2019-01-14 23:11:06 +01:00
|
|
|
if (!url.pathname.startsWith('/tagged/')) return url.host;
|
2018-12-16 11:32:37 +01:00
|
|
|
}
|
|
|
|
return null;
|
2019-06-29 19:47:05 +02:00
|
|
|
} else if (domainIs(host, 'wikipedia.org') || domainIs(host, 'rationalwiki.org')) {
|
2019-01-13 18:25:47 +01:00
|
|
|
if (url.hash || url.pathname.includes(':')) return null;
|
2019-06-29 19:47:05 +02:00
|
|
|
if (url.pathname.startsWith('/wiki/')) return 'wikipedia.org' + getPartialPath(url.pathname, 2);
|
2019-01-13 17:16:37 +01:00
|
|
|
else return null;
|
2019-06-29 19:47:05 +02:00
|
|
|
} else if (host.indexOf('.blogspot.') != -1) {
|
2019-06-29 15:41:37 +02:00
|
|
|
const m = captureRegex(host, /([a-zA-Z0-9\-]*)\.blogspot/);
|
2019-01-14 23:11:06 +01:00
|
|
|
if (m) return m + '.blogspot.com';
|
2019-06-29 19:47:05 +02:00
|
|
|
else return null;
|
|
|
|
} else {
|
|
|
|
if (host.startsWith('m.')) host = host.substr(2);
|
|
|
|
return host;
|
2018-09-16 12:27:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
2018-12-17 21:56:30 +01:00
|
|
|
var lastGeneratedLinkId = 0;
|
|
|
|
|
2019-06-29 19:54:25 +02:00
|
|
|
function getSnippet(node: HTMLElement) {
|
2019-03-03 21:02:57 +01:00
|
|
|
while (node) {
|
|
|
|
var classList = node.classList;
|
|
|
|
if (hostname == 'facebook.com' && node.dataset && node.dataset.ftr) return node;
|
|
|
|
if (hostname == 'reddit.com' && (classList.contains('scrollerItem') || classList.contains('thing') || classList.contains('Comment'))) return node;
|
|
|
|
if (hostname == 'twitter.com' && (classList.contains('stream-item'))) return node;
|
|
|
|
if (hostname == 'disqus.com' && (classList.contains('post-content'))) return node;
|
|
|
|
if (hostname == 'medium.com' && (classList.contains('streamItem') || classList.contains('streamItemConversationItem'))) return node;
|
|
|
|
if (hostname == 'youtube.com' && node.tagName == 'YTD-COMMENT-RENDERER') return node;
|
|
|
|
if (hostname.endsWith('tumblr.com') && (node.dataset.postId || classList.contains('post'))) return node;
|
|
|
|
|
|
|
|
node = node.parentElement;
|
|
|
|
}
|
|
|
|
return null;
|
2019-03-03 21:02:40 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 16:42:20 +01:00
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
browser.runtime.onMessage.addListener<ShinigamiEyesMessage, ShinigamiEyesSubmission>((message, sender, sendResponse) => {
|
2019-03-03 21:02:40 +01:00
|
|
|
|
|
|
|
if (message.updateAllLabels) {
|
|
|
|
updateAllLabels(true);
|
|
|
|
return;
|
|
|
|
}
|
2019-06-29 19:54:25 +02:00
|
|
|
|
2019-03-03 21:02:40 +01:00
|
|
|
message.contextPage = window.location.href;
|
|
|
|
var target = lastRightClickedElement; // message.elementId ? browser.menus.getTargetElement(message.elementId) : null;
|
2019-06-29 19:54:25 +02:00
|
|
|
|
|
|
|
while (target) {
|
|
|
|
if ((<HTMLAnchorElement>target).href) break;
|
2019-03-03 21:02:40 +01:00
|
|
|
target = target.parentElement;
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
if (target && (<HTMLAnchorElement>target).href != message.url) target = null;
|
2018-12-16 16:42:20 +01:00
|
|
|
|
2019-06-29 19:47:05 +02:00
|
|
|
var identifier = target ? getIdentifier(<HTMLAnchorElement>target) : getIdentifier(message.url);
|
2019-03-03 21:02:40 +01:00
|
|
|
if (!identifier) return;
|
|
|
|
|
|
|
|
message.identifier = identifier;
|
|
|
|
if (identifier.startsWith('facebook.com/'))
|
|
|
|
message.secondaryIdentifier = getIdentifier(message.url);
|
2019-03-03 21:02:57 +01:00
|
|
|
|
2019-06-29 19:54:25 +02:00
|
|
|
var snippet = getSnippet(target);
|
2018-12-17 21:56:30 +01:00
|
|
|
message.linkId = ++lastGeneratedLinkId;
|
|
|
|
|
2019-06-29 19:54:25 +02:00
|
|
|
if (target)
|
2019-06-29 19:47:05 +02:00
|
|
|
target.setAttribute('shinigami-eyes-link-id', '' + lastGeneratedLinkId);
|
2018-12-17 21:56:30 +01:00
|
|
|
|
2019-03-03 21:02:40 +01:00
|
|
|
message.snippet = snippet ? snippet.outerHTML : null;
|
2018-12-16 17:36:19 +01:00
|
|
|
var debugClass = 'shinigami-eyes-debug-snippet-highlight';
|
2019-06-29 19:54:25 +02:00
|
|
|
|
2019-03-03 21:02:40 +01:00
|
|
|
if (snippet && message.debug) {
|
|
|
|
snippet.classList.add(debugClass);
|
2018-12-16 17:36:19 +01:00
|
|
|
if (message.debug <= 1)
|
2019-03-03 21:02:40 +01:00
|
|
|
setTimeout(() => snippet.classList.remove(debugClass), 1500)
|
2018-12-16 17:36:19 +01:00
|
|
|
}
|
2018-09-16 12:27:42 +02:00
|
|
|
sendResponse(message);
|
|
|
|
})
|