Midora/extension/content.ts

660 lines
25 KiB
TypeScript
Raw Normal View History

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 == 'mobile.twitter.com') hostname = 'twitter.com';
2018-09-16 12:27:42 +02:00
if (hostname.endsWith('.reddit.com')) hostname = 'reddit.com';
2019-02-25 23:44:34 +01:00
if (hostname.endsWith('.facebook.com')) hostname = 'facebook.com';
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;
2019-10-06 13:04:11 +02:00
var isSocialNetwork: boolean = null;
2018-11-16 21:51:31 +01:00
2018-12-16 17:36:19 +01:00
function fixupSiteStyles() {
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(`
2019-07-17 23:35:00 +02:00
.assigned-label-transphobic { outline: 2px solid var(--ShinigamiEyesTransphobic) !important; }
.assigned-label-t-friendly { outline: 1px solid var(--ShinigamiEyesTFriendly) !important; }
`);
2019-10-06 11:23:23 +02:00
} else if (hostname == 'rationalwiki.org' || domainIs(hostname, 'wikipedia.org')) {
addStyleSheet(`
2019-07-17 23:35:00 +02:00
.assigned-label-transphobic { outline: 1px solid var(--ShinigamiEyesTransphobic) !important; }
.assigned-label-t-friendly { outline: 1px solid var(--ShinigamiEyesTFriendly) !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) {
let m = document.querySelector('#USER_DROPDOWN_ID');
2018-11-16 21:51:31 +01:00
if (m) {
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
}
addStyleSheet(`
.author { color: #369 !important;}
`);
2018-09-16 12:27:42 +02:00
}
}
2018-11-16 21:51:31 +01:00
2019-06-29 19:54:25 +02:00
function addStyleSheet(css: string) {
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)
[...document.styleSheets].filter(<any>shouldDisable).forEach(x => x.disabled = true);
}
2018-09-16 12:27:42 +02:00
function init() {
2019-10-06 13:04:11 +02:00
isSocialNetwork = [
'facebook.com',
'youtube.com',
'reddit.com',
'twitter.com',
'medium.com',
'disqus.com',
'rationalwiki.org',
'duckduckgo.com',
'bing.com',
].includes(hostname) ||
domainIs(hostname, 'tumblr.com') ||
domainIs(hostname, 'wikipedia.org') ||
/^google(\.co)?\.\w+$/.test(hostname);
2018-11-16 21:51:31 +01:00
fixupSiteStyles();
2018-12-16 17:36:19 +01:00
if (domainIs(hostname, 'youtube.com')) {
2018-11-16 21:51:31 +01:00
setInterval(updateYouTubeChannelHeader, 300);
setInterval(updateAllLabels, 6000);
2018-11-16 21:51:31 +01:00
}
if (hostname == 'twitter.com') {
setInterval(updateTwitterClasses, 800);
}
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
2019-10-06 13:04:11 +02:00
document.addEventListener('contextmenu', evt => {
lastRightClickedElement = <HTMLElement>evt.target;
}, true);
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
2019-10-06 13:04:11 +02:00
if (isSocialNetwork) {
var observer = new MutationObserver(mutationsList => {
maybeDisableCustomCss();
for (const mutation of mutationsList) {
if (mutation.type == 'childList') {
for (const node of mutation.addedNodes) {
if (node instanceof HTMLAnchorElement) {
initLink(node);
}
if (node instanceof HTMLElement) {
for (const subnode of node.querySelectorAll('a')) {
initLink(subnode);
}
2018-09-16 12:27:42 +02:00
}
}
}
}
2019-10-06 13:04:11 +02:00
solvePendingLabels();
});
2018-09-16 12:27:42 +02:00
2019-10-06 13:04:11 +02:00
observer.observe(document.body, {
childList: true,
subtree: 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
var lastAppliedTwitterUrl: string = null;
2019-07-28 07:31:24 +02:00
function updateTwitterClasses() {
if (location.href != lastAppliedTwitterUrl) {
setTimeout(updateAllLabels, 200);
lastAppliedTwitterUrl = location.href;
}
for (const a of document.querySelectorAll('a')) {
if (a.assignedCssLabel && !a.classList.contains('has-assigned-label')) {
a.classList.add('assigned-label-' + a.assignedCssLabel);
2019-07-28 07:31:24 +02:00
a.classList.add('has-assigned-label');
}
}
}
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 = <HTMLElement>document.querySelector('#channel-header ytd-channel-name yt-formatted-string');
if (title && !title.parentElement.offsetParent) title = null;
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';
replacement.style.color = 'black';
2018-10-30 18:10:57 +01:00
}
replacement.textContent = lastAppliedYouTubeTitle;
replacement.href = lastAppliedYouTubeUrl;
}
updateAllLabels();
setTimeout(updateAllLabels, 2000);
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-10-06 13:04:11 +02:00
if (isSocialNetwork) {
for (const a of document.getElementsByTagName('a')) {
initLink(a);
}
2018-09-16 12:27:42 +02:00
}
solvePendingLabels();
}
var knownLabels: LabelMap = {};
2019-07-17 23:35:00 +02:00
var currentlyAppliedTheme = '_none_';
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 = [];
browser.runtime.sendMessage<ShinigamiEyesCommand, LabelMap>({ ids: uniqueIdentifiers, myself: <string>myself }, (response: LabelMap) => {
2019-07-17 23:35:00 +02:00
const theme = response[':theme'];
if (theme != currentlyAppliedTheme) {
if (currentlyAppliedTheme) document.body.classList.remove('shinigami-eyes-theme-' + currentlyAppliedTheme);
if (theme) document.body.classList.add('shinigami-eyes-theme-' + theme);
currentlyAppliedTheme = theme;
}
2019-06-29 15:41:37 +02:00
for (const item of tosolve) {
2019-07-17 23:35:00 +02:00
const label = response[item.identifier];
2018-09-16 12:27:42 +02:00
knownLabels[item.identifier] = label || '';
applyLabel(item.element, item.identifier);
}
});
}
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' || hostname == 'twitter.com')
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);
}
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;
}
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--;
if (m.length != num) return '!!'
2018-09-16 12:27:42 +02:00
return '/' + m.join('/');
}
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
function captureRegex(str: string, regex: RegExp) {
2018-12-16 17:36:19 +01:00
if (!str) return null;
var match = str.match(regex);
2018-12-16 17:36:19 +01:00
if (match && match[1]) return match[1];
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) {
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']")
if (elem) return captureRegex(elem.getAttribute('ajaxify'), /\/(\d+)\//);
2018-09-16 12:27:42 +02:00
// group
elem = document.querySelector("[id^='headerAction_']");
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;
}
function getIdentifier(link: string | HTMLAnchorElement) {
2018-12-16 17:36:19 +01:00
try {
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) {
console.warn("Unable to get identifier for " + link);
return null;
}
}
function getIdentifierFromElementImpl(element: HTMLAnchorElement): string {
if (!element) return null;
2018-09-16 12:27:42 +02:00
const dataset = element.dataset;
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;
}
// comment timestamp
if (element.firstChild && (<HTMLElement>element.firstChild).tagName == 'ABBR' && element.lastChild == element.firstChild) return null;
2019-06-29 19:54:25 +02:00
// post 'see more'
if (element.classList.contains('see_more_link')) return null;
// post 'continue reading'
if (parent && parent.classList.contains('text_exposed_link')) return null;
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;
}
// post Comments link
if (dataset.testid == 'UFI2CommentsCount/root') return null;
2019-06-29 19:54:25 +02:00
// notification
if (dataset.testid == 'notif_list_item_link') return null;
// post Comments link
if (dataset.commentPreludeRef) return null;
// page left sidebar
if (dataset.endpoint) return null;
2019-03-03 21:02:57 +01:00
// profile tabs
if (dataset.tabKey) return null;
2019-03-03 21:02:57 +01: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;
}
}
// comment interaction buttons
if (dataset.sigil) return null;
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-07-28 07:31:24 +02:00
} else if (hostname == 'twitter.com') {
if (dataset && dataset.expandedUrl) return getIdentifier(dataset.expandedUrl);
if (element.href.startsWith('https://t.co/')) {
const title = element.title;
if (title && (title.startsWith('http://') || title.startsWith('https://')))
return getIdentifier(title);
const content = element.textContent;
2019-07-28 08:01:17 +02:00
if (!content.includes(' ') && content.includes('.') && !content.includes('…'))
return getIdentifier('http://' + content);
}
} else if (domainIs(hostname, 'wikipedia.org')) {
if (element.classList.contains('interlanguage-link-target')) return null;
2018-09-16 12:27:42 +02:00
}
2019-07-14 21:55:23 +02:00
if (element.classList.contains('tumblelog')) return element.textContent.substr(1) + '.tumblr.com';
const href = element.href;
if (href && (!href.endsWith('#') || href.includes('&stick='))) return getIdentifierFromURLImpl(tryParseURL(href));
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 {
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;
}
}
2018-09-16 12:27:42 +02:00
function tryUnwrapNestedURL(url: URL): URL {
2019-06-29 19:54:25 +02:00
if (!url) return null;
2018-09-16 12:27:42 +02:00
if (url.href.indexOf('http', 1) != -1) {
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 tryParseURL(value);
2018-09-16 12:27:42 +02:00
}
}
const newurl = tryParseURL(url.href.substring(url.href.indexOf('http', 1)));
if (newurl) return newurl;
}
return null;
}
function getIdentifierFromURLImpl(url: URL): string {
if (!url) return null;
// nested urls
const nested = tryUnwrapNestedURL(url);
if (nested) {
return getIdentifierFromURLImpl(nested);
2018-09-16 12:27:42 +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;
const searchParams = url.searchParams;
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;
return getIdentifierFromURLImpl(tryParseURL('http://' + match));
2018-09-16 12:27:42 +02:00
}
2018-09-16 12:27:42 +02:00
if (host.startsWith('www.')) host = host.substring(4);
if (domainIs(host, 'facebook.com')) {
const fbId = searchParams.get('id');
2019-06-29 15:41:37 +02:00
const p = url.pathname.replace('/pg/', '/');
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/');
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;
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/');
if (!pathname.startsWith('/user/') && !pathname.startsWith('/channel/')) return null;
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/')) {
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;
} else if (domainIs(host, 'wikipedia.org') || domainIs(host, 'rationalwiki.org')) {
if (url.hash || url.pathname.includes(':')) return null;
2019-07-28 07:41:49 +02:00
if (url.pathname.startsWith('/wiki/')) return 'wikipedia.org' + decodeURIComponent(getPartialPath(url.pathname, 2));
2019-01-13 17:16:37 +01:00
else return null;
} 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';
else return null;
2019-07-28 07:31:24 +02:00
} else if (host.includes('google.')) {
if (url.pathname == '/search' && searchParams.get('stick') && !searchParams.get('tbm') && !searchParams.get('start')) {
const q = searchParams.get('q');
2019-07-28 07:31:24 +02:00
if (q) return 'wikipedia.org/wiki/' + q.replace(/\s/g, '_');
}
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;
2019-07-28 06:57:49 +02:00
if (hostname == 'twitter.com' && (classList.contains('stream-item') || classList.contains('permalink-tweet-container') || node.tagName == 'ARTICLE')) return node;
2019-03-03 21:02:57 +01:00
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;
}
function getBadIdentifierReason(identifier: string, url: string) {
identifier = identifier || '';
url = url || '';
if (url) {
const nested = tryUnwrapNestedURL(tryParseURL(url));
if (nested) url = nested.href;
}
if (identifier == 't.co') return 'Shortened link. Please follow the link and then mark the resulting page.';
if (
identifier.startsWith('reddit.com/user/') ||
identifier == 'twitter.com/threadreaderapp' ||
identifier == 'twitter.com/threader_app') return 'This is user is a bot.';
if (identifier == 'twitter.com/hashtag') return 'Hashtags cannot be labeled, only users.';
if (url.includes('youtube.com/watch')) return 'Only channels can be labeled, not specific videos.';
if (url.includes('reddit.com/') && url.includes('/comments/')) return 'Only users and subreddits can be labeled, not specific posts.';
if (url.includes('facebook.com') && (
url.includes('/posts/') ||
url.includes('/photo.php') ||
url.includes('/photos/'))) return 'Only pages, users and groups can be labeled, not specific posts or photos.';
if (url.includes('wiki') && url.includes('#')) return 'Wiki paragraphs cannot be labeled, only whole articles.';
return null;
}
var previousConfirmationMessage: HTMLElement = null;
function displayConfirmation(identifier: string, label: LabelKind, badIdentifierReason: BadIdentifierReason, url: string) {
if (previousConfirmationMessage) {
previousConfirmationMessage.remove();
previousConfirmationMessage = null;
}
if (!label) return;
if (isSocialNetwork && label != 'bad-identifier') return;
2019-10-06 14:29:26 +02:00
const confirmation = document.createElement('div');
const background =
label == 't-friendly' ? '#eaffcf' :
label == 'transphobic' ? '#f5d7d7' :
'#eeeeee';
confirmation.style.cssText = `transition: opacity 7s ease-in-out; opacity: 1; position: fixed; padding: 30px 15px; z-index: 99999999; white-space: pre-wrap; top: 200px; left: 30%; right: 30%; background: ${background}; color: black; font-weight: bold; font-family: Arial; box-shadow: 0px 5px 10px #ddd; border: 1px solid #ccc; font-size: 11pt;`;
let text: string;
if (label == 'bad-identifier') {
const displayReason = getBadIdentifierReason(identifier, url);
if (displayReason) text = displayReason;
else if (badIdentifierReason == 'SN') text = 'This social network is not supported: ' + identifier + '.';
else if (badIdentifierReason == 'AR') text = 'This is an archival link, it cannot be labeled: ' + identifier;
else text = `This item could not be labeled. Possible reasons:
It doesn't represent a specific user or page
It's not a kind of object supported by Shinigami Eyes
${identifier || url}
`;
} else {
text = identifier + (
label == 't-friendly' ? ' will be displayed as trans-friendly on search engines and social networks.' :
label == 'transphobic' ? ' will be displayed as anti-trans on search engines and social networks.' :
' has been cleared.'
);
}
confirmation.textContent = text;
2019-10-06 14:29:26 +02:00
document.body.appendChild(confirmation);
previousConfirmationMessage = confirmation;
confirmation.addEventListener('mousedown', () => confirmation.remove());
setTimeout(() => {
confirmation.style.opacity = '0';
}, 2000);
2019-10-06 14:29:26 +02:00
setTimeout(() => {
confirmation.remove();
}, 9000);
2019-10-06 14:29:26 +02:00
}
browser.runtime.onMessage.addListener<ShinigamiEyesMessage, ShinigamiEyesSubmission>((message, sender, sendResponse) => {
if (message.updateAllLabels || message.confirmSetLabel) {
displayConfirmation(message.confirmSetIdentifier, message.confirmSetLabel, message.badIdentifierReason, message.confirmSetUrl);
updateAllLabels(true);
return;
}
2019-06-29 19:54:25 +02: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;
target = target.parentElement;
}
if (target && (<HTMLAnchorElement>target).href != message.url) target = null;
var identifier = target ? getIdentifier(<HTMLAnchorElement>target) : getIdentifier(message.url);
if (!identifier) {
displayConfirmation(null, 'bad-identifier', null, message.url);
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)
target.setAttribute('shinigami-eyes-link-id', '' + lastGeneratedLinkId);
2018-12-17 21:56:30 +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
if (snippet && message.debug) {
snippet.classList.add(debugClass);
2018-12-16 17:36:19 +01:00
if (message.debug <= 1)
setTimeout(() => snippet.classList.remove(debugClass), 1500)
2018-12-16 17:36:19 +01:00
}
2019-10-06 14:29:26 +02:00
message.isSocialNetwork = isSocialNetwork;
2018-09-16 12:27:42 +02:00
sendResponse(message);
})