-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
159 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
Plugins/tetrax-userscript-library/tetrax-userscript-library.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
window.TetraxUSL = (() => { | ||
function concatRegexp(reg, exp) { | ||
let flags = reg.flags + exp.flags | ||
flags = Array.from(new Set(flags.split(""))).join() | ||
return new RegExp(reg.source + exp.source, flags) | ||
} | ||
|
||
function matchUrl(location, fragment) { | ||
const regexp = concatRegexp(new RegExp(location.origin), fragment) | ||
return location.href.match(regexp) != null | ||
} | ||
|
||
async function callGQL(reqData) { | ||
const options = { | ||
method: "POST", | ||
body: JSON.stringify(reqData), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
|
||
try { | ||
const res = await window.fetch("/graphql", options) | ||
return res.json() | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
|
||
async function getInstalledPlugins() { | ||
try { | ||
const res = await callGQL({ | ||
operationName: "Plugins", | ||
variables: {}, | ||
query: `query Plugins{plugins{id}}`, | ||
}) | ||
|
||
return res.data.plugins.map((plugin) => plugin.id) | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
|
||
async function isPluginInstalled(plugin) { | ||
const installedPlugins = await getInstalledPlugins() | ||
return installedPlugins.includes(plugin) | ||
} | ||
|
||
async function waitForElement(selector, timeout = null, location = document.body) { | ||
return new Promise((resolve) => { | ||
if (document.querySelector(selector)) { | ||
return resolve(document.querySelector(selector)) | ||
} | ||
|
||
const observer = new MutationObserver(async () => { | ||
if (document.querySelector(selector)) { | ||
resolve(document.querySelector(selector)) | ||
observer.disconnect() | ||
} else { | ||
if (timeout) { | ||
async function timeOver() { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
observer.disconnect() | ||
resolve(false) | ||
}, timeout) | ||
}) | ||
} | ||
resolve(await timeOver()) | ||
} | ||
} | ||
}) | ||
|
||
observer.observe(location, { | ||
childList: true, | ||
subtree: true, | ||
}) | ||
}) | ||
} | ||
|
||
return { | ||
stash: { | ||
concatRegexp: concatRegexp, | ||
matchUrl: matchUrl, | ||
callGQL: callGQL, | ||
get: { | ||
plugins: { | ||
installed: getInstalledPlugins, | ||
}, | ||
}, | ||
}, | ||
utils: { | ||
ui: { | ||
waitForElement: waitForElement, | ||
}, | ||
plugins: { | ||
isInstalled: isPluginInstalled, | ||
}, | ||
}, | ||
} | ||
})() |
6 changes: 6 additions & 0 deletions
6
Plugins/tetrax-userscript-library/tetrax-userscript-library.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name: Tetrax Userscript Library | ||
description: Exports utility functions thats used in Tetrax's plugins | ||
version: 1.0 | ||
ui: | ||
javascript: | ||
- tetrax-userscript-library.js |