Skip to content

Instantly share code, notes, and snippets.

@tar-bin
Created March 15, 2023 08:22
Show Gist options
  • Save tar-bin/f94d54c187f7facb9cc4b6cb8376ad23 to your computer and use it in GitHub Desktop.
Save tar-bin/f94d54c187f7facb9cc4b6cb8376ad23 to your computer and use it in GitHub Desktop.
Misskeyのページ閲覧時に特定の単語を置換して表示するプラグインスクリプト
/// @ 0.12.4
///
/// こちらはノート向けの以下のスクリプト及び設定をページ向けに改変したものです
/// https://misskey.io/notes/9blgsec74k
/// https://nijimiss.moe/@akari_h/pages/1678003058099
///
/// 前提として以下のパッチがMisskey本体に取り込まれている必要があります。
/// https://github.com/misskey-dev/misskey/pull/10323
///
### {
name: "Word Replacer for Misskey Page"
version: "0.1.0"
author: "@tar_bin"
description: "ページ閲覧時に特定の単語を置換して表示します"
permissions: []
config: {
repls: {
type: 'string'
label: '置換リスト'
description: '例: ["old", "new"], ["旧", "新"] ...'
default: '["みょうじ", "漢字苗字"], ["なまえ", "漢字名前"], ["にゃまえ", "漢字名前"], ["ミョウジ", "カタカナ苗字"], ["ナマエ", "カタカナ名前"], ["ニャマエ", "カタカナ名前"]'
}
}
}
@validate(repls) {
if Core:type(repls) != 'arr' {
return [[] false]
}
let new_repls = []
var ok = true
each let r repls {
if Core:type(r) != 'arr' {
ok = false
continue
} elif r.len != 2 {
ok = false
continue
} elif Core:type(r[0]) != 'str' || Core:type(r[1]) != 'str' {
ok = false
continue
}
new_repls.push(r)
}
return [new_repls ok]
}
@replace(text repls) {
if Core:type(text) != 'str' {
return text
}
each let r repls {
text = text.replace(r[0] r[1])
}
return text
}
let a = validate(Json:parse(`[{Plugin:config.repls}]`))
let repls = a[0]
let ok = a[1]
@contentReplace(content) {
if content != null {
each let c content {
if c.type == "text" {
c.text = replace(c.text repls)
}
if c.type == "section" {
c.title = replace(c.title repls)
contentReplace(c.children)
}
}
}
}
@f(page) {
page.title = replace(page.title repls)
contentReplace(page.content)
return page
}
if !ok {
// Json:parseが例外落ちしたら機能しません
Mk:dialog('Word Replace' '置換リストの一部がうまく読み込めませんでした' 'error')
}
Plugin:register_page_view_interruptor(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment