# プラグインで post-cooked.js.es6 のメソッドをオーバーライドする方法は？

**URL:** https://meta.discourse.org/t/how-to-override-a-method-in-post-cooked-js-es6-in-a-plugin/120613
**Category:** Development
**Created:** [2019 年 6 月 17 日午後 9:15 UTC](https://meta.discourse.org/t/how-to-override-a-method-in-post-cooked-js-es6-in-a-plugin/120613 "2019-06-17T21:15:37Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![spirobel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spirobel/32/170908_2.png) [@spirobel](https://meta.discourse.org/u/spirobel)
#### Post date: [2020 年 6 月 28 日午前 7:57 UTC](https://meta.discourse.org/t/how-to-override-a-method-in-post-cooked-js-es6-in-a-plugin/120613/5 "2020-06-28T07:57:32Z")

</div>

1 日ほどあれこれ試行錯誤した結果、この回答に行きつきました。

> [@A tour of how the Widget (Virtual DOM) code in Discourse works](https://meta.discourse.org/t/a-tour-of-how-the-widget-virtual-dom-code-in-discourse-works/40347/10?u=spirobel):
>
> Is it possible to mount a widget into a post from decorateCooked? The polls plugin seems to do some funky stuff with Ember components/controllers/templates, and a set of widgets seem like they might be easier to work with.

どうやらこのアプローチは無駄だったようで、別の方法を探る必要があります。  
`elem.innerHTML` に `&lt;script&gt;alert(1)&lt;/script&gt;` を設定すると、エスケープが解除されて `<script>alert(1)</script>` になってしまいます（作曲画面で入力している間は、エスケープされていない状態で入力するとプレビューで削除されるのが確認できます）。これは問題でしょうか、それとも CSP が阻止してくれるのでしょうか？  
[https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243!](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243!) [csp error|690x191](https://meta.discourse.org/uploads/short-url/10w9dF6kuG5hZOc2p9b07C6CpF8.png)

説明の一部としてスクリプトタグを含む投稿も、同様にこれらの CSP エラーを発生させているようです。今、本当に混乱しています。保存型 XSS を気にする必要はあるのでしょうか、それとも CSP が自動的にブロックしてくれるのでしょうか？作曲画面では self-XSS を防ぐために CKEditor を使用しています。もし気にする必要があるなら、安全でないタグを除去する必要があるようです。現時点では以下のようにしています。

```ruby
  value = Loofah.fragment(value).scrub!(:escape).to_s

```

しかし、`elem.innerHTML` にこの値を設定すると HTML エンティティがエスケープ解除されてしまうため、全く効果がありません。

編集：ついに混乱の原因を見つけました。要素の検査機能では実際の HTML は表示されません。HTML エンティティはすでに変換されて表示されてしまいます。

 ![confusion](https://global.discourse-cdn.com/meta/original/3X/9/d/9dfea6f92d4c575e3efb4097b16685647044f5dd.png)

インスペクターで「HTML として編集」をクリックすると、実際にはすべて正常であることが明確になります。目に見えないはずのタグがレンダリングされていることを目撃していた時点で、この方向に気づくべきでした。

---

_[View the full topic](https://meta.discourse.org/t/how-to-override-a-method-in-post-cooked-js-es6-in-a-plugin/120613)._
