Fixed typescript compilation errors

This commit is contained in:
shinigami-eyes 2019-06-29 15:41:37 +02:00
parent bea4257a53
commit db717cfe15
5 changed files with 90 additions and 51 deletions

View File

@ -439,9 +439,9 @@ browser.contextMenus.onClicked.addListener(function (info, tab) {
var tabId = tab.id; var tabId = tab.id;
var frameId = info.frameId; var frameId = info.frameId;
var label = info.menuItemId.substring('mark-'.length); var label = <LabelKind>info.menuItemId.substring('mark-'.length);
if (label == 'none') label = ''; if (label == 'none') label = '';
browser.tabs.sendMessage(tabId, { browser.tabs.sendMessage<ShinigamiSubmission, ShinigamiSubmission>(tabId, {
mark: label, mark: label,
url: info.linkUrl, url: info.linkUrl,
tabId: tabId, tabId: tabId,

View File

@ -120,14 +120,14 @@ function init() {
var observer = new MutationObserver(mutationsList => { var observer = new MutationObserver(mutationsList => {
maybeDisableCustomCss(); maybeDisableCustomCss();
for (var mutation of mutationsList) { for (const mutation of mutationsList) {
if (mutation.type == 'childList') { if (mutation.type == 'childList') {
for (var node of mutation.addedNodes) { for (const node of mutation.addedNodes) {
if (node.tagName == 'A') { if (node instanceof HTMLAnchorElement) {
initLink(node); initLink(node);
} }
if (node.querySelectorAll) { if (node instanceof HTMLElement) {
for (var subnode of node.querySelectorAll('a')) { for (const subnode of node.querySelectorAll('a')) {
initLink(subnode); initLink(subnode);
} }
} }
@ -164,9 +164,9 @@ function updateYouTubeChannelHeader() {
lastAppliedYouTubeTitle = currentTitle; lastAppliedYouTubeTitle = currentTitle;
if (currentTitle) { if (currentTitle) {
var replacement = document.getElementById('channel-title-replacement'); var replacement = <HTMLAnchorElement>document.getElementById('channel-title-replacement');
if (!replacement) { if (!replacement) {
replacement = document.createElement('A'); replacement = <HTMLAnchorElement>document.createElement('A');
replacement.id = 'channel-title-replacement' replacement.id = 'channel-title-replacement'
replacement.className = title.className; replacement.className = title.className;
title.parentNode.insertBefore(replacement, title.nextSibling); title.parentNode.insertBefore(replacement, title.nextSibling);
@ -185,11 +185,11 @@ function updateYouTubeChannelHeader() {
setTimeout(updateAllLabels, 4000); setTimeout(updateAllLabels, 4000);
} }
function updateAllLabels(refresh) { function updateAllLabels(refresh?: boolean) {
if (refresh) knownLabels = {}; if (refresh) knownLabels = {};
var links = document.links; var links = document.links;
for (var i = 0; i < links.length; i++) { for (var i = 0; i < links.length; i++) {
var a = links[i]; var a = <HTMLAnchorElement>links[i];
initLink(a); initLink(a);
} }
solvePendingLabels(); solvePendingLabels();
@ -205,7 +205,7 @@ function solvePendingLabels() {
var tosolve = labelsToSolve; var tosolve = labelsToSolve;
labelsToSolve = []; labelsToSolve = [];
browser.runtime.sendMessage({ ids: uniqueIdentifiers, myself: myself }, response => { browser.runtime.sendMessage({ ids: uniqueIdentifiers, myself: myself }, response => {
for (item of tosolve) { for (const item of tosolve) {
var label = response[item.identifier]; var label = response[item.identifier];
knownLabels[item.identifier] = label || ''; knownLabels[item.identifier] = label || '';
applyLabel(item.element, item.identifier); applyLabel(item.element, item.identifier);
@ -230,7 +230,7 @@ function applyLabel(a, identifier) {
} }
} }
function initLink(a) { function initLink(a: HTMLAnchorElement) {
var identifier = getIdentifier(a); var identifier = getIdentifier(a);
if (!identifier){ if (!identifier){
if(hostname == 'youtube.com') if(hostname == 'youtube.com')
@ -254,7 +254,7 @@ function isHostedOn(/** @type {string}*/fullHost, /** @type {string}*/baseHost)
else return false; else return false;
} }
function getQuery(/** @type {string}*/search) { function getQuery(search: string) : any {
if (!search) return {}; if (!search) return {};
var s = {}; var s = {};
if (search.startsWith('?')) search = search.substring(1); if (search.startsWith('?')) search = search.substring(1);
@ -266,14 +266,14 @@ function getQuery(/** @type {string}*/search) {
return s; return s;
} }
function takeFirstPathComponents(/** @type {string}*/path, /** @type {number}*/num) { function takeFirstPathComponents(path: string, num: number) {
var m = path.split('/') var m = path.split('/')
m = m.slice(1, 1 + num); m = m.slice(1, 1 + num);
if (m.length && !m[m.length - 1]) m.length--; if (m.length && !m[m.length - 1]) m.length--;
if (m.length != num) return '!!' if (m.length != num) return '!!'
return '/' + m.join('/'); return '/' + m.join('/');
} }
function takeNthPathComponent(/** @type {string}*/path, /** @type {number}*/nth) { function takeNthPathComponent(path: string, nth: number) {
return path.split('/')[nth + 1] || null; return path.split('/')[nth + 1] || null;
} }
@ -287,7 +287,7 @@ function captureRegex(str, regex) {
function getCurrentFacebookPageId() { function getCurrentFacebookPageId() {
// page // page
var elem = document.querySelector("a[rel=theater][aria-label='Profile picture']"); var elem = <HTMLAnchorElement>document.querySelector("a[rel=theater][aria-label='Profile picture']");
if (elem) { if (elem) {
var p = captureRegex(elem.href, /facebook\.com\/(\d+)/) var p = captureRegex(elem.href, /facebook\.com\/(\d+)/)
if (p) return p; if (p) return p;
@ -322,7 +322,7 @@ function getIdentifierInternal(urlstr) {
if (!urlstr) return null; if (!urlstr) return null;
if (hostname == 'reddit.com') { if (hostname == 'reddit.com') {
var parent = urlstr.parentElement; const parent = urlstr.parentElement;
if (parent && parent.classList.contains('domain') && urlstr.textContent.startsWith('self.')) return null; if (parent && parent.classList.contains('domain') && urlstr.textContent.startsWith('self.')) return null;
} }
if (hostname == 'disqus.com') { if (hostname == 'disqus.com') {
@ -330,9 +330,9 @@ function getIdentifierInternal(urlstr) {
} }
if (hostname == 'facebook.com' && urlstr.tagName) { if (hostname == 'facebook.com' && urlstr.tagName) {
var parent = urlstr.parentElement; const parent = urlstr.parentElement;
if (parent && (parent.tagName == 'H1' || parent.id == 'fb-timeline-cover-name')) { if (parent && (parent.tagName == 'H1' || parent.id == 'fb-timeline-cover-name')) {
var id = getCurrentFacebookPageId(); const id = getCurrentFacebookPageId();
//console.log('Current fb page: ' + id) //console.log('Current fb page: ' + id)
if (id) if (id)
return 'facebook.com/' + id; return 'facebook.com/' + id;
@ -349,9 +349,9 @@ function getIdentifierInternal(urlstr) {
if (urlstr.dataset) { if (urlstr.dataset) {
var hovercard = urlstr.dataset.hovercard; const hovercard = urlstr.dataset.hovercard;
if (hovercard) { if (hovercard) {
var id = captureRegex(hovercard, /id=(\d+)/); const id = captureRegex(hovercard, /id=(\d+)/);
if (id) if (id)
return 'facebook.com/' + id; return 'facebook.com/' + id;
} }
@ -368,9 +368,9 @@ function getIdentifierInternal(urlstr) {
// profile tabs // profile tabs
if (urlstr.dataset.tabKey) return null; if (urlstr.dataset.tabKey) return null;
var gt = urlstr.dataset.gt; const gt = urlstr.dataset.gt;
if (gt) { if (gt) {
var gtParsed = JSON.parse(gt); const gtParsed = JSON.parse(gt);
if (gtParsed.engagement && gtParsed.engagement.eng_tid) { if (gtParsed.engagement && gtParsed.engagement.eng_tid) {
return 'facebook.com/' + gtParsed.engagement.eng_tid; return 'facebook.com/' + gtParsed.engagement.eng_tid;
} }
@ -379,11 +379,11 @@ function getIdentifierInternal(urlstr) {
// comment interaction buttons // comment interaction buttons
if (urlstr.dataset.sigil) return null; if (urlstr.dataset.sigil) return null;
var p = urlstr; let p = <HTMLElement>urlstr;
while (p) { while (p) {
var bt = p.dataset.bt; const bt = p.dataset.bt;
if (bt) { if (bt) {
var btParsed = JSON.parse(bt); const btParsed = JSON.parse(bt);
if (btParsed.id) return 'facebook.com/' + btParsed.id; if (btParsed.id) return 'facebook.com/' + btParsed.id;
} }
p = p.parentElement; p = p.parentElement;
@ -394,8 +394,9 @@ function getIdentifierInternal(urlstr) {
if (urlstr.href !== undefined) urlstr = urlstr.href; if (urlstr.href !== undefined) urlstr = urlstr.href;
if (!urlstr) return null; if (!urlstr) return null;
if (urlstr.endsWith('#')) return null; if (urlstr.endsWith('#')) return null;
let url;
try { try {
var url = new URL(urlstr); url = new URL(urlstr);
} catch (e) { } catch (e) {
return null; return null;
} }
@ -405,11 +406,11 @@ function getIdentifierInternal(urlstr) {
if (url.pathname.includes('/badge_member_list/')) return null; if (url.pathname.includes('/badge_member_list/')) return null;
if (url.href.indexOf('http', 1) != -1) { if (url.href.indexOf('http', 1) != -1) {
var s = getQuery(url.search); const s = getQuery(url.search);
urlstr = null; urlstr = null;
for (var key in s) { for (const key in s) {
if (s.hasOwnProperty(key)) { if (s.hasOwnProperty(key)) {
var element = s[key]; const element = s[key];
if (element.startsWith('http:') || element.startsWith('https')) { if (element.startsWith('http:') || element.startsWith('https')) {
urlstr = element; urlstr = element;
break; break;
@ -424,15 +425,15 @@ function getIdentifierInternal(urlstr) {
} catch (e) { } } catch (e) { }
} }
var host = url.hostname; let host = url.hostname;
if (isHostedOn(host, 'web.archive.org')) { if (isHostedOn(host, 'web.archive.org')) {
var match = captureRegex(url.href, /\/web\/\w+\/(.*)/); const match = captureRegex(url.href, /\/web\/\w+\/(.*)/);
if (!match) return null; if (!match) return null;
return getIdentifierInternal('http://' + match); return getIdentifierInternal('http://' + match);
} }
if (url.search && url.search.includes('http')) { if (url.search && url.search.includes('http')) {
if (url.pathname.startsWith('/intl/')) return null; // facebook language switch links if (url.pathname.startsWith('/intl/')) return null; // facebook language switch links
for (var q of url.searchParams) { for (const q of url.searchParams) {
if (q[1].startsWith('http')) return getIdentifierInternal(q[1]); if (q[1].startsWith('http')) return getIdentifierInternal(q[1]);
} }
} }
@ -444,12 +445,12 @@ function getIdentifierInternal(urlstr) {
if (host.startsWith('www.')) host = host.substring(4); if (host.startsWith('www.')) host = host.substring(4);
if (isHostedOn(host, 'facebook.com')) { if (isHostedOn(host, 'facebook.com')) {
var s = getQuery(url.search); const s = getQuery(url.search);
var p = url.pathname.replace('/pg/', '/'); const p = url.pathname.replace('/pg/', '/');
return 'facebook.com/' + (s.id || takeFirstPathComponents(p, p.startsWith('/groups/') ? 2 : 1).substring(1)); return 'facebook.com/' + (s.id || takeFirstPathComponents(p, p.startsWith('/groups/') ? 2 : 1).substring(1));
} }
if (isHostedOn(host, 'reddit.com')) { if (isHostedOn(host, 'reddit.com')) {
var pathname = url.pathname.replace('/u/', '/user/'); const pathname = url.pathname.replace('/u/', '/user/');
if (!pathname.startsWith('/user/') && !pathname.startsWith('/r/')) return null; if (!pathname.startsWith('/user/') && !pathname.startsWith('/r/')) return null;
if(pathname.includes('/comments/') && hostname == 'reddit.com') return null; if(pathname.includes('/comments/') && hostname == 'reddit.com') return null;
return 'reddit.com' + takeFirstPathComponents(pathname, 2); return 'reddit.com' + takeFirstPathComponents(pathname, 2);
@ -458,7 +459,7 @@ function getIdentifierInternal(urlstr) {
return 'twitter.com' + takeFirstPathComponents(url.pathname, 1); return 'twitter.com' + takeFirstPathComponents(url.pathname, 1);
} }
if (isHostedOn(host, 'youtube.com')) { if (isHostedOn(host, 'youtube.com')) {
var pathname = url.pathname.replace('/c/', '/user/'); const pathname = url.pathname.replace('/c/', '/user/');
if (!pathname.startsWith('/user/') && !pathname.startsWith('/channel/')) return null; if (!pathname.startsWith('/user/') && !pathname.startsWith('/channel/')) return null;
return 'youtube.com' + takeFirstPathComponents(pathname, 2); return 'youtube.com' + takeFirstPathComponents(pathname, 2);
} }
@ -470,7 +471,7 @@ function getIdentifierInternal(urlstr) {
} }
if (isHostedOn(host, 'tumblr.com')) { if (isHostedOn(host, 'tumblr.com')) {
if (url.pathname.startsWith('/register/follow/')) { if (url.pathname.startsWith('/register/follow/')) {
var name = takeNthPathComponent(url.pathname, 2); const name = takeNthPathComponent(url.pathname, 2);
return name ? name + '.tumblr.com' : null; return name ? name + '.tumblr.com' : null;
} }
if (host != 'www.tumblr.com' && host != 'assets.tumblr.com' && host.indexOf('.media.') == -1) { if (host != 'www.tumblr.com' && host != 'assets.tumblr.com' && host.indexOf('.media.') == -1) {
@ -484,11 +485,11 @@ function getIdentifierInternal(urlstr) {
else return null; else return null;
} }
if (host.indexOf('.blogspot.') != -1) { if (host.indexOf('.blogspot.') != -1) {
var m = captureRegex(host, /([a-zA-Z0-9\-]*)\.blogspot/); const m = captureRegex(host, /([a-zA-Z0-9\-]*)\.blogspot/);
if (m) return m + '.blogspot.com'; if (m) return m + '.blogspot.com';
} }
var id = host; let id = host;
if (id.startsWith('www.')) id = id.substr(4); if (id.startsWith('www.')) id = id.substr(4);
if (id.startsWith('m.')) id = id.substr(2); if (id.startsWith('m.')) id = id.substr(2);
return id; return id;

View File

@ -3,4 +3,14 @@ declare class BloomFilter {
test(key: string): boolean; test(key: string): boolean;
name: string; name: string;
} }
type LabelKind = 't-friendly' | 'transphobic' | 'none' | '';
type ShinigamiSubmission = {
mark: LabelKind
url: string
tabId: number
frameId: number
debug: number
identifier?: string
secondaryIdentifier?: string
}
type ContextMenuCommand = 'mark-t-friendly' | 'mark-transphobic' | 'mark-none' | 'help'; type ContextMenuCommand = 'mark-t-friendly' | 'mark-transphobic' | 'mark-none' | 'help';

View File

@ -1,10 +1,8 @@
var browser : Browser = browser || chrome; var browser : Browser = browser || chrome;
document.getElementById('cancelButton').addEventListener('click', () => { document.getElementById('cancelButton').addEventListener('click', () => {
browser.runtime.sendMessage({ acceptClicked: false }, () => { });
browser.runtime.sendMessage({ acceptClicked: false }, response => { });
}) })
document.getElementById('acceptButton').addEventListener('click', () => { document.getElementById('acceptButton').addEventListener('click', () => {
browser.runtime.sendMessage({ acceptClicked: true }, () => { });
browser.runtime.sendMessage({ acceptClicked: true }, response => { });
}) })

View File

@ -1,18 +1,48 @@
declare type Browser = { declare type Browser = {
runtime: { runtime: {
sendMessage<TRequest, TResponse>(request: TRequest, response: (response: TResponse) => void);
onMessage: {
addListener(listener: (message, any, sendResponse) => void)
}
} }
storage: { storage: {
local: BrowserStorage local: BrowserStorage
} }
tabs: {
remove(id: number)
sendMessage<TRequest, TResponse>(tabId: number, request: TRequest, options?: {
frameId: number
}, callback?: (response: TResponse) => void)
create(options: {
url: string
})
}
extension: { extension: {
getURL(relativeUrl: string): string
}
contextMenus: {
create(options: {
id: string
title: string
contexts: 'link'[]
targetUrlPatterns: string[]
}): void
onClicked: {
addListener(listener: (info: {
menuItemId: string
frameId: number
linkUrl: string
}, tab: {
id: number
}) => void)
}
} }
} }
declare type BrowserStorage = { declare type BrowserStorage = {
get(names: string[], callback: (obj: any) => void)
set(obj: { [name: string]: any });
} }
declare var browser : Browser; declare var browser: Browser;
declare var chrome : Browser; declare var chrome: Browser;