GWT アプリから Disqus を使用する必要があったため、アプリ内の仮想ページが変更されたときにスレッドをオンデマンドでロードするという問題を解決する必要がありました。
少量のリバース エンジニアリングと実験により、ユーティリティ クラスを作成しました (以下を参照)。
主な洞察は次のとおりです。
disqus_container_id
コメントを好きな場所に配置できる、ドキュメント化されていないグローバル パラメータが呼び出されます。将来のバージョンでそれが機能しない場合、私のフォールバックは一時的にターゲット要素の id を に設定しdisqus_thread
、コメントを追加してから元の id に戻すことでした。
- これは JSNI を使用して GWT 用に開発されていたので、元のウィンドウ コンテキストでグローバル パラメータを設定する必要がありました
$wnd
。それに応じて、デフォルトの Disqus 埋め込みコードを変更しました。すべてのグローバル変数が Window オブジェクトにあることを以前は知りませんでしたが、新しいことを学びました。
- 同じコンテナを再利用できます。Disqus はアクティブ化すると中身がクリアされるようです。
- これにより、DOM に script タグのコピーが多数残ります。これらも使い終わったらきれいに掃除したほうがいいかもしれません。
DISQUS.reset
または、他の回答で説明されている方法を使用して、さらに実験を行うこともできます。
JS を単独で使用している人にとって重要な情報だけを抽出すると、好きな場所に Disqus スレッドを貼り付けることができます。
function loadComments(container_id, shortname, identifier, developer) {
// CONFIGURATION VARIABLES
window.disqus_container_id = container_id;
window.disqus_developer = developer ? 1 : 0;
window.disqus_shortname = shortname; // required
if (identifier) window.disqus_identifier = identifier;
// DON'T EDIT BELOW THIS LINE
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
}
そして、これが完全な GWT ユーティリティ クラスです。これまでのところ、必要なパラメーターのみを実装しました。
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.Widget;
public class Disqus {
public static boolean developer = false;
public static String shortname;
public static void showComments(Widget where, String identifier) {
showComments(where.getElement(), identifier);
}
public static void showComments(Element where, String identifier) {
if (shortname == null)
throw new IllegalArgumentException(
"You must specify the disqus shortname before displaying comments");
// Store the original id of the target element
String id = where.getId();
if (id == null) {
id = "disqus-" + Integer.toHexString(Random.nextInt());
where.setId(id);
}
// Update the id temporarily
where.setId("disqus_thread");
// Load the comments
loadComments(id, shortname, identifier, developer);
}
private static native void loadComments(String container_id, String shortname, String identifier, boolean developer) /*-{
// CONFIGURATION VARIABLES
$wnd.disqus_container_id = container_id;
$wnd.disqus_developer = developer ? 1 : 0;
$wnd.disqus_shortname = shortname; // required
if (identifier) $wnd.disqus_identifier = identifier;
// TODO
// disqus_url
// disqus_title
// disqus_category_id
// DON'T EDIT BELOW THIS LINE (sorry, I've edited it anyway)
(function() {
var dsq = $doc.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + shortname + '.disqus.com/embed.js';
($doc.getElementsByTagName('head')[0] || $doc.getElementsByTagName('body')[0]).appendChild(dsq);
})();
}-*/;
}