Skip to content

Commit

Permalink
switched to TetraxUSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrax-10 committed Jan 15, 2024
1 parent b0cd3af commit 59562e6
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
;(async () => {
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,
})
})
while (!window.TetraxUSL.stash) {
await new Promise((resolve) => setTimeout(resolve, 100))
}

const Stash = window.TetraxUSL.stash
const Utils = window.TetraxUSL.utils

async function injectVideosAsPreview(timeOut) {
await waitForElement(".scene-card-preview-video", timeOut)
await Utils.ui.waitForElement(".scene-card-preview-video", timeOut)

const allPreviewElement = document.querySelectorAll(".scene-card-preview-video")
allPreviewElement.forEach((element) => {
Expand All @@ -42,30 +17,23 @@

/////////////////////////////// Main ///////////////////////////////

;(async function main() {
if (!window.stash) {
setTimeout(main, 300)
return
}

if (await waitForElement(".main > div", 5000)) {
let previousUrl = window.location.href
setInterval(() => {
if (!document.querySelector(".main > div[playVideosInsteadOfPreviews]") || window.location.href !== previousUrl) {
document.querySelector(".main > div").setAttribute("playVideosInsteadOfPreviews", "")
previousUrl = window.location.href

if (
stash.matchUrl(window.location, /\/scenes\?/) ||
stash.matchUrl(window.location, /\/performers\/\d+\?/) ||
stash.matchUrl(window.location, /\/studios\/\d+\?/) ||
stash.matchUrl(window.location, /\/tags\/\d+\?/) ||
stash.matchUrl(window.location, /\/$/)
) {
injectVideosAsPreview(5000)
}
if (await Utils.ui.waitForElement(".main > div", 5000)) {
let previousUrl = window.location.href
setInterval(() => {
if (!document.querySelector(".main > div[playVideosInsteadOfPreviews]") || window.location.href !== previousUrl) {
document.querySelector(".main > div").setAttribute("playVideosInsteadOfPreviews", "")
previousUrl = window.location.href

if (
Stash.matchUrl(window.location, /\/scenes\?/) ||
Stash.matchUrl(window.location, /\/performers\/\d+\?/) ||
Stash.matchUrl(window.location, /\/studios\/\d+\?/) ||
Stash.matchUrl(window.location, /\/tags\/\d+\?/) ||
Stash.matchUrl(window.location, /\/$/)
) {
injectVideosAsPreview(5000)
}
}, 100)
}
})()
}
}, 100)
}
})()
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ description: Plays videos instead of previews in scene cards
version: 1.0
ui:
requires:
- StashUserscriptLibrary
- tetrax-userscript-library
javascript:
- play-videos-instead-of-previews.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
;(async () => {
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,
})
})
while (!window.TetraxUSL.stash) {
await new Promise((resolve) => setTimeout(resolve, 100))
}

const Stash = window.TetraxUSL.stash
const Utils = window.TetraxUSL.utils

async function replaceThumbnailsWithImages(timeOut, className) {
await waitForElement(className, timeOut)
await Utils.ui.waitForElement(className, timeOut)

const allThumbnailElement = document.querySelectorAll(className)
allThumbnailElement.forEach((element) => {
Expand All @@ -42,32 +17,25 @@

/////////////////////////////// Main ///////////////////////////////

;(async function main() {
if (!window.stash) {
setTimeout(main, 300)
return
}

if (await waitForElement(".main > div", 5000)) {
let previousUrl = window.location.href
setInterval(() => {
if (!document.querySelector(".main > div[replaceThumbnailsWithImages]") || window.location.href !== previousUrl) {
document.querySelector(".main > div").setAttribute("replaceThumbnailsWithImages", "")
previousUrl = window.location.href

if (
stash.matchUrl(window.location, /\/images\?/) ||
stash.matchUrl(window.location, /\/galleries\/\d+\?/) ||
stash.matchUrl(window.location, /\/galleries\/\d+\/add/) ||
stash.matchUrl(window.location, /\/performers\/\d+\/images/) ||
stash.matchUrl(window.location, /\/$/)
) {
replaceThumbnailsWithImages(5000, ".image-card-preview-image")
} else if (stash.matchUrl(window.location, /\/galleries\?/) || stash.matchUrl(window.location, /\/performers\/\d+\/galleries/) || stash.matchUrl(window.location, /\/$/)) {
replaceThumbnailsWithImages(5000, ".gallery-card-image")
}
if (await Utils.ui.waitForElement(".main > div", 5000)) {
let previousUrl = window.location.href
setInterval(() => {
if (!document.querySelector(".main > div[replaceThumbnailsWithImages]") || window.location.href !== previousUrl) {
document.querySelector(".main > div").setAttribute("replaceThumbnailsWithImages", "")
previousUrl = window.location.href

if (
Stash.matchUrl(window.location, /\/images\?/) ||
Stash.matchUrl(window.location, /\/galleries\/\d+\?/) ||
Stash.matchUrl(window.location, /\/galleries\/\d+\/add/) ||
Stash.matchUrl(window.location, /\/performers\/\d+\/images/) ||
Stash.matchUrl(window.location, /\/$/)
) {
replaceThumbnailsWithImages(5000, ".image-card-preview-image")
} else if (Stash.matchUrl(window.location, /\/galleries\?/) || Stash.matchUrl(window.location, /\/performers\/\d+\/galleries/) || Stash.matchUrl(window.location, /\/$/)) {
replaceThumbnailsWithImages(5000, ".gallery-card-image")
}
}, 100)
}
})()
}
}, 100)
}
})()
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ description: Replaces thumbnails with original high res images
version: 1.0
ui:
requires:
- StashUserscriptLibrary
- tetrax-userscript-library
javascript:
- replace-thumbnails-with-images.js
101 changes: 101 additions & 0 deletions Plugins/tetrax-userscript-library/tetrax-userscript-library.js
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,
},
},
}
})()
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

0 comments on commit 59562e6

Please sign in to comment.