Bläddra i källkod

added faisalman/ua-parser-js example

Signed-off-by: Gerald <gera2ld@163.com>
Gerald 10 år sedan
förälder
incheckning
cf951fd335
7 ändrade filer med 53 tillägg och 6 borttagningar
  1. 1 0
      .gitignore
  2. 3 1
      README.md
  3. 1 1
      dist/duoshuo-ua-with-css.min.js
  4. 1 1
      dist/duoshuo-ua.min.js
  5. 2 2
      src/duoshuo-ua.js
  6. 6 1
      test/example.html
  7. 39 0
      test/faisalman.html

+ 1 - 0
.gitignore

@@ -1,2 +1,3 @@
 test/test.html
+test/ua-parser.min.js
 node_modules/

+ 3 - 1
README.md

@@ -31,6 +31,8 @@ duoshuoQuery中可以定义`myIds`和`getUAString`函数:
 <script>var duoshuoQuery={short_name:'test',myIds:[1234567]};</script>
 <script src=http://static.duoshuo.com/embed.js></script>
 
+<!-- 只需要按以上顺序加载,不一定要写在<head>中 -->
+
 </head>
 <body>
 
@@ -50,4 +52,4 @@ duoshuoQuery中可以定义`myIds`和`getUAString`函数:
 1. 我的网站:<http://geraldl.net/about>
 1. 在线工具:<http://www.atool.org>
 
-如果对UA解析要求更高一点可以使用<https://github.com/faisalman/ua-parser-js>。
+如果对UA解析要求更高一点可以使用<https://github.com/faisalman/ua-parser-js>,参考[test/faisalman.html](test/faisalman.html)

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 1 - 1
dist/duoshuo-ua-with-css.min.js


Filskillnaden har hållts tillbaka eftersom den är för stor
+ 1 - 1
dist/duoshuo-ua.min.js


+ 2 - 2
src/duoshuo-ua.js

@@ -3,7 +3,7 @@
  * @require ua-parser.js
  */
 function getUAString(local){
-	var a=local.agent,toString=function(o){
+	var a=parseAgent(local.agent),toString=function(o){
 		var s=o.name;if(o.version) s+=' '+o.version;
 		return s;
 	};
@@ -15,7 +15,7 @@ function callBefore(local,args){
 	var e=args[0],id,myIds=duoshuoQuery.myIds||[];
 	if(args.length==1)	// embed.unstable.js
 		e=e.post;
-	local.agent=parseAgent(e.agent);
+	local.agent=e.agent;
 	id=e.author_id;
 	if(!myIds.indexOf) myIds=[myIds];
 	local.webmaster=myIds.indexOf(id)<0?0:id;

+ 6 - 1
test/example.html

@@ -3,7 +3,12 @@
 	<head>
 		<meta charset=utf-8 />
 		<title>Test DUOSHUO</title>
-		<script>var duoshuoQuery={short_name:'official',myIds:[1234567]};</script>
+		<script>
+			var duoshuoQuery={
+				short_name:'official',
+				myIds:[1234567],
+			};
+		</script>
 		<script src=../dist/duoshuo-ua-with-css.min.js></script>
 		<script src=http://static.duoshuo.com/embed.unstable.js></script>
 	</head>

+ 39 - 0
test/faisalman.html

@@ -0,0 +1,39 @@
+<!doctype html>
+<html>
+	<head>
+		<meta charset=utf-8 />
+		<title>Test DUOSHUO</title>
+		<!-- 先下载并加载faisalman的ua-parser.min.js -->
+		<script src=ua-parser.min.js></script>
+		<!-- 然后设置duoshuoQuery和加载多说,并设置自定义的getUAString -->
+		<script>
+			// 先初始化UAParser
+			var parser=new UAParser();
+			// 准备好生成显示字串的函数
+			parser.getString=function(o,def){
+				var s=o.name;
+				if(s) {
+					if(o.version) s+=' '+o.version;
+				} else s=def||'';
+				return s;
+			};
+			// 然后设置duoshuoQuery
+			var duoshuoQuery={
+				short_name:'official',
+				myIds:[1234567],
+				getUAString:function(local){
+					parser.setUA(local.agent);
+					var result=parser.getResult();
+					return '<div class="ds-os">'+parser.getString(result.os,'山寨操作系统')+'</div>'+
+						'<div class="ds-br">'+parser.getString(result.browser,'山寨浏览器')+'</div>'+
+						(local.webmaster?'<div class=ds-webmaster>站长</div>':'');
+				},
+			};
+		</script>
+		<script src=../dist/duoshuo-ua-with-css.min.js></script>
+		<script src=http://static.duoshuo.com/embed.unstable.js></script>
+	</head>
+	<body>
+		<div class=ds-thread data-thread-key="5174f0a16a04594d4800590e"></div>
+	</body>
+</html>