script.js// バックアップ用のプロジェクト名
let backupProject = "init";
if (scrapbox.Project.name==="suto3scratchpad") {
backupProject = "suto3";
} else {
//backupProject = "suto3suto3";
backupProject = "suto3scratchpad";
}
//console.log(backupProject)
const project = scrapbox.Project.name ;
const menu = 'SystemReport' ;
script.jsscrapbox.PageMenu.addMenu({
title: menu,
//image: 'https://gyazo.com/1e3180e3bcab79cd1187b3c24e0e2758/raw'
image: '/api/pages/suto3/UserScript:SystemReport/icon'
})
script.js// APIを叩いてページのリンクを拾ってくる
scrapbox.PageMenu(menu).addItem({
title: () => "Linked Page Report",
onClick: () => {
const project = scrapbox.Project.name
const { title, lines } = scrapbox.Page
const report="Report-" + title
const url = `https://scrapbox.io/api/pages/${project}/${title}`
fetch(url)
.then((response) => response.json())
.then((json) => {
// JSONデータを扱った処理など
//console.log(json)
const relatedPagesData = json["relatedPages"]
//console.log(relatedPagesData)
// 1 hop link の取得
const l1 = relatedPagesData["links1hop"]
.map(line => " [/"+project+"/"+line.title+"]")
.join('\n');
//console.log(l1);
// 2 hop link の取得
const l2 = relatedPagesData["links2hop"]
.map(line => " [/"+project+"/"+line.title+"]")
.join('\n');
//console.log(l2);
// ページ本文を組み立て
const now = new Date().toLocaleString()
const body =
"Project : [/" + project + "]\n"
+ "Page : [/" + project + "/" + title + "]\n"
+ "Date : " + now + "\n\n"
+ "1 hop link :\n" + l1
+ "\n\n"
+ "2 hop link :\n" + l2
+ "\nEOF\n"
//console.log(body)
// バックアップ用プロジェクトにレポートを作成
window.open(`https://scrapbox.io/${backupProject}/${report}?body=${encodeURIComponent(body)}`)
}).catch(err => {
console.error('fetch failed', err)
})
}
})
script.js// APIを叩いてページのリンクを拾ってくる
scrapbox.PageMenu(menu).addItem({
title: () => "Linked Page (Graphviz)",
onClick: () => {
const project = scrapbox.Project.name
const { title, lines } = scrapbox.Page
const report="Report-" + title
const url = `https://scrapbox.io/api/pages/${project}/${title}`
fetch(url)
.then((response) => response.json())
.then((json) => {
// JSONデータを扱った処理など
console.log(json)
const relatedPagesData = json["relatedPages"]
//console.log(relatedPagesData)
// 1 hop link の取得
const l1 = relatedPagesData["links1hop"]
//.map(line => ' "' + title + '" -> "' + line.titleLc + '";')
.map(line => ' "' + title + '" -> "' + line.title + '";')
.join('\n');
//console.log(l1);
// 2 hop link の取得
const l2 = relatedPagesData["links2hop"]
.map(line => ' "' + line.linksLc[0] + '" -> "' +line.titleLc + '";')
.join('\n');
//console.log(l2);
// ページ本文を組み立て
const now = new Date().toLocaleString()
const body =
"Project : [/" + project + "]\n"
+ "code:d3.js\n"
+ " const digraph = ` digraph g {\n"
+ " rankdir = LR;\n"
+ " ratio=auto;\n"
+ " overlap=false;\n"
+ " ranksep=1.1;\n"
+ l1 + "\n"
+ l2 + "\n"
+ " }`\n"
+ " d3.select(\"#graph\").graphviz()\n"
+ " .fade(false)\n"
+ " .renderDot(digraph);\n\n"
+ `https://suto3.github.io/rund3?code=https://scrapbox.io/api/code/${backupProject}/${report}/d3.js`
+ "\nEOF\n"
console.log(body)
// バックアップ用プロジェクトにレポートを作成
//window.open(`https://scrapbox.io/${backupProject}/${report}?body=${encodeURIComponent(body)}`)
}).catch(err => {
console.error('fetch failed', err)
})
}
})
script.js// 直近に変更されたページのリストを取得
scrapbox.PageMenu(menu).addItem({
title: 'Top20',
onClick: () => {
const report="Top20-" + scrapbox.Project.name
const top = scrapbox.Project.pages
.filter(page => page.exists)
.slice(0, 20)
.map(line => " [/"+project+"/"+line.title +"]")
.join('\n')
// ページ本文を組み立て
const now = new Date().toLocaleString()
const body =
"Project : [/" + project + "]\n"
+ "Date : " + now + "\n\n"
+ top
+ "\nEOF\n"
//console.log(body);
// バックアップ用プロジェクトにレポートを作成
window.open(`https://scrapbox.io/${backupProject}/${report}?body=${encodeURIComponent(body)}`)
}
})
script.js // ページリストの底のほうを取得
scrapbox.PageMenu(menu).addItem({
title: 'Bottom20',
onClick: () => {
const report="Bottom20-" + scrapbox.Project.name
const bottom = scrapbox.Project.pages
.filter(page => page.exists)
.slice(-20)
.map(line => " [/"+project+"/"+line.title +"]")
.join('\n')
// ページ本文を組み立て
const now = new Date().toLocaleString()
const body =
"Project : [/" + project + "]\n"
+ "Date : " + now + "\n\n"
+ bottom
+ "\nEOF\n"
//console.log(body);
// バックアップ用プロジェクトにレポートを作成
window.open(`https://scrapbox.io/${backupProject}/${report}?body=${encodeURIComponent(body)}`)
}
})
script.js // ページリストをパターンマッチングで拾ってくる
// パターンマッチングの対象は、ページタイトルのみ
scrapbox.PageMenu(menu).addItem({
title: 'Pagename',
onClick: () => {
const report="Pattern-" + scrapbox.Project.name
const max = 200 // 最大取得件数
const pat = prompt(
"検索パターンを入力してください\n"
+ "最大 " + max + " 件まで取得します\n"
+ "何も入力しなければキャンセルします"
)
if (pat === undefined || pat === null) return
if (!(0 < pat.length && pat.length < 101)) return
//console.log(pat);
const pagelist = scrapbox.Project.pages
.filter(page => page.exists)
.filter(line => line.title.match(pat))
//console.log(pagelist);
if (pagelist.length > max) {
const ret = confirm(
"件数が " + max + " 件を超えています。\n"
+ "続行しますか? (OKで強制続行)"
)
if(!ret) return
}
// ページ本文を組み立て
const now = new Date().toLocaleString()
const body =
"Project : [/" + project + ": "+ pat +"]\n"
+ "Date : " + now + "\n\n"
+ "Pattern : " + pat + "\n\n"
+ pagelist.map(line => " [/"+project+"/"+line.title +"]").join('\n')
+ "\nEOF\n"
//console.log(body);
// バックアップ用プロジェクトにレポートを作成
window.open(`https://scrapbox.io/${backupProject}/${report}?body=${encodeURIComponent(body)}`)
}
})
script.js scrapbox.PageMenu(menu).addItem({
title: 'Page Info',
onClick: () => {
const url = '/api/pages/'
+ scrapbox.Project.name + '/'
+ scrapbox.Page.title
//console.log(url)
fetch(url)
.then(response => response.json())
.then(json => {
console.log(json)
// 文字数
const chars = scrapbox.Page.lines
.map(line => line.text.length)
.reduce((a,b) => a + b)
const created = new Date(json.created * 1000).toLocaleString()
//console.log(created)
const updated = new Date(json.updated * 1000).toLocaleString()
window.alert(
"ページ情報\n"
+ "プロジェクト名 : " + scrapbox.Project.name + "\n"
+ "ページ名 : " + scrapbox.Page.title + "\n"
+ "ページ行数 : " + json.lines.length + "\n"
+ "ページ文字数 : " + chars + "\n"
+ "作成年月日 : " + created + "\n"
+ "更新年月日 : " + updated + "\n"
+ "作成者 : " + json.user.name + "\n"
+ "Views : " + json.views + "\n"
)
}).catch(err => {
console.error('fetch failed', err)
})
}
})