/* eslint-disable no-multi-spaces */
// ==UserScript==
// @name Greasyfork 快捷编辑收藏
// @name:zh-CN Greasyfork 快捷编辑收藏
// @name:en Greasyfork add-favorite button
// @namespace Greasyfork-Favorite
// @version 0.1
// @description 在脚本信息页添加快速打开收藏集编辑页面功能
// @description:zh-CN 在脚本信息页添加快速打开收藏集编辑页面功能
// @description:en Add open script-set-edit-page button in script info page
// @author PY-DNG
// @license GPL-3
// @match http*://greasyfork.org/*
// @icon https://api.iowen.cn/favicon/get.php?url=greasyfork.org
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
// Arguments: level=LogLevel.Info, logContent, asObject=false
// Needs one call "DoLog();" to get it initialized before using it!
function DoLog() {
// Global log levels set
window.LogLevel = {
None: 0,
Error: 1,
Success: 2,
Warning: 3,
Info: 4,
}
window.LogLevelMap = {};
window.LogLevelMap[LogLevel.None] = {prefix: '' , color: 'color:#ffffff'}
window.LogLevelMap[LogLevel.Error] = {prefix: '[Error]' , color: 'color:#ff0000'}
window.LogLevelMap[LogLevel.Success] = {prefix: '[Success]' , color: 'color:#00aa00'}
window.LogLevelMap[LogLevel.Warning] = {prefix: '[Warning]' , color: 'color:#ffa500'}
window.LogLevelMap[LogLevel.Info] = {prefix: '[Info]' , color: 'color:#888888'}
window.LogLevelMap[LogLevel.Elements] = {prefix: '[Elements]', color: 'color:#000000'}
// Current log level
DoLog.logLevel = (unsafeWindow ? unsafeWindow.isPY_DNG : window.isPY_DNG) ? LogLevel.Info : LogLevel.Warning; // Info Warning Success Error
// Log counter
DoLog.logCount === undefined && (DoLog.logCount = 0);
if (++DoLog.logCount > 512) {
console.clear();
DoLog.logCount = 0;
}
// Get args
let level, logContent, asObject;
switch (arguments.length) {
case 1:
level = LogLevel.Info;
logContent = arguments[0];
asObject = false;
break;
case 2:
level = arguments[0];
logContent = arguments[1];
asObject = false;
break;
case 3:
level = arguments[0];
logContent = arguments[1];
asObject = arguments[2];
break;
default:
level = LogLevel.Info;
logContent = 'DoLog initialized.';
asObject = false;
break;
}
// Log when log level permits
if (level <= DoLog.logLevel) {
let msg = '%c' + LogLevelMap[level].prefix;
let subst = LogLevelMap[level].color;
if (asObject) {
msg += ' %o';
} else {
switch(typeof(logContent)) {
case 'string': msg += ' %s'; break;
case 'number': msg += ' %d'; break;
case 'object': msg += ' %o'; break;
}
}
console.log(msg, subst, logContent);
}
}
DoLog();
bypassXB();
GM_PolyFill('default');
// Inner consts with i18n
const CONST = {
Text: {
'zh-CN': {
FavEdit: '收藏集:',
Edit: '编辑',
CopySID: '复制脚本ID'
},
'en': {
FavEdit: 'Add to/Remove from favorite list: ',
Edit: 'Edit',
CopySID: 'Copy-Script-ID'
},
'default': {
FavEdit: 'Add to/Remove from favorite list: ',
Edit: 'Edit',
CopySID: 'Copy-Script-ID'
},
}
}
// Get i18n code
let i18n = navigator.language;
if (!Object.keys(CONST.Text).includes(i18n)) {i18n = 'default';}
main()
function main() {
const HOST = getHost();
const API = getAPI();
// Common actions
commons();
// API-based actions
switch(API[1]) {
case "scripts":
centerScript(API);
break;
default:
DoLog('API is {}'.replace('{}', API));
}
}
function centerScript(API) {
switch(API[3]) {
case undefined:
pageScript();
break;
case 'code':
pageCode();
break;
case 'feedback':
pageFeedback();
break;
}
}
function commons() {
// Your common actions here...
}
function pageScript() {
addFavPanel();
}
function pageCode() {
addFavPanel();
}
function pageFeedback() {
addFavPanel();
}
function addFavPanel() {
if (!getUserpage()) {return false;}
GUI();
function GUI() {
// Get elements
const script_after = $('#script-feedback-suggestion+*') || $('#new-script-discussion');
const script_parent = script_after.parentElement;
// My elements
const script_favorite = $C('div');
script_favorite.id = 'script-favorite';
script_favorite.style.margin = '0.75em 0';
script_favorite.innerHTML = CONST.Text[i18n].FavEdit;
const favorite_groups = $C('select');
favorite_groups.id = 'favorite-groups';
const stored_sets = GM_getValue('script-sets', {sets: []}).sets;
for (const set of stored_sets) {
// Make