Discourse の JavaScript API を使用すると、テーマやプラグインでユーザーエクスペリエンスを大幅にカスタマイズできます。これを使用する最も簡単な方法は、管理パネルから新しいテーマを作成し、「コードの編集」をクリックしてから、JS タブに移動することです。
ファイルベースのテーマの場合、API は api-initializers ディレクトリにファイルを作成することで使用できます。テーマの場合は {theme}/javascripts/api-initializers/init-theme.gjs、プラグインの場合は {plugin}/assets/javascripts/discourse/api-initializers/init-plugin.js となります。内容は次のとおりです。
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer((api) => {
// ここにコードを記述
});
利用可能なすべての API は、Discourse コアの plugin-api.gjs ソースコード に、簡単な説明と例とともにリストされています。
JS API の使用例を含む完全なチュートリアルについては、以下を確認してください。
This tutorial will teach you how to create a Discourse Theme or Theme Component from the ground up. While this topic assumes no previous experience working on Discourse themes, it does assume some prior experience using HTML , CSS and JavaScript . It’ll also help if you know your way around GitHub .
What are Discourse themes?
A theme or theme component is a set of files packaged together designed to either modify Discourse visually or to add new features.
Themes
In general, themes are not suppo…
このドキュメントはバージョン管理されています。変更の提案はgithub で行ってください。
「いいね!」 39
What’s the best way to “import” as a Site Customisation? - is it just to use require?
Whilst this works:
<script type="text/discourse-plugin" version="0.1">
var HamburgerMenuComponent = require('discourse/components/hamburger-menu').default;
</script>
This does not:
<script type="text/discourse-plugin" version="0.1">
import {default as HamburgerMenuComponent2 } from 'discourse/components/hamburger-menu';
</script>
Where I get this error:
<script type="text/discourse-js-error">unknown: 'import' and 'export' may only appear at the top level (3:0)
1 | Discourse._registerPluginCode('0.1', api => {
2 |
> 3 | import {default as HamburgerMenuComponent2 } from 'discourse/components/hamburger-menu';
| ^
4 |
5 |
6 | }); at <eval>:8695:14</script>
「いいね!」 2
eviltrout
(Robin Ward)
2016 年 3 月 30 日午前 5:05
3
Instead of importing it, you can look it up with
api.container.lookupFactory('component:hamburger-menu')
「いいね!」 5
Nuck
(Emma Lejeck)
2016 年 4 月 6 日午後 9:15
4
Is there a way to get access to the h helper from virtual-dom in a site customization? I’m trying to add a simple dropdown widget to use in our header, and I can’t get that darned h, even though I can get createWidget
Yuun
(Sam)
2016 年 4 月 6 日午後 9:22
5
Does:
h = require('virtual-dom').h;
not work?
「いいね!」 2
Nuck
(Emma Lejeck)
2016 年 4 月 6 日午後 9:25
6
Yes it does! Works perfectly, thanks a ton!
eviltrout
(Robin Ward)
2016 年 4 月 6 日午後 9:25
7
I wouldn’t recommend doing it that way as it would likely break future compatibility. You can use this for now but I’ll try to introduce a workaround shortly that will be safer long term.
「いいね!」 5
Nuck
(Emma Lejeck)
2016 年 4 月 6 日午後 9:29
8
Makes sense, it’s kinda circumventing the whole Plugin API thing and relying on implementation details of the ES6 compilation output, both things that are Dangerous™
Anyways, I’ll definitely keep an eye out for a better solution
eviltrout
(Robin Ward)
2016 年 4 月 6 日午後 9:29
9
I’ll try to get to it soon and I’ll reply in this topic, so watch it and you’ll see
「いいね!」 4
eviltrout
(Robin Ward)
2016 年 4 月 7 日午後 8:18
10
Actually thinking about it, the decorateWidget helper gets called with an object that has the h method. How are you inserting your widget if not via a decorator?
If you could post code that would be helpful.
Nuck
(Emma Lejeck)
2016 年 4 月 7 日午後 8:20
11
{{mount-widget}} in a template for a plugin outlet.
「いいね!」 1
eviltrout
(Robin Ward)
2016 年 4 月 7 日午後 8:30
12
Ah that’s clever – I didn’t think people would try that. Okay, let me try something out.
eviltrout
(Robin Ward)
2016 年 4 月 7 日午後 8:39
13
Okay, I’ve added h to the pluginApi object as long as you request plugin api v0.3:
<script type="text/discourse-plugin" version="0.3">
console.log(api.h('b', ['hello', 'world']));
</script>
That should work for you!
committed 08:38PM - 07 Apr 16 UTC
「いいね!」 9
[quote=“Discourse, post:1, topic:41281”]
`
「いいね!」 1
david
(David Taylor)
2025 年 5 月 27 日午後 3:19
18
こちらと他の多くのバージョン管理されたドキュメントを更新しました。お知らせありがとうございます @NateDhaliwal
「いいね!」 2
jenmck
(jen)
2025 年 11 月 15 日午前 1:04
19
このリンクは機能しなくなりました。次のメッセージが表示されます。
discourseのmainブランチには、app/assets/javascripts/discourse/app/lib/plugin-api.gjsのパスが含まれていません。
「いいね!」 2
これは、javascript ファイルが app/assets/javascripts/ ではなく frontend/ ディレクトリに移動されたためです。
PR を開きました。
main ← NateDhaliwal:plugin-api-file-moved-to-frontend-dir
opened 01:56AM - 15 Nov 25 UTC
This PR changes the link the the `plugin-api.gjs` file from the `app/assets/java… scripts/` directory to the `frontend/` directory.
「いいね!」 4
このPRを再浮上させます。レビューしていただけますか?ありがとうございます!
「いいね!」 1