Skip to content

Commit

Permalink
Merge pull request #852 from Davilarek/patch-2
Browse files Browse the repository at this point in the history
Add jsdoc types for some functions
  • Loading branch information
ManeraKai authored Nov 15, 2023
2 parents 62d10a2 + 57c978b commit cac79c6
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/assets/javascripts/localise.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/assets/javascripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -774,6 +804,10 @@ function processUpdate() {
})
}

/**
* @param {URL} url
* @param {boolean} test
*/
async function copyRaw(url, test) {
const newUrl = await reverse(url)
if (newUrl) {
Expand All @@ -794,6 +828,9 @@ async function copyRaw(url, test) {
}
}

/**
* @param {URL} url
*/
function isException(url) {
if (!options.exceptions) return false
let exceptions = options.exceptions
Expand Down
45 changes: 45 additions & 0 deletions src/assets/javascripts/utils.js
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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 => {
Expand Down Expand Up @@ -106,6 +145,9 @@ function getList(options) {
})
}

/**
* @param {string} href
*/
function pingOnce(href) {
return new Promise(async resolve => {
let started
Expand All @@ -130,6 +172,9 @@ function pingOnce(href) {
})
}

/**
* @param {string} href
*/
function ping(href) {
return new Promise(async resolve => {
let average = 0
Expand Down
21 changes: 20 additions & 1 deletion src/pages/options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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'),
Expand All @@ -357,6 +373,9 @@ async function ping(frontend) {
}
}

/**
* @param {number} time
*/
function processTime(time) {
let text
let color
Expand All @@ -377,4 +396,4 @@ function processTime(time) {
return {
color, text
}
}
}

0 comments on commit cac79c6

Please sign in to comment.