Merge pull request #852 from Davilarek/patch-2

Add jsdoc types for some functions
This commit is contained in:
ManeraKai 2023-11-15 15:19:01 +03:00 committed by GitHub
commit cac79c6959
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 105 additions and 1 deletions

View File

@ -1,6 +1,9 @@
window.browser = window.browser || window.chrome
function localisePage() {
/**
* @param {string} tag
*/
function getMessage(tag) {
return tag.replace(/__MSG_(\w+)__/g, (_match, v1) => {
return v1 ? browser.i18n.getMessage(v1) : null

View File

@ -30,6 +30,12 @@ function all(service, frontend, options, config) {
return instances
}
/**
* @param {string} service
* @param {URL} url
* @param {{}} config
* @param {string} frontend
*/
function regexArray(service, url, config, frontend) {
let targetList = config.services[service].targets
if (frontend && 'excludeTargets' in config.services[service].frontends[frontend]) {
@ -44,11 +50,24 @@ function regexArray(service, url, config, frontend) {
return false
}
/**
* @param {URL} url
* @param {string} type
* @param {URL} initiator
* @param {boolean} forceRedirection
*/
async function redirectAsync(url, type, initiator, forceRedirection) {
await init()
return redirect(url, type, initiator, forceRedirection)
}
/**
* @param {URL} url
* @param {string} type
* @param {URL} initiator
* @param {boolean} forceRedirection
* @returns {string | undefined}
*/
function redirect(url, type, initiator, forceRedirection) {
if (type != "main_frame" && type != "sub_frame" && type != "image") return
let randomInstance
@ -545,6 +564,10 @@ function redirect(url, type, initiator, forceRedirection) {
}
}
/**
* @param {URL} url
* @param {*} returnFrontend
*/
function computeService(url, returnFrontend) {
return new Promise(async resolve => {
const config = await utils.getConfig()
@ -569,6 +592,10 @@ function computeService(url, returnFrontend) {
})
}
/**
* @param {URL} url
* @param {string} customService
*/
function switchInstance(url, customService) {
return new Promise(async resolve => {
let options = await utils.getOptions()
@ -599,6 +626,9 @@ function switchInstance(url, customService) {
})
}
/**
* @param {URL} url
*/
async function reverse(url) {
let options = await utils.getOptions()
let config = await utils.getConfig()
@ -774,6 +804,10 @@ function processUpdate() {
})
}
/**
* @param {URL} url
* @param {boolean} test
*/
async function copyRaw(url, test) {
const newUrl = await reverse(url)
if (newUrl) {
@ -794,6 +828,9 @@ async function copyRaw(url, test) {
}
}
/**
* @param {URL} url
*/
function isException(url) {
if (!options.exceptions) return false
let exceptions = options.exceptions

View File

@ -1,18 +1,49 @@
window.browser = window.browser || window.chrome
/**
* @param {Array.<T>} instances
* @returns {T}
*/
function getRandomInstance(instances) {
return instances[~~(instances.length * Math.random())]
}
/**
* @param {string} str
*/
function camelCase(str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
/**
* @param {URL} url
*/
function protocolHost(url) {
if (url.username && url.password) return `${url.protocol}//${url.username}:${url.password}@${url.host}`
return `${url.protocol}//${url.host}`
}
/**
* @typedef FrontendInfo
* @prop {boolean} instanceList
* @prop {string} name
* @prop {string} url
*/
/**
* @typedef {Object} Service
* @prop {Object.<string, FrontendInfo>} frontends
* @prop {Object} options
*/
/**
* @typedef {Object} Config
* @prop {Object.<string, Service>} services
*/
/**
* @returns {Promise<Config>}
*/
function getConfig() {
return new Promise(resolve => {
fetch("/config.json")
@ -24,6 +55,14 @@ function getConfig() {
})
}
/**
* @typedef {Object} Option
* @prop {string} frontend
*/
/**
* @returns {Promise<Object.<string, Option | string[]>>}
*/
function getOptions() {
return new Promise(resolve =>
browser.storage.local.get("options", r => {
@ -106,6 +145,9 @@ function getList(options) {
})
}
/**
* @param {string} href
*/
function pingOnce(href) {
return new Promise(async resolve => {
let started
@ -130,6 +172,9 @@ function pingOnce(href) {
})
}
/**
* @param {string} href
*/
function ping(href) {
return new Promise(async resolve => {
let average = 0

View File

@ -17,6 +17,9 @@ for (const a of document.getElementById("links").getElementsByTagName("a")) {
config = await utils.getConfig()
options = await utils.getOptions()
/**
* @param {string} service
*/
async function changeFrontendsSettings(service) {
options = await utils.getOptions()
const opacityDiv = document.getElementById(`${service}-opacity`)
@ -95,6 +98,9 @@ async function changeFrontendsSettings(service) {
frontend_name_element.href = config.services[service].frontends[divs[service].frontend.value].url
}
/**
* @param {string} path
*/
async function loadPage(path) {
options = await utils.getOptions()
for (const section of document.getElementById("pages").getElementsByTagName("section")) section.style.display = "none"
@ -251,6 +257,13 @@ async function processCustomInstances(frontend, document) {
})
}
/**
* @param {string} frontend
* @param {*} networks
* @param {*} document
* @param {*} redirects
* @param {*} blacklist
*/
async function createList(frontend, networks, document, redirects, blacklist) {
const pingCache = await utils.getPingCache()
const options = await utils.getOptions()
@ -331,6 +344,9 @@ const r = window.location.href.match(/#(.*)/)
if (r) loadPage(r[1])
else loadPage("general")
/**
* @param {string} frontend
*/
async function ping(frontend) {
const instanceElements = [
...document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].getElementsByTagName('x'),
@ -357,6 +373,9 @@ async function ping(frontend) {
}
}
/**
* @param {number} time
*/
function processTime(time) {
let text
let color
@ -377,4 +396,4 @@ function processTime(time) {
return {
color, text
}
}
}