Skip to content

Instantly share code, notes, and snippets.

@nora-tetsu
Last active February 23, 2023 09:24
Show Gist options
  • Save nora-tetsu/aef2d3f5833eebcfe0358de3a8e191da to your computer and use it in GitHub Desktop.
Save nora-tetsu/aef2d3f5833eebcfe0358de3a8e191da to your computer and use it in GitHub Desktop.
Page Menuからstyle要素の追加・除去をトグルするUserScript
(function (){
scrapbox.PageMenu.addMenu({
title: '関連リンクを左に',
image: 'https://i.gyazo.com/7057219f5b20ca8afd122945b72453d3.png', // Scrapboxアイコン
onClick: () => {
const url = '/api/code/noratetsu/_CSS_related-page-list_left/style.css'; // CSSが書いてあるコードブロック
const styleId = 'related-left'; // 適当なidを設定する
const elm = document.getElementById(styleId);
if(elm){ // 既に設定されている時
elm.remove(); // 除去する
}else{ // 設定されていない時
fetch(url) // 指定したコードブロックの中身を取得
.then(response => response.text())
.then(text => {
// style要素にCSSの記述を入れてbody要素に追加する
const style = document.createElement('style');
style.id = styleId;
style.append(document.createTextNode(text));
document.body.append(style);
});
};
},
})
})();
@nora-tetsu
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment