Reputation: 1032
I want to add disqus to my android app. I traced and found the code for displaying the disqus comments with id and short name. But I am not able to find documents for adding the secret and public key. Then go forward.
Already registered in Disqus, got my private and public key. Now I need to know,
1. Where to add the keys to the app.
2. How do I create id mapping to a particular page(activity/fragment).
3. Where am I supposed to give the short name?
Upvotes: 1
Views: 4115
Reputation: 309
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
this.page.url = 'linkOFPage'
this.page.identifier = 'page_identifier';
};
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//forumName.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
then made activity as--
webDisqus = (WebView) findViewById(R.id.disqus);
//set up disqus
WebSettings webSettings2 = webDisqus.getSettings();
webSettings2.setJavaScriptEnabled(true);
webSettings2.setBuiltInZoomControls(true);
webDisqus.requestFocusFromTouch();
String htmlComments = getHtmlComment(url, "forumName");
webDisqus.setWebViewClient(new MyWebViewClient(htmlComments,"text/html", null, ""));
webDisqus.setWebChromeClient(new WebChromeClient());
webDisqus.loadDataWithBaseURL("http://forumName.disqus.com/",htmlComments,"text/html","UTF-8","");
public String getHtmlComment(String idPost, String shortName) {
return "<html><head></head><body><div id='disqus_thread'></div></body>" + "<script type='text/javascript'>"
+ "var disqus_identifier = '" + idPost + "';" + "var disqus_shortname = '" + shortName + "';"
+ "var disqus_config = function () {this.page.identifier='" + idPost + "';" + "}" + ";"
+ " (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;"
+ "dsq.src = '/embed.js';"
+ "(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();"
+ "</script></html>";
}
custom web view client file--
public class MyWebViewClient extends WebViewClient
{
private String html_comments, sType, obj, sMyURL;
public MyWebViewClient(String htmlComments, String string, String object, String URL) {
// TODO Auto-generated constructor stub
html_comments = htmlComments;
sType = string;
obj = object;
sMyURL = URL;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("page started", url);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("disqus error", "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]");
}
public void onPageFinished(WebView view, String url) {
if (url.indexOf("logout") > -1 || url.indexOf("disqus.com/next/login-success") > -1) {
view.loadDataWithBaseURL("http://forumName.disqus.com/", html_comments, "text/html", "UTF-8", null);
}
if (url.indexOf("disqus.com/_ax/twitter/complete") > -1 || url.indexOf("disqus.com/_ax/facebook/complete") > -1
|| url.indexOf("disqus.com/_ax/google/complete") > -1) {
view.loadUrl("file:///android_res/raw/login.php");
}
if (url.indexOf("file:///android_res/raw/login.php") > -1) {
view.loadDataWithBaseURL("http://forumName.disqus.com/", html_comments, "text/html", "UTF-8", null);
}}
finally i made a raw folder in res and saved ablank file by name login.php
hope this helps someone. thanks to globe totter
Upvotes: 0
Reputation: 81
@ndgreen, doesn't work anymore ! Cross domains security provide to load the external JS. We have to do this now :
String htmlComments = getHtmlComment("idPost", "youriddisqus");
yourWebView.loadDataWithBaseURL("http://youriddisqus.disqus.com/", htmlComments, "text/html", "UTF-8", "");
public String getHtmlComment(String idPost, String shortName) {
return "<html><head></head><body><div id='disqus_thread'></div></body>"
+ "<script type='text/javascript'>"
+ "var disqus_identifier = '"
+ idPost
+ "';"
+ "var disqus_shortname = '"
+ shortName
+ "';"
+ " (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;"
+ "dsq.src = '/embed.js';"
+ "(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();"
+ "</script></html>";
}
And voila !
Upvotes: 5