2020-11-28 07:57:11 +11:00
|
|
|
|
|
|
|
var plugins = getPlugin()
|
|
|
|
function getPlugin() {
|
|
|
|
const json = localStorage.getItem('plugins')
|
|
|
|
let ret = {
|
|
|
|
buttonOnPostbox: [],
|
2021-01-09 23:07:59 +11:00
|
|
|
buttonOnToot: [],
|
|
|
|
buttonOnBottom: [],
|
|
|
|
init: [],
|
|
|
|
tips: [],
|
|
|
|
none: []
|
2020-11-28 07:57:11 +11:00
|
|
|
}
|
2021-01-09 23:07:59 +11:00
|
|
|
if (!json) return ret
|
2020-11-28 09:07:16 +11:00
|
|
|
const plugins = JSON.parse(json)
|
2020-11-28 07:57:11 +11:00
|
|
|
for (let plugin of plugins) {
|
|
|
|
const meta = getMeta(plugin.content)
|
|
|
|
if (!meta) continue
|
|
|
|
const type = meta.event
|
|
|
|
ret[type] ? ret[type].push(plugin) : ret[type] = [plugin]
|
2021-01-09 23:07:59 +11:00
|
|
|
if (type === 'buttonOnToot') continue
|
|
|
|
if (type === 'tips') {
|
|
|
|
if (meta.interval) {
|
|
|
|
const matchCID = /custom:([abcdef0-9]{8}-[abcdef0-9]{4}-4[abcdef0-9]{3}-[abcdef0-9]{4}-[abcdef0-9]{12})/
|
|
|
|
setInterval(function () {
|
|
|
|
const tipsName = localStorage.getItem('tips')
|
|
|
|
if (tipsName.match(matchCID)) {
|
|
|
|
const id = tipsName.match(matchCID)[1]
|
|
|
|
if (id === plugin.id) if (location.href.split('/').pop() === 'index.html') execPlugin(id, 'tips', null)
|
|
|
|
}
|
|
|
|
|
|
|
|
}, meta.interval)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
const shortcut = meta.shortcut
|
|
|
|
$(window).keydown(function (e) {
|
|
|
|
if (e.keyCode === shortcut && e.altKey) execPlugin(plugin.id, type)
|
|
|
|
})
|
2020-11-28 07:57:11 +11:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
function initPlugin() {
|
|
|
|
asCommon['TheDesk:dialog'] = asValue.FN_NATIVE((z) => {
|
|
|
|
Swal.fire({
|
|
|
|
title: z[0].value,
|
|
|
|
icon: z[2] ? z[2].value : 'info',
|
|
|
|
text: z[1] ? z[1].value : ''
|
|
|
|
})
|
|
|
|
})
|
|
|
|
asCommon['TheDesk:confirm'] = asValue.FN_NATIVE(async (z) => {
|
|
|
|
const alert = await Swal.fire({
|
|
|
|
title: z[0].value,
|
|
|
|
text: z[1].value,
|
|
|
|
icon: z[2] ? z[2].value : 'info',
|
|
|
|
showCancelButton: true
|
|
|
|
})
|
|
|
|
return asUtil.jsToVal(!!(alert.value && alert.value === true))
|
|
|
|
})
|
|
|
|
asCommon['TheDesk:css'] = asValue.FN_NATIVE((z) => {
|
|
|
|
$(escapeHTML(z[0].value)).css(escapeHTML(z[1].value), escapeHTML(z[2].value))
|
|
|
|
})
|
2021-01-09 23:07:59 +11:00
|
|
|
asCommon['TheDesk:openLink'] = asValue.FN_NATIVE((z) => {
|
|
|
|
postMessage(['openUrl', z[0].value], '*')
|
2020-11-28 07:57:11 +11:00
|
|
|
})
|
2021-01-09 23:07:59 +11:00
|
|
|
|
|
|
|
const { buttonOnPostbox, init, buttonOnBottom, tips } = plugins
|
2020-11-28 07:57:11 +11:00
|
|
|
for (let target of buttonOnPostbox) {
|
|
|
|
const meta = getMeta(target.content)
|
|
|
|
$('#dropdown2').append(`<li><a onclick="execPlugin('${target.id}','buttonOnPostbox', null);">${escapeHTML(meta.name)}</a></li>`)
|
|
|
|
}
|
2021-01-09 23:07:59 +11:00
|
|
|
for (let target of buttonOnBottom) {
|
|
|
|
const meta = getMeta(target.content)
|
|
|
|
$('#group .btnsgroup').append(`<a onclick="execPlugin('${target.id}','buttonOnBottom', null);" class="nex waves-effect pluginNex"><span title="${escapeHTML(meta.name)}">${escapeHTML(meta.name).substr(0, 1)}</span></a>`)
|
|
|
|
}
|
|
|
|
for (let target of tips) {
|
|
|
|
const meta = getMeta(target.content)
|
|
|
|
$('#tips-menu .btnsgroup').append(`<a onclick="tips('custom', '${target.id}')" class="nex waves-effect pluginNex"><span title="${escapeHTML(meta.name)}">${escapeHTML(meta.name).substr(0, 1)}</span></a>`)
|
|
|
|
}
|
2020-11-28 07:57:11 +11:00
|
|
|
for (let target of init) {
|
|
|
|
const as = new AiScript(asCommon)
|
|
|
|
const meta = getMeta(target.content)
|
|
|
|
M.toast({ html: `${escapeHTML(meta.name)}を実行しました`, displayLength: 1000 })
|
|
|
|
if (target) as.exec(asParse(target.content))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function getMeta(plugin) {
|
|
|
|
try {
|
|
|
|
return AiScript.collectMetadata(asParse(plugin)).get(null)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function execPlugin(id, source, args) {
|
|
|
|
const coh = plugins[source]
|
|
|
|
let exe = null
|
|
|
|
for (let plugin of coh) {
|
|
|
|
if (plugin.id == id) {
|
|
|
|
exe = plugin.content
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const common = _.cloneDeep(asCommon)
|
|
|
|
if (source == 'buttonOnToot') {
|
|
|
|
common.DATA = args
|
|
|
|
const domain = localStorage.getItem(`domain_${args.acct_id}`)
|
|
|
|
const at = localStorage.getItem(`acct_${args.acct_id}_at`)
|
|
|
|
const start = `https://${domain}/api/v1/statuses/${args.id}`
|
|
|
|
const promise = await fetch(start, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
Authorization:
|
|
|
|
`Bearer ${at}`
|
|
|
|
}
|
|
|
|
})
|
2021-01-09 23:07:59 +11:00
|
|
|
let json = await promise.json()
|
2020-11-28 07:57:11 +11:00
|
|
|
common.TOOT = asUtil.jsToVal(json)
|
|
|
|
common['TheDesk:changeText'] = asValue.FN_NATIVE((z) => {
|
2021-01-09 23:07:59 +11:00
|
|
|
const v = sanitizeHtml(z[0].value,
|
|
|
|
{
|
|
|
|
allowedTags: ['p', 'br', 'a', 'span'],
|
|
|
|
allowedAttributes: {
|
|
|
|
'a': ['href', 'class', 'rel', 'target'],
|
|
|
|
'span': [],
|
|
|
|
'p': [],
|
|
|
|
'br': [],
|
|
|
|
}
|
|
|
|
}).replace(/href="javascript:/, 'href="').replace(/href='javascript:/, 'href="').replace(/href=javascript:/, 'href="')
|
|
|
|
json.content = v
|
|
|
|
if (getMeta(exe).dangerHtml) $(`[unique-id=${args.id}] .toot`).html(parse([json], null, null, null, null, null, null, true))
|
2020-11-28 07:57:11 +11:00
|
|
|
})
|
|
|
|
} else if (source == 'buttonOnPostbox') {
|
|
|
|
const postDt = post(null, false, true)
|
|
|
|
common.POST = asUtil.jsToVal(postDt)
|
|
|
|
common.ACCT_ID = asUtil.jsToVal(postDt.TheDeskAcctId)
|
|
|
|
common['TheDesk:postText'] = asValue.FN_NATIVE((z) => {
|
|
|
|
$('#textarea').val(z[0].value)
|
|
|
|
})
|
|
|
|
common['TheDesk:postCW'] = asValue.FN_NATIVE((z) => {
|
|
|
|
if (z[1]) $('#cw-text').val(z[1].value)
|
|
|
|
cw(z[0] ? z[0].value : false)
|
|
|
|
})
|
|
|
|
common['TheDesk:postNSFW'] = asValue.FN_NATIVE((z) => {
|
|
|
|
nsfw(z[0] ? z[0].value : false)
|
|
|
|
})
|
|
|
|
common['TheDesk:postVis'] = asValue.FN_NATIVE((z) => {
|
|
|
|
vis(z[0].value)
|
|
|
|
})
|
|
|
|
common['TheDesk:postClearbox'] = asValue.FN_NATIVE(() => {
|
|
|
|
clear()
|
|
|
|
})
|
|
|
|
common['TheDesk:postExec'] = asValue.FN_NATIVE(() => {
|
|
|
|
if (getMeta(exe).apiPost) post()
|
|
|
|
})
|
2021-01-09 23:07:59 +11:00
|
|
|
} else if (source == 'tips') {
|
|
|
|
common['TheDesk:refreshTipsView'] = asValue.FN_NATIVE((z) => {
|
|
|
|
const v = sanitizeHtml(z[0].value,
|
|
|
|
{
|
|
|
|
allowedTags: ['p', 'br', 'a', 'span', 'img'],
|
|
|
|
allowedAttributes: {
|
|
|
|
'a': ['href', 'class', 'rel', 'target', 'style'],
|
|
|
|
'span': ['style'],
|
|
|
|
'p': ['style'],
|
|
|
|
'br': [],
|
|
|
|
'img': ['src', 'style']
|
|
|
|
}
|
|
|
|
}).replace(/href="javascript:/, 'href="').replace(/href='javascript:/, 'href="').replace(/href=javascript:/, 'href="')
|
|
|
|
if (getMeta(exe).dangerHtml) $('#tips-text').html(v)
|
|
|
|
})
|
2020-11-28 07:57:11 +11:00
|
|
|
}
|
|
|
|
common['TheDesk:console'] = asValue.FN_NATIVE((z) => {
|
|
|
|
console.log(z[0].value)
|
|
|
|
})
|
2021-01-09 23:07:59 +11:00
|
|
|
common['TheDesk:api'] = asValue.FN_NATIVE(async (z) => {
|
|
|
|
try {
|
|
|
|
if (!getMeta(exe).apiGet && z[0].value == "GET") return asUtil.jsToVal(null)
|
|
|
|
if (!getMeta(exe).apiPost && (z[0].value == "POST" || z[0].value == "DELETE" || z[0].value == "PUT")) return asUtil.jsToVal(null)
|
|
|
|
const domain = localStorage.getItem(`domain_${z[3].value}`)
|
|
|
|
const at = localStorage.getItem(`acct_${z[3].value}_at`)
|
|
|
|
const start = `https://${domain}/api/${z[1].value}`
|
|
|
|
const q = {
|
|
|
|
method: z[0].value,
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
Authorization:
|
|
|
|
`Bearer ${at}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (z[2]) q.body = z[2].value
|
|
|
|
const promise = await fetch(start, q)
|
|
|
|
const json = await promise.json()
|
|
|
|
return asUtil.jsToVal(json)
|
|
|
|
} catch (e) {
|
|
|
|
return asUtil.jsToVal(null)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
common['TheDesk:getRequest'] = asValue.FN_NATIVE(async (z) => {
|
|
|
|
try {
|
|
|
|
if (!getMeta(exe).apiGet) return asUtil.jsToVal(null)
|
|
|
|
const start = `https://${z[0].value}`
|
|
|
|
const promise = await fetch(start)
|
|
|
|
let json = null
|
|
|
|
if (z[1].value) {
|
|
|
|
json = await promise.json()
|
|
|
|
} else {
|
|
|
|
json = await promise.text()
|
|
|
|
}
|
|
|
|
return asUtil.jsToVal(json)
|
|
|
|
} catch (e) {
|
|
|
|
return asUtil.jsToVal(null)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2020-11-28 07:57:11 +11:00
|
|
|
const as = new AiScript(common)
|
|
|
|
if (exe) as.exec(asParse(exe))
|
|
|
|
}
|