// ==UserScript==
// @name Greasy Fork+
// @name:de Greasy Fork+
// @name:es Greasy Fork+
// @name:fr Greasy Fork+
// @name:it Greasy Fork+
// @name:ru Greasy Fork+
// @name:zh-CN Greasy Fork+
// @author Davide <iFelix18@protonmail.com>
// @namespace https://github.com/iFelix18
// @icon https://www.google.com/s2/favicons?domain=https://greasyfork.org
// @description Adds various features and improves the Greasy Fork experience
// @description:de Fügt verschiedene Funktionen hinzu und verbessert das Greasy Fork-Erlebnis
// @description:es Agrega varias funciones y mejora la experiencia de Greasy Fork
// @description:fr Ajoute diverses fonctionnalités et améliore l'expérience Greasy Fork
// @description:it Aggiunge varie funzionalità e migliora l'esperienza di Greasy Fork
// @description:ru Добавляет различные функции и улучшает работу с Greasy Fork
// @description:zh-CN 添加各种功能并改善 Greasy Fork 体验
// @copyright 2021, Davide (https://github.com/iFelix18)
// @license MIT
// @version 1.8.8
// @homepage https://github.com/iFelix18/Userscripts#readme
// @homepageURL https://github.com/iFelix18/Userscripts#readme
// @supportURL https://github.com/iFelix18/Userscripts/issues
// @require https://cdn.jsdelivr.net/gh/sizzlemctwizzle/GM_config@43fd0fe4de1166f343883511e53546e87840aeaf/gm_config.min.js
// @require https://cdn.jsdelivr.net/npm/@ifelix18/utils@5.0.0/lib/index.min.js
// @require https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1.2.6/dist/index.min.js
// @match *://greasyfork.org/*
// @match *://sleazyfork.org/*
// @connect greasyfork.org
// @compatible chrome
// @compatible edge
// @compatible firefox
// @compatible safari
// @grant GM_getValue
// @grant GM_setValue
// @grant GM.deleteValue
// @grant GM.getValue
// @grant GM.notification
// @grant GM.registerMenuCommand
// @grant GM.setValue
// @grant GM.xmlHttpRequest
// @run-at document-idle
// @inject-into page
// ==/UserScript==
/* global $, GM_configStruct, u, UserscriptUtils, VM */
(async () => {
u.migrateConfig('config', 'greasyfork-plus') // migrate to the new config ID
//* Const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Tg9xuoWSUYAa9GHJyjZa0E2B7ndIukygH/ATw6Alo4y7LkFFO9XRxlaD9b+hesnR9ysyvfbwzzN3RazRSdU901kJQ2I42zNJVV0w7YMRAACAgI4c+ZMPv/880xKStIFBAR8hAFyoLP1fMIj1jNr/g5AXFwc33nnHaakpEgzWQPySHPr6c1ccVK63R8BMJlM8hLZMWPGGAIDAyvFOgfjJpIkAK8mJSX9OyMjw6BUKrlx40ZqNBrqdDoeOHCAd9xxh4VyZs2axR9//JFXr151GHkgiauTMKQIhWvXrlGj0fCjjz5iSEgIy8rKpMiOdqVS+a0YOpKEm1QEceQ8DsD2sLAw3YIFC1hSUkKtVsuamhrZPWxvsZ515AHcPAlDilAwGo1sa2tjY2Mjd+3axbS0NAYGBraK4YLjxLoJuIUkAcByQRCOp6WlXVm4cKFh6tSpnDhxIquqqlhVVcXp06czOjqawcHBNpEHcHFyxalTp+Rls/v27eOKFSsYExOjFwThN1wPEV8OJ4Gyt5IocX3BQk5QUNB/x8bGfpeenv6rWq226TOSkpJ44cIFedOPzs5OajQai4OXBw0axGXLlnHChAkE0J6cnHw+Ojr6W1xfFpQjlqXyF0pwKUajMUAQBMV1n5Zg4ehSKBRd4u8q0enVZcchppKudXXdli1bAvfs2aP+448/wvV6fbhOp7uzq6srzWg03knyDpIxJCMBRHR1dYWpVCoA0Hd1dV0FcBWABsDF8PDwOpVKVaXVan8ZOXJkZ1xcXNvhw4ebxZGsRZlSfUwmk0oQBLS3t3eLwVTuOPvsvo+z9zSX/wfl+jWwZp8+ogAAAABJRU5ErkJggg=='
const nonLatins = /[^\p{Script=Latin}\p{Script=Common}\p{Script=Inherited}]/gu
const blacklist = new RegExp([ /* cSpell: disable-next-line */
'\\bagar((.)?io)?\\b', '\\bagma((.)?io)?\\b', '\\baimbot\\b', '\\barras((.)?io)?\\b', '\\bbot(s)?\\b', '\\bbubble((.)?am)?\\b', '\\bcheat(s)?\\b', '\\bdiep((.)?io)?\\b', '\\bfreebitco((.)?in)?\\b', '\\bgota((.)?io)?\\b', '\\bhack(s)?\\b', '\\bkrunker((.)?io)?\\b', '\\blostworld((.)?io)?\\b', '\\bmoomoo((.)?io)?\\b', '\\broblox(.com)?\\b', '\\bshell\\sshockers\\b', '\\bshellshock((.)?io)?\\b', '\\bshellshockers\\b', '\\bskribbl((.)?io)?\\b', '\\bslither((.)?io)?\\b', '\\bsurviv((.)?io)?\\b', '\\btaming((.)?io)?\\b', '\\bvenge((.)?io)?\\b', '\\bvertix((.)?io)?\\b', '\\bzombs((.)?io)?\\b', '\\p{Extended_Pictographic}'
].join('|'), 'giu')
const hiddenList = JSON.parse(await GM.getValue('hiddenList', '{}'))
const lang = $('html').attr('lang')
const locales = { /* cSpell: disable */
de: {
downgrade: 'Auf zurückstufen',
hide: '❌ Dieses skript ausblenden',
install: 'Installieren',
notHide: '✔️ Dieses skript nicht ausblenden',
milestone: 'Herzlichen Glückwunsch, Ihre Skripte haben den Meilenstein von insgesamt $1 Installationen überschritten!',
reinstall: 'Erneut installieren',
update: 'Auf aktualisieren'
},
en: {
downgrade: 'Downgrade to',
hide: '❌ Hide this script',
install: 'Install',
notHide: '✔️ Not hide this script',
milestone: 'Congrats, your scripts got over the milestone of $1 total installs!',
reinstall: 'Reinstall',
update: 'Update to'
},
es: {
downgrade: 'Degradar a',
hide: '❌ Ocultar este script',
install: 'Instalar',
notHide: '✔️ No ocultar este script',
milestone: '¡Felicidades, sus scripts superaron el hito de $1 instalaciones totales!',
reinstall: 'Reinstalar',
update: 'Actualizar a'
},
fr: {
downgrade: 'Revenir à',
hide: '❌ Cacher ce script',
install: 'Installer',
notHide: '✔️ Ne pas cacher ce script',
milestone: 'Félicitations, vos scripts ont franchi le cap des $1 installations au total!',
reinstall: 'Réinstaller',
update: 'Mettre à'
},
it: {
downgrade: 'Riporta a',
hide: '❌ Nascondi questo script',
install: 'Installa',
notHide: '✔️ Non nascondere questo script',
milestone: 'Congratulazioni, i tuoi script hanno superato il traguardo di $1 installazioni totali!',
reinstall: 'Reinstalla',
update: 'Aggiorna a'
},
ru: {
downgrade: 'Откатить до',
hide: '❌ Скрыть этот скрипт',
install: 'Установить',
notHide: '✔️ Не скрывать этот сценарий',
milestone: 'Поздравляем, ваши скрипты преодолели рубеж в $1 установок!',
reinstall: 'Переустановить',
update: 'Обновить до'
},
'zh-CN': {
downgrade: '降级到',
hide: '❌ 隐藏此脚本',
install: '安装',
notHide: '✔️ 不隐藏此脚本',
milestone: '恭喜,您的脚本超过了 $1 次总安装的里程碑!',
reinstall: '重新安装',
update: '更新到'
}
} /* cSpell: enable */
//* GM_config
const config = new GM_configStruct()
const fields = {
hideNonLatinScripts: {
label: 'Hide non-Latin scripts, press "Ctrl + Alt + L" to show non-Latin scripts',
section: ['Features'],
labelPos: 'right',
type: 'checkbox',
default: true
},
hideBlacklistedScripts: {
label: 'Hide blacklisted scripts, press "Ctrl + Alt + B" to show Blacklisted scripts',
labelPos: 'right',
type: 'checkbox',
default: true
},
hideScript: {
label: 'Add a button to hide the script, press "Ctrl + Alt + H" to show Hidden scripts',
labelPos: 'right',
type: 'checkbox',
default: true
},
installButton: {
label: 'Add a button to install the script directly',
labelPos: 'right',
type: 'checkbox',
default: true
},
showTotalInstalls: {
label: 'Shows the number of daily and total installations on the user profile',
labelPos: 'right',
type: 'checkbox',
default: true
},
milestoneNotification: {
label: 'Get notified whenever your total installs got over any of these milestone (leave blank to disable) - Separate milestones with a comma!',
labelPos: 'left',
type: 'text',
title: 'Separate milestones with a comma!',
size: 150,
default: '10, 100, 500, 1000, 2500, 5000, 10000, 100000, 1000000'
},
logging: {
label: 'Logging',
section: ['Develop'],
labelPos: 'right',
type: 'checkbox',
default: false
}
}
const title = `${GM.info.script.name} v${GM.info.script.version} Settings`
const id = 'greasyfork-plus'
if (document.location.pathname === '/settings') {
$('html').attr('style', 'background: none !important;')
document.title = title
config.init({
frame: $('body > .width-constraint').empty().get(0),
id,
title,
fields,
css: '#greasyfork-plus *{font-family:Open Sans,sans-serif,Segoe UI Emoji!important;color:#000!important}#greasyfork-plus{background-color:#fff!important;border-radius:5px!important;border:1px solid #bbb!important;box-shadow:0 0 5px #ddd!important;box-sizing:border-box!important;height:auto!important;list-style-type:none!important;margin-bottom:0!important;margin-left:auto!important;margin-right:auto!important;margin-top:14px!important;max-height:none!important;max-width:1200px!important;padding:0 1em 1em!important;position:static!important;width:auto!important}#greasyfork-plus .config_header{display:block!important;font-size:1.5em!important;font-weight:700!important;margin-bottom:.83em!important;margin-left:0!important;margin-right:0!important;margin-top:.83em!important}#greasyfork-plus .section_header{background-color:#670000!important;background-image:linear-gradient(#670000,#900)!important;border:1px solid transparent!important;color:#fff!important}#greasyfork-plus .field_label{font-size:.85em!important;font-weight:500!important;margin-left:6px!important}#greasyfork-plus_field_milestoneNotification{display:ruby-text-container!important;width:100%!important}#greasyfork-plus_closeBtn{display:none!important}',
events: {
init: () => {
config.open()
},
save: () => {
window.location = document.referrer
}
}
})
} else {
config.init({
id,
title,
fields,
events: {
init: () => {
if (GM.info.scriptHandler !== 'Userscripts') { //! Userscripts Safari: GM.registerMenuCommand is missing
GM.registerMenuCommand('Configure', () => config.open())
}
},
save: () => {
config.close()
setTimeout(window.location.reload(false), 500)
}
}
})
}
//* Utils
const UU = new UserscriptUtils({
name: GM.info.script.name,
version: GM.info.script.version,
author: GM.info.script.author,
logging: config.get('logging')
})
UU.init(id)
UU.log(nonLatins)
UU.log(blacklist)
UU.log(hiddenList)
//* Shortcuts
const { register } = VM.shortcut
register('ctrl-alt-l', () => {
$('.script-list li.non-latin').toggle()
})
register('ctrl-alt-b', () => {
$('.script-list li.blacklisted').toggle()
})
register('ctrl-alt-h', () => {
$('.script-list li.hidden').toggle()
})
//* Functions
/**
* Adds a link to the menu to access the script configuration
*/
const addSettings = () => {
const menu = `<li class='${GM.info.script.name.toLowerCase().replace(/\s/g, '-')}-settings'><a href='/settings' title='Settings'>${GM.info.script.name}</a></li>`
if (document.location.pathname !== '/settings') $('#site-nav > nav > li').first().before(menu)
}
/**
* Adds buttons to the side menu to quickly show/hide scripts hidden by filters
*/
const addOptions = () => {
// create menu
const html = `
<div id="${GM.info.script.name.toLowerCase().replace(/\s/g, '-')}-options" class="list-option-group">${GM.info.script.name} filters:
<ul>
<li class="list-option non-latin"><a href="/non-latin-scripts" onclick="return false">Non-Latin scripts</a></li>
<li class="list-option blacklisted"><a href="/blacklisted-scripts" onclick="return false">Blacklisted scripts</a></li>
<li class="list-option hidden"><a href="/hidden-scripts" onclick="return false">Hidden scripts</a></li>
</ul>
</div>
`
$('.list-option-groups > div').first().before(html)
// click
$('.list-option-group li.non-latin').click(() => $('.script-list li.non-latin').toggle())
$('.list-option-group li.blacklisted').click(() => $('.script-list li.blacklisted').toggle())
$('.list-option-group li.hidden').click(() => $('.script-list li.hidden').toggle())
}
/**
* Get script data from Greasy Fork API
*
* @param {number} id Script ID
* @returns {Promise} Script data
*/
const getScriptData = async (id) => {
return new Promise((resolve, reject) => {
GM.xmlHttpRequest({
method: 'GET',
url: `https://${window.location.hostname}/scripts/${id}.json`,
onload: (response) => {
UU.log(`${response.status}: ${response.finalUrl}`)
const data = JSON.parse(response.responseText)
resolve(data)
}
})
})
}
/**
* Get user data from Greasy Fork API
*
* @param {string} userID User ID
* @returns {Promise} User data
*/
const getUserData = (userID) => {
return new Promise((resolve, reject) => {
GM.xmlHttpRequest({
method: 'GET',
url: `https://${window.location.hostname}/users/${userID}.json`,
onload: (response) => {
UU.log(`${response.status}: ${response.finalUrl}`)
const data = JSON.parse(response.responseText)
resolve(data)
}
})
})
}
/**
* Get user total installs
*
* @param {object} data Data
* @returns {Promise} Total installs
*/
const getTotalInstalls = (data) => {
return new Promise((resolve, reject) => {
const totalInstalls = []
$.each(data.scripts, (index, element) => {
totalInstalls.push(Number.parseInt(element.total_installs, 10))
})
resolve(totalInstalls.reduce((a, b) => a + b, 0))
})
}
/**
* Returns installed version
*
* @param {string} name Script name
* @param {string} namespace Script namespace
* @returns {string} Installed version
*/
const isInstalled = (name, namespace) => {
return new Promise((resolve, reject) => {
if (window.external && window.external.Violentmonkey) {
window.external.Violentmonkey.isInstalled(name, namespace).then((data) => resolve(data))
return
}
if (window.external && window.external.Tampermonkey) {
window.external.Tampermonkey.isInstalled(name, namespace, (data) => {
(data.installed) ? resolve(data.version) : resolve()
})
return
}
resolve()
})
}
/**
* Compare two version
*
* @param {string} v1 First version
* @param {string} v2 Second version
* @returns {number} Comparison value
*/
const compareVersions = (v1, v2) => {
if (!v1 || !v2) return
if (v1 === null || v2 === null) return
if (v1 === v2) return 0
const sv1 = v1.split('.').map((index) => +index)
const sv2 = v2.split('.').map((index) => +index)
for (let index = 0; index < Math.max(sv1.length, sv2.length); index++) {
if (sv1[index] > sv2[index]) return 1
if (sv1[index] < sv2[index]) return -1
}
return 0
}
/**
* Return label for the hide script button
*
* @param {boolean} hidden Is hidden
* @returns {string} Label
*/
const blockLabel = (hidden) => {
return hidden ? (locales[lang] ? locales[lang].notHide : locales.en.notHide) : (locales[lang] ? locales[lang].hide : locales.en.hide)
}
/**
* Return label for the install button
*
* @param {number} update Update value
* @returns {string} Label
*/
const installLabel = (update) => {
switch (update) {
case undefined: {
return locales[lang] ? locales[lang].install : locales.en.install
}
case 1: {
return locales[lang] ? locales[lang].update : locales.en.update
}
case -1: {
return locales[lang] ? locales[lang].downgrade : locales.en.downgrade
}
default: {
return locales[lang] ? locales[lang].reinstall : locales.en.reinstall
}
}
}
/**
* Hide all scripts with non-Latin characters in the name or description
*
* @param {object} element Script
*/
const hideNonLatinScripts = (element) => {
const name = $(element).find('.script-link').text()
const description = $(element).find('.script-description').text()
if (!name) return
if (nonLatins.test(name) || nonLatins.test(description)) {
$(element).addClass('non-latin')
}
}
/**
* Hide all scripts with blacklisted words in the name or description
*
* @param {object} element Script
*/
const hideBlacklistedScripts = (element) => {
const name = $(element).find('.script-link').text()
const description = $(element).find('.script-description').text()
if (!name) return
if (blacklist.test(name) || blacklist.test(description)) {
$(element).addClass('blacklisted')
}
}
/**
* Hide scripts
*
* @param {object} element Script
* @param {number} id Script ID
* @param {boolean} list Is list
*/
const hideScript = async (element, id, list) => {
// if is in hiddenlist hide it
if (id in hiddenList) { $(element).addClass('hidden') }
// add button to hide the script
$(element).find('.badge-js, .badge-css').before(`<span class="block-button" role="button" style="cursor: pointer; font-size: 70%;">${blockLabel($(element).hasClass('hidden'))}</span>`)
$(element).find('header h2').append(`<span class="block-button" role="button" style="cursor: pointer; font-size: 50%; margin-left: 1ex;">${blockLabel($(element).hasClass('hidden'))}</span>`)
// on click...
$(element).find('.block-button').click(async () => {
// ...if it is not in the list add it and hide it...
if (!(id in hiddenList)) {
hiddenList[id] = id
GM.setValue('hiddenList', JSON.stringify(hiddenList))
if (list) {
$(element).hide(750).addClass('hidden').find('.block-button').text(blockLabel($(element).hasClass('hidden')))
} else {
$(element).addClass('hidden').find('.block-button').text(blockLabel($(element).hasClass('hidden')))
}
} else { // ...else remove it
delete hiddenList[id]
GM.setValue('hiddenList', JSON.stringify(hiddenList))
$(element).removeClass('hidden').find('.block-button').text(blockLabel($(element).hasClass('hidden')))
}
})
}
/**
* Shows a button to install the script
*
* @param {object} element Script
* @param {string} url Script URL
* @param {string} label Label
* @param {string} version Script version
*/
const addInstallButton = (element, url, label, version) => {
$(element)
.find('.badge-js, .badge-css')
.after(`<a class="install-link" href="${url}" style="float: right; zoom: 0.7; -moz-transform: scale(0.7); text-decoration: none;">${label} ${version}</a>`)
}
//* Script
$(document).ready(async () => {
addSettings()
if (config.get('hideNonLatinScripts') || config.get('hideBlacklistedScripts') || config.get('hideScript') || config.get('installButton')) {
if (config.get('hideNonLatinScripts') || config.get('hideBlacklistedScripts') || config.get('hideScript')) {
addOptions()
$('head').append('<style>.script-list li.non-latin, .script-list li.blacklisted, .script-list li.hidden { display: none; background: rgb(50, 25, 25); color: rgb(232, 230, 227); } .script-list li.non-latin a:not(.install-link), .script-list li.blacklisted a:not(.install-link), .script-list li.hidden a:not(.install-link) { color: rgb(255, 132, 132); } #script-info.hidden, #script-info.hidden .user-content { background: rgb(50, 25, 25); color: rgb(232, 230, 227); } #script-info.hidden a:not(.install-link):not(.install-help-link) { color: rgb(255, 132, 132); } #script-info.hidden code { background-color: transparent; }</style>')
}
$('.script-list').find('li').each(async (index, element) => {
const scriptID = $(element).data('script-id')
if (config.get('hideNonLatinScripts')) hideNonLatinScripts(element)
if (config.get('hideBlacklistedScripts')) hideBlacklistedScripts(element)
if (config.get('hideScript')) hideScript(element, scriptID, true)
if (config.get('installButton')) {
const script = await getScriptData(scriptID).then()
const installed = await isInstalled(script.name, script.namespace).then()
const update = compareVersions(script.version, installed)
const label = installLabel(update)
addInstallButton(element, script.code_url, label, script.version)
}
})
if (config.get('hideScript') && $('#script-info').length > 0) {
const id = $('#script-info').find('.install-link').data('script-id')
hideScript($('#script-info'), id, false)
}
}
if (config.get('showTotalInstalls') && $('#user-script-list').length > 0) {
const dailyInstalls = []
const totalInstalls = []
$('#user-script-list').find('li dd.script-list-daily-installs').each((index, element) => {
dailyInstalls.push(Number.parseInt($(element).text().replace(/\D/g, ''), 10))
})
$('#user-script-list').find('li dd.script-list-total-installs').each((index, element) => {
totalInstalls.push(Number.parseInt($(element).text().replace(/\D/g, ''), 10))
})
$('#script-list-sort').find('.list-option.list-current:nth-child(1), .list-option:not(list-current):nth-child(1) a').append(`<span> (${dailyInstalls.reduce((a, b) => a + b, 0).toLocaleString()})</span>`)
$('#script-list-sort').find('.list-option.list-current:nth-child(2), .list-option:not(list-current):nth-child(2) a').append(`<span> (${totalInstalls.reduce((a, b) => a + b, 0).toLocaleString()})</span>`)
}
if (config.get('milestoneNotification')) {
const userID = $('.user-profile-link a').attr('href')
const milestones = config.get('milestoneNotification').replace(/\s/g, '').split(',').map((element) => Number(element))
if (!userID) return
const userData = await getUserData(userID.match(/\d+(?=\D)/g)).then()
const totalInstalls = await getTotalInstalls(userData).then()
const lastMilestone = await GM.getValue('lastMilestone', 0)
const milestone = $($.grep(milestones, (milestone) => totalInstalls >= milestone)).get(-1)
UU.log(`total installs are "${totalInstalls}", milestone reached is "${milestone}", last milestone reached is "${lastMilestone}"`)
if (milestone <= lastMilestone) return
GM.setValue('lastMilestone', milestone)
const text = (locales[lang] ? locales[lang].milestone : locales.en.milestone).replace('$1', milestone.toLocaleString())
if (GM.info.scriptHandler !== 'Userscripts') { //! Userscripts Safari: GM.notification is missing
GM.notification({
text,
title: GM.info.script.name,
image: logo,
onclick: () => {
window.location = `https://${window.location.hostname}${userID}#user-script-list-section`
}
})
} else {
UU.alert(text)
}
}
})
})()