Skip to content
Permalink
Browse files

Script: Refactor duoshuo hook.

  • Loading branch information
iissnan committed Nov 15, 2015
1 parent 1c8a398 commit 09f9f11de5ec8fa5cfb3313591225fdae3a19c06
@@ -4,7 +4,7 @@ root = true

[*]
charset = utf-8
end_of_line = crlf
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
@@ -3,6 +3,8 @@ title:
category: Kategorie
tag: Tag

author: Author

menu:
home: Startseite
archives: Archiv
@@ -3,6 +3,8 @@ title:
category: Category
tag: Tag

author: Author

menu:
home: Home
archives: Archives
@@ -3,6 +3,8 @@ title:
category: Category
tag: Tag

author: Author

menu:
home: Home
archives: Archives
@@ -3,6 +3,8 @@ title:
category: Catégorie
tag: Tag

author: Author

menu:
home: Accueil
archives: Archives
@@ -3,6 +3,8 @@ title:
category: Categoria
tag: Tag

author: Author

menu:
home: Home
archives: Arquivos
@@ -3,6 +3,8 @@ title:
category: Категория
tag: Тэг

author: Author

menu:
home: Домой
archives: Архив
@@ -3,6 +3,8 @@ title:
category: 分类
tag: 标签

author: 博主

menu:
home: 首页
archives: 归档
@@ -3,6 +3,8 @@ title:
category: 分類
tag: 標籤

author: 博主

menu:
home: 首頁
archives: 歸檔
@@ -3,6 +3,8 @@ title:
category: 分類
tag: 標籤

author: 博主

menu:
home: 首頁
archives: 歸檔
@@ -21,18 +21,13 @@

{% if theme.duoshuo_info.ua_enable %}
{% if theme.duoshuo_info.admin_enable %}

{% if theme.duoshuo_info.admin_nickname %}
<script type="text/javascript">
var duoshuo_user_ID = {{theme.duoshuo_info.user_id}};
var duoshuo_admin_nickname="{{theme.duoshuo_info.admin_nickname}}";
</script>
{% endif %}

<script type="text/javascript">
var duoshuo_user_ID = {{theme.duoshuo_info.user_id}};
var duoshuo_admin_nickname = "{{theme.duoshuo_info.admin_nickname | default(__('author'))}}";
</script>
<script src="{{ url_for(theme.vendors) }}/ua-parser-js/dist/ua-parser.min.js"></script>
<script src="{{ url_for(theme.js) }}/src/hook-duoshuo.js"></script>
{% endif %}

{% endif %}

{% endif %}
@@ -154,64 +154,84 @@
background: none;
}
}

/*duoshuo UA style begin*/

.this_ua {
#ds-reset .duoshuo-ua-admin {
display: inline-block;
color: red;
}

#ds-reset .duoshuo-ua-platform,
#ds-reset .duoshuo-ua-browser {
color: #ccc;

.fa {
display: inline-block;
margin-right: 3px;
}
}

#ds-reset .duoshuo-ua-seperator {
display: inline-block;
margin-left: 5px;
}

.this_ua {
background-color: #ccc !important;
border-radius: 4px;
padding: 0 5px !important;
margin: 1px 1px !important;
border: 1px solid #BBB !important;
color: #fff;
display: inline-block !important;
}
display: inline-block !important;
}

.this_ua.admin {
.this_ua.admin {
background-color: #d9534f !important;
border-color: #d9534f !important;
}
}

.this_ua.platform.iOS, .this_ua.platform.Mac, .this_ua.platform.Windows {
.this_ua.platform.iOS, .this_ua.platform.Mac, .this_ua.platform.Windows {
background-color: #39b3d7 !important;
border-color: #46b8da !important;
}
}

.this_ua.platform.Linux {
.this_ua.platform.Linux {
background-color: #3A3A3A !important;
border-color: #1F1F1F !important;
}
}

.this_ua.platform.Android {
.this_ua.platform.Android {
background-color: #00C47D !important;
border-color: #01B171 !important;
}
}

.this_ua.browser.Mobile, .this_ua.browser.Chrome {
.this_ua.browser.Mobile, .this_ua.browser.Chrome {
background-color: #5cb85c !important;
border-color: #4cae4c !important;
}
}

.this_ua.browser.Firefox {
.this_ua.browser.Firefox {
background-color: #f0ad4e !important;
border-color: #eea236 !important;
}
}

.this_ua.browser.Maxthon, .this_ua.browser.IE {
.this_ua.browser.Maxthon, .this_ua.browser.IE {
background-color: #428bca !important;
border-color: #357ebd !important;
}
}

.this_ua.browser.baidu, .this_ua.browser.UCBrowser, .this_ua.browser.Opera {
.this_ua.browser.baidu, .this_ua.browser.UCBrowser, .this_ua.browser.Opera {
background-color: #d9534f !important;
border-color: #d43f3a !important;
}
}

.this_ua.browser.Android, .this_ua.browser.QQBrowser {
.this_ua.browser.Android, .this_ua.browser.QQBrowser {
background-color: #78ACE9 !important;
border-color: #4cae4c !important;
}
}

/*duoshuo UA style end*/

}
No changes.
@@ -1,70 +1,96 @@
if (typeof DUOSHUO !== 'undefined') {
hookDUOSHUO_tp();
} else {
$('#duoshuo-script')[0].onload = hookDUOSHUO_tp;
}
typeof DUOSHUO !== 'undefined' ?
hookDUOSHUO_tp() :
($('#duoshuo-script')[0].onload = hookDUOSHUO_tp);


function hookDUOSHUO_tp() {
var _D_post = DUOSHUO.templates.post;

DUOSHUO.templates.post = function (e, t) {
var rs = _D_post(e, t);
var agent = e.post.agent;
var isAdmin;
if (typeof duoshuo_user_ID !== 'undefined') {
if (e.post.author.user_id && (e.post.author.user_id == duoshuo_user_ID)) {
if (duoshuo_admin_nickname) {
isAdmin = '<span class="fa">' + duoshuo_admin_nickname + '</span>';
} else {
isAdmin = '<span class="fa">博主</span>';
}
} else {
isAdmin = '';
}
} else {
isAdmin = '';
var userId = e.post.author.user_id;
var admin = '';

if (userId && (userId == duoshuo_user_ID)) {
admin = '<span class="duoshuo-ua-admin">' + duoshuo_admin_nickname + '</span>';
}
if (agent && /^Mozilla/.test(agent)) rs = rs.replace(/<\/div><p>/, isAdmin + showUa(agent) + '</div><p>');
return rs;
}
}

//移动客户端判断
function checkMobile() {
var isiPad = navigator.userAgent.match(/iPad/i) !== null;
if (isiPad) {
return false;
}
var isMobile = navigator.userAgent.match(/iphone|android|phone|mobile|wap|netfront|x11|java|opera mobi|opera mini|ucweb|windows ce|symbian|symbianos|series|webos|sony|blackberry|dopod|nokia|samsung|palmsource|xda|pieplus|meizu|midp|cldc|motorola|foma|docomo|up.browser|up.link|blazer|helio|hosin|huawei|novarra|coolpad|webos|techfaith|palmsource|alcatel|amoi|ktouch|nexian|ericsson|philips|sagem|wellcom|bunjalloo|maui|smartphone|iemobile|spice|bird|zte-|longcos|pantech|gionee|portalmmm|jig browser|hiptop|benq|haier|^lct|320x320|240x320|176x220/i) != null;
if (isMobile) {
return true;
if (agent && /^Mozilla/.test(agent)) {
rs = rs.replace(/<\/div><p>/, admin + getAgentInfo(agent) + '</div><p>');
}

return rs;
}
return false;
}

function showUa(string) {
function getAgentInfo(string) {
$.ua.set(string);

var UNKNOWN = 'Unknown';
var sua = $.ua;
var br = '&nbsp;&nbsp;';
var osIco = '<i class="fa fa-desktop"></i>&nbsp;';
var browserIco = '<i class="fa fa-globe"></i>&nbsp;'
var osName = sua.os.name;
var browserName = sua.browser.name;
if (checkMobile()) br = '<br><br>';
var seperator = isMobile() ? '<br><br>' : '<span class="duoshuo-ua-seperator"></span>';
var osName = sua.os.name || UNKNOWN;
var osVersion = sua.os.version || UNKNOWN;
var browserName = sua.browser.name || UNKNOWN;
var browserVersion = sua.browser.version || UNKNOWN;
var iconMapping = {
os: {
android : 'android',
linux : 'linux',
windows : 'windows',
ios : 'apple',
'mac os': 'apple',
unknown : 'desktop'
},
browser: {
chrome : 'chrome',
'chromium': 'chrome',
firefox: 'firefox',
opera : 'opera',
safari : 'safari',
ie : 'internet-explorer',
unknown: 'globe'
}
};
var osIcon = iconMapping.os[osName.toLowerCase()];
var browserIcon = iconMapping.browser[browserName.toLowerCase()];

return seperator +
'<span class="duoshuo-ua-platform duoshuo-ua-platform-' + osName.toLowerCase() + '">' +
'<i class="fa fa-' + osIcon + '"></i>' +
osName + ' ' + osVersion +
'</span>' + seperator +
'<span class="duoshuo-ua-browser duoshuo-ua-browser-' + browserName.toLowerCase() + '">' +
'<i class="fa fa-' + browserIcon + '"></i>' +
browserName + ' ' + browserVersion +
'</span>';

if (osName == 'Android') osIco = '<i class="fa fa-android"></i>&nbsp;';
if (osName.match(/linux/i)) osIco = '<i class="fa fa-linux"></i>&nbsp;';
if (osName.match(/mac os|ios/i)) osIco = '<i class="fa fa-apple"></i>&nbsp;';
if (sua.os.version == 'x86_64') sua.os.version = 'x64';
function isMobile() {
var userAgent = navigator.userAgent;

if (browserName.match(/chrome|chromium/i)) browserIco = '<i class="fa fa-chrome"></i>&nbsp;';
if (browserName.match(/firefox/i)) browserIco = '<i class="fa fa-firefox"></i>&nbsp;';
if (browserName.match(/opera/i)) browserIco = '<i class="fa fa-opera"></i>&nbsp;';
if (browserName.match(/safari/i)) browserIco = '<i class="fa fa-safari"></i>&nbsp;';
if (browserName.match(/ie/i)) browserIco = '<i class="fa fa-internet-explorer"></i>&nbsp;';
var isiPad = userAgent.match(/iPad/i) !== null;
var mobileUA = [
'iphone', 'android', 'phone', 'mobile',
'wap', 'netfront', 'x11', 'java', 'opera mobi',
'opera mini', 'ucweb', 'windows ce', 'symbian',
'symbianos', 'series', 'webos', 'sony',
'blackberry', 'dopod', 'nokia', 'samsung',
'palmsource', 'xda', 'pieplus', 'meizu',
'midp' ,'cldc' , 'motorola', 'foma',
'docomo', 'up.browser', 'up.link', 'blazer',
'helio', 'hosin', 'huawei', 'novarra',
'coolpad', 'webos', 'techfaith', 'palmsource',
'alcatel', 'amoi', 'ktouch', 'nexian',
'ericsson', 'philips', 'sagem', 'wellcom',
'bunjalloo', 'maui', 'smartphone', 'iemobile',
'spice', 'bird', 'zte-', 'longcos',
'pantech', 'gionee', 'portalmmm', 'jig browser',
'hiptop', 'benq', 'haier', '^lct',
'320x320', '240x320', '176x220'
];
var pattern = new RegExp(mobileUA.join('|'), 'i');

return br + '<span class="platform ' + sua.os.name + '">' + osIco +
sua.os.name + ' ' + sua.os.version + '</span>' + br +
'<span class="browser ' + sua.browser.name + '">' + browserIco +
sua.browser.name + '|' + sua.browser.version + '</span>';
return !isiPad && userAgent.match(pattern);
}
}

0 comments on commit 09f9f11

Please sign in to comment.
You can’t perform that action at this time.