// ==UserScript==
// @name get-gaoding-material
// @namespace get-gaoding-material
// @version 1.0
// @license MIT
// @icon https://gd-filems.dancf.com/gaoding/gaoding/0/c012b037-b933-4364-96d0-c7ed4e1588984217369.png
// @description 获取搞定设计画布所有素材
// @author sertraline
// @match https://www.gaoding.com/design*
// @require https://cdn.staticfile.org/jquery/3.6.2/jquery.min.js
// @grant GM_openInTab
// @grant GM_download
// @downloadURL https://update.greasyfork.icu/scripts/456745/get-gaoding-material.user.js
// @updateURL https://update.greasyfork.icu/scripts/456745/get-gaoding-material.meta.js
// ==/UserScript==
const SOURCE_IMG_SELECTOR = '.design-editor img'
const BTN_BOX_SELECTOR = `.editor-right-actions`
const NAMESPACE = GM_info.script.namespace
function uuid() {
return Math.random().toString(36).substr(2)
}
function namespace(str) {
return `${NAMESPACE}-${str}`
}
let toasts = [];
let $toastContainer;
const TOAST_CONTAINER_SELECTOR = `#${namespace("toast")}`;
function initToast() {
if (!$toastContainer) {
$toastContainer = $(TOAST_CONTAINER_SELECTOR);
if (!$toastContainer.length) {
const toast = `
`;
$toastContainer = $(toast);
$("body").append($toastContainer);
}
}
}
function openToast(text, duration = 2000) {
if (!text) return;
initToast();
const id = namespace(uuid + "toast")
const toast = {
id,
element: $.parseHTML(`
${text}
`),
};
toasts.push(toast);
$toastContainer.append(toast.element);
setTimeout(() => {
$(toast.element).remove();
toasts = toasts.filter((t) => t.id !== toast.id);
}, duration);
}
let btnBoxLoaded = false
function createBtn(id, text, callback) {
const element = document.querySelector(BTN_BOX_SELECTOR)
if (!element) {
const observer = new MutationObserver((mutationsList, observer) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
const element = document.querySelector(BTN_BOX_SELECTOR)
if (element) {
observer.disconnect()
btnBoxLoaded = true
createBtn(id, text, callback)
}
}
}
})
observer.observe(document.body, {
childList: true
})
return
}
const _id = namespace(id)
if (document.getElementById(_id)) return
const btn = `
${text}
`
$(BTN_BOX_SELECTOR).append(btn)
$(`#${_id}`).click(callback)
}
function createDrawer() {
const drawer = `
`
$('body').append(drawer)
$(`#${namespace('drawer__mask')}`).click(function () {
hideDrawer()
})
$(`#${namespace('drawer__content')}`).click(function (e) {
const target = e.target
if ($(target).hasClass(namespace('drawer__pic')) || $(target).parent().hasClass(namespace('drawer__pic'))) {
const src = $(target).find('img').attr('src') || $(target).attr('src')
if (src.indexOf('http') !== 0) {
GM_openInTab(src, { active: true })
return
}
downloadImg(src)
}
})
hideDrawer()
}
function clearDrawer() {
const _contentId = namespace('drawer__content')
if (!$(`#${_contentId}`).length) return
$(`#${_contentId}`).empty()
}
function hideDrawer() {
const _drawerId = namespace('drawer')
if (!$(`#${_drawerId}`).length) return
$(`#${_drawerId}`).fadeOut()
}
function showDrawer() {
const _drawerId = namespace('drawer')
if (!$(`#${_drawerId}`).length) return
$(`#${_drawerId}`).fadeIn()
}
function updateDrawerInfo() {
const len = $(`#${namespace('drawer__content')}`).children().length
$(`#${namespace('drawer__info')}`).text(`共${len}个素材`)
}
function pushImgItem(src) {
const img = `
${getFileTypeFromUrl(src) || '未知'}
`
$(`#${namespace('drawer__content')}`).append(img)
}
function getNameFromUrl(url) {
const SPLIT_REGEXP = /\/|\?/
return new URL(url).pathname.split(SPLIT_REGEXP).pop()
}
function getFileTypeFromUrl(url) {
const name = getNameFromUrl(url)
const SPLIT_REGEXP = /\./
const res = name.split(SPLIT_REGEXP)[1]
return res ? res.toUpperCase() : null
}
function downloadImg(url) {
const name = getNameFromUrl(url)
GM_download({
url,
name,
saveAs: true,
onload: () => {
openToast('下载成功')
},
onerror: () => {
openToast('下载失败')
}
})
}
; (function ($) {
initStyles()
createDrawer()
createBtn(
'get-material',
`获取素材`,
function () {
const sourceImgEls = document.querySelectorAll(SOURCE_IMG_SELECTOR)
if (!sourceImgEls.length) {
openToast('请等待页面加载完成')
return
}
clearDrawer()
sourceImgEls.forEach(el => {
pushImgItem(el.src)
})
updateDrawerInfo()
showDrawer()
openToast('素材获取成功')
}
)
})(jQuery)
function initStyles() {
const styles = `
`
$('head').append(styles)
}