js-script-button
JavaScript を実行する
↓アイコン画像
https://gyazo.com/69c812cab9c00c30353a10cef12c4ab9
code:button.js
(function () {
let jsscript = document.getElementById('__JSsc__')
if (jsscript){
jsscript.parentNode.removeChild(jsscript);
//console.log("nop")
// リロードしてリセット(暫定処置)
location.reload(false)
alert("removed");
} else {
jsscript = document.createElement("script");
jsscript.id = '__JSsc__'
jsscript.src = https://scrapbox.io/api/code//${scrapbox.Project.name}/${encodeURIComponent(scrapbox.Page.title)}/script.js
document.body.appendChild(jsscript);
}
})()
code:icon-button.js
export async function execute() {
const jsScript = new JSScript('__JSsc__');
if(jsScript.hasScript()) {
jsScript.remove();
// リロードしてリセット(暫定処置)
location.reload(false)
alert("removed");
return;
}
jsScript.draw()
}
class JSScript {
constructor(id) {
this.id = id;
}
draw() {
const jsscript = document.createElement("script");
jsscript.id = this.id;
//console.log(this.id);
jsscript.src = https://scrapbox.io/api/code/${scrapbox.Project.name}/${encodeURIComponent(scrapbox.Page.title)}/script.js ;
document.body.appendChild(jsscript);
//console.log(jsscript);
}
hasScript() { return document.getElementById(this.id) !== null;}
remove() {
//console.log('remove');
const jsscript = document.getElementById(this.id);
if (!jsscript) return;
// 既にscriptがある場合は削除する
jsscript.parentNode.removeChild(jsscript);
console.log('The script already exists. Removing it...');
}
}
code:script.js
alert("Hello, world!!");
js-script-button.icon←実行ボタン
UserScript.icon