script.jsconst projects = new Promise((resolve, reject) => {
fetch('https://scrapbox.io/api/projects')
.then(response => response.json())
.then(json => json.projects)
.then(resolve)
.catch(reject)
})
const rescrap = project => {
const { title, lines } = scrapbox.Page
const body = lines.reduce((acc, { text }) => `${acc}\n${text}`, '').trim() + `\n\nfrom [/${scrapbox.Project.name}/${title}]\n`
const link = `https://scrapbox.io/${encodeURIComponent(project)}/new?body=${encodeURIComponent(body)}`
console.log(body)
window.open(link, '_blank', 'noopener')
}
const rescrapProjectFilterInput = document.createElement('input')
rescrapProjectFilterInput.type = 'text'
rescrapProjectFilterInput.id = 'rescrap-filtering-input'
rescrapProjectFilterInput.addEventListener('input', async e => {
scrapbox.PageMenu('re-scrap').removeAllItems()
const query = rescrapProjectFilterInput.value.toLowerCase()
for (const { name, displayName } of await projects) {
if (!name.toLowerCase().includes(query) && !displayName.toLowerCase().includes(query)) continue
scrapbox.PageMenu('re-scrap').addItem({
title: displayName,
onClick: () => { rescrap(name) }
})
}
})
scrapbox.PageMenu.addMenu({
title: 're-scrap',
image: 'https://gyazo.com/024fe39c0328fabb99566c6ad743ee54/raw',
onClick: async () => {
scrapbox.PageMenu('re-scrap').addItem({ title: 'Now loading...', onClick: () => null })
await projects
scrapbox.PageMenu('re-scrap').removeAllItems()
for (const { name, displayName } of await projects) {
scrapbox.PageMenu('re-scrap').addItem({
title: displayName,
onClick: () => { rescrap(name) }
})
}
const ul = document.querySelector('ul[aria-labelledby="re-scrap"].dropdown-menu')
rescrapProjectFilterInput.value = ''
ul.prepend(rescrapProjectFilterInput)
rescrapProjectFilterInput.focus()
}
})