瀏覽代碼

fixed compatibility with embed.js

Signed-off-by: Gerald <gera2ld@163.com>
Gerald 10 年之前
父節點
當前提交
89e8525c18
共有 3 個文件被更改,包括 21 次插入13 次删除
  1. 8 3
      README.md
  2. 12 9
      duoshuo-ua.js
  3. 1 1
      test.example.html

+ 8 - 3
README.md

@@ -25,12 +25,17 @@ duoshuoQuery.ondomready=function(){
 <html>
 <head>
 <link rel=stylesheet type=text/css href=duoshuo-ua.css />
+
+<!-- 第1部分:必须写在后2部分的前面 -->
 <script>var duoshuoQuery={short_name:'test'};</script>
-<!-- 下面的脚本加载顺序可以交换 -->
+
+<!-- 第2部分:加载duoshuo-ua.js后才能设置ondomready -->
 <script src=duoshuo-ua.js></script>
-<script src=http://static.duoshuo.com/embed.js></script>
-<!-- 加载duoshuo-ua.js后才能设置ondomready -->
 <script>duoshuoQuery.ondomready=duoshuoQuery.pluginUA(my_duoshuo_id);</script>
+
+<!-- 第3部分:可以和第2部分交换,也可以写成异步加载,但是必须写在第1部分后面 -->
+<script src=http://static.duoshuo.com/embed.js></script>
+
 ...
 </head>
 <body>

+ 12 - 9
duoshuo-ua.js

@@ -95,11 +95,14 @@ duoshuoQuery.pluginUA=function(duoshuo_id, getUAString){
 		r=getRule(a,rules_br);if(r[0]) {result.br=r[0];result.br_cls=r[1];}
 		return result;
 	}
-	function callBefore(local,e){
-		local.agent=parseAgent(e.post.agent);
-		local.webmaster=e.post.author_id==duoshuo_id;
+	function callBefore(local,args){
+		var e=args[0];
+		if(args.length==1)	// embed.unstable.js
+			e=e.post;
+		local.agent=parseAgent(e.agent);
+		local.webmaster=e.author_id==duoshuo_id;
 	}
-	function callAfter(local,e){
+	function callAfter(local,args){
 		var r=local.result,a=local.agent,
 				i=r.indexOf('<div class="ds-comment-header">'),
 				j=r.indexOf('</div>',i);
@@ -113,11 +116,11 @@ duoshuoQuery.pluginUA=function(duoshuo_id, getUAString){
 	};
 	return function(){
 		var post=DUOSHUO.templates.post;
-		DUOSHUO.templates.post=function(e){
-			var local={};
-			callBefore.call(this,local,e);
-			local.result=post.call(this,e);
-			callAfter.call(this,local,e);
+		DUOSHUO.templates.post=function(){
+			var local={},args=arguments;
+			callBefore.call(this,local,args);
+			local.result=post.apply(this,args);
+			callAfter.call(this,local,args);
 			return local.result;
 		}
 	};

+ 1 - 1
test.example.html

@@ -8,8 +8,8 @@
 	<body>
 		<script>var duoshuoQuery={short_name:'your_short_name'};</script>
 		<div class=ds-thread data-thread-key="your_key"></div>
-		<script src=duoshuo-ua.js></script>
 		<script src=http://static.duoshuo.com/embed.unstable.js></script>
+		<script src=duoshuo-ua.js></script>
 		<script>duoshuoQuery.ondomready=duoshuoQuery.pluginUA(1234567);</script>
 	</body>
 </html>