Skip to content

Commit 26c6bd9

Browse files
author
volio
committed
support pjax
1 parent acc422b commit 26c6bd9

7 files changed

Lines changed: 108 additions & 128 deletions

File tree

Plugin.php

Lines changed: 58 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*
77
* @package DPlayer
88
* @author Volio
9-
* @version 1.0.4
10-
* @link http://github.com/volio/DPlayer-for-typecho
9+
* @version 1.1.0
10+
* @link https://niconiconi.org
1111
*/
1212
class DPlayer_Plugin implements Typecho_Plugin_Interface
1313
{
@@ -20,8 +20,8 @@ class DPlayer_Plugin implements Typecho_Plugin_Interface
2020
*/
2121
public static function activate()
2222
{
23-
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = ['DPlayer_Plugin', 'parsePlayer'];
24-
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = ['DPlayer_Plugin', 'parsePlayer'];
23+
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = ['DPlayer_Plugin', 'replacePlayer'];
24+
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = ['DPlayer_Plugin', 'replacePlayer'];
2525
Typecho_Plugin::factory('Widget_Archive')->header = ['DPlayer_Plugin', 'playerHeader'];
2626
Typecho_Plugin::factory('Widget_Archive')->footer = ['DPlayer_Plugin', 'playerFooter'];
2727
Typecho_Plugin::factory('admin/write-post.php')->bottom = ['DPlayer_Plugin', 'addEditorButton'];
@@ -44,10 +44,8 @@ public static function deactivate()
4444
*/
4545
public static function playerHeader()
4646
{
47-
$url = Helper::options()->pluginUrl . '/DPlayer';
4847
echo <<<EOF
4948
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.css" />
50-
<script>var dPlayerOptions = [];</script>
5149
EOF;
5250
}
5351

@@ -66,29 +64,26 @@ public static function playerFooter()
6664
}
6765
echo <<<EOF
6866
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.js"></script>
69-
<script type="text/javascript" src="$url/dist/util.js"></script>
67+
<script type="text/javascript" src="$url/assets/player.js"></script>
7068
EOF;
7169
}
7270

7371
/**
7472
* 内容标签替换
7573
*
76-
* @param string $content
74+
* @param $text
7775
* @param $widget
78-
* @param $lastResult
76+
* @param $last
7977
* @return string
8078
*/
81-
public static function parsePlayer($content, $widget, $lastResult)
79+
public static function replacePlayer($text, $widget, $last)
8280
{
83-
$content = empty($lastResult) ? $content : $lastResult;
81+
$text = empty($last) ? $text : $last;
8482
if ($widget instanceof Widget_Archive) {
85-
if (false === strpos($content, '[')) {
86-
return $content;
87-
}
8883
$pattern = self::get_shortcode_regex(['dplayer']);
89-
$content = preg_replace_callback("/$pattern/", ['DPlayer_Plugin', 'parseCallback'], $content);
84+
$text = preg_replace_callback("/$pattern/", ['DPlayer_Plugin', 'parseCallback'], $text);
9085
}
91-
return $content;
86+
return $text;
9287
}
9388

9489
/**
@@ -114,87 +109,75 @@ public static function parseCallback($matches)
114109
}
115110
//还原转义后的html
116111
//[dplayer title=&quot;Test Abc&quot; artist=&quot;haha&quot; id=&quot;1234543&quot;/]
117-
$attr = htmlspecialchars_decode($matches[3]);
112+
$tag = htmlspecialchars_decode($matches[3]);
118113
//[dplayer]标签的属性,类型为array
119-
$atts = self::shortcode_parse_atts($attr);
120-
//播放器id
121-
$id = md5(isset($atts['url']) ? $atts['url'] : 'default id');
114+
$attrs = self::shortcode_parse_atts($tag);
115+
return DPlayer_Plugin::parsePlayer($attrs);
116+
}
122117

118+
public static function parsePlayer($attrs)
119+
{
123120
//播放器设置
124-
$theme = Typecho_Widget::widget('Widget_Options')->plugin('DPlayer')->theme;
121+
$theme = Typecho_Widget::widget('Widget_Options')->plugin('DPlayer')->theme ?: '#FADFA3';
125122
$api = Typecho_Widget::widget('Widget_Options')->plugin('DPlayer')->api;
126-
if (!$theme) $theme = '#FADFA3';
127-
128-
//输出代码
129-
$playerCode = '<div id="player' . $id . '" class="dplayer">';
130-
$playerCode .= "</div>\n";
131123

132-
$video = array(
133-
'url' => isset($atts['url']) ? $atts['url'] : '',
134-
'pic' => isset($atts['pic']) ? $atts['pic'] : '',
135-
'type' => isset($atts['type']) ? $atts['type'] : 'auto',
136-
'thumbnails' => isset($atts['thumbnails']) ? $atts['thumbnails'] : '',
137-
);
138-
//弹幕部分配置文件
139-
$subtitle = [
140-
'url' => isset($atts['subtitleurl']) ? $atts['subtitleurl'] : '',
141-
'type' => isset($atts['subtitletype']) ? $atts['subtitletype'] : 'webvtt',
142-
'fontSize' => isset($atts['subtitlefontsize']) ? $atts['subtitlefontsize'] : '25px',
143-
'bottom' => isset($atts['subtitlebottom']) ? $atts['subtitlebottom'] : '10%',
144-
'color' => isset($atts['subtitlecolor']) ? $atts['subtitlecolor'] : '#b7daff',
145-
];
146-
$danmaku = [
147-
'id' => $id,
148-
'api' => $api,
149-
'maximum' => isset($atts['maximum']) ? $atts['maximum'] : 1000,
150-
'addition' => isset($atts['addition']) ? [$atts['addition']] : null,
151-
'user' => isset($atts['user']) ? $atts['user'] : 'DIYgod',
152-
'bottom' => isset($atts['bottom']) ? $atts['bottom'] : '15%',
153-
'unlimited' => true,
154-
];
155-
156-
//播放器默认属性
157-
$data = [
158-
'id' => $id,
124+
//播放器属性
125+
$config = [
159126
'live' => false,
160-
'autoplay' => false,
161-
'theme' => isset($atts['theme']) ? $atts['theme'] : '#FADFA3',
162-
'loop' => (isset($atts['loop']) && $atts['loop'] == 'true') ? true : false,
163-
'screenshot' => (isset($atts['screenshot']) && $atts['screenshot'] == 'true') ? true : false,
127+
'autoplay' => isset($attrs['autoplay']) && $attrs['autoplay'] == 'true',
128+
'theme' => isset($attrs['theme']) ? $attrs['theme'] : $theme,
129+
'loop' => isset($attrs['loop']) && $attrs['loop'] == 'true',
130+
'screenshot' => isset($attrs['screenshot']) && $attrs['screenshot'] == 'true',
164131
'hotkey' => true,
165132
'preload' => 'metadata',
166-
'lang' => isset($atts['lang']) ? $atts['lang'] : 'zh-cn',
167-
'logo' => isset($atts['logo']) ? $atts['logo'] : null,
168-
'volume' => isset($atts['volume']) ? $atts['volume'] : 0.7,
133+
'lang' => isset($attrs['lang']) ? $attrs['lang'] : 'zh-cn',
134+
'logo' => isset($attrs['logo']) ? $attrs['logo'] : null,
135+
'volume' => isset($attrs['volume']) ? $attrs['volume'] : 0.7,
169136
'mutex' => true,
137+
'video' => [
138+
'url' => isset($attrs['url']) ? $attrs['url'] : null,
139+
'pic' => isset($attrs['pic']) ? $attrs['pic'] : null,
140+
'type' => isset($attrs['type']) ? $attrs['type'] : 'auto',
141+
'thumbnails' => isset($attrs['thumbnails']) ? $attrs['thumbnails'] : null,
142+
],
170143
];
171-
$data['video'] = $video;
172-
$data['danmaku'] = (isset($atts['danmu']) && $atts['danmu'] == 'true') ? $danmaku : null;
173-
$data['subtitle'] = isset($atts['subtitleurl']) ? $subtitle : null;
174-
$data['autoplay'] = (isset($atts['autoplay']) && $atts['autoplay'] == 'true') ? true : false;
175-
$data['theme'] = isset($atts['theme']) ? $atts['theme'] : $theme;
176-
//加入头部数组
177-
$js = json_encode($data);
178-
$playerCode .= <<<EOF
179-
<script>dPlayerOptions.push({$js});</script>
180-
EOF;
181-
return $playerCode;
144+
if (isset($attrs['danmu']) && $attrs['danmu'] == 'true') {
145+
$config['danmaku'] = [
146+
'id' => md5(isset($attrs['url']) ? $attrs['url'] : ''),
147+
'api' => $api,
148+
'maximum' => isset($attrs['maximum']) ? $attrs['maximum'] : 1000,
149+
'user' => isset($attrs['user']) ? $attrs['user'] : 'DIYgod',
150+
'bottom' => isset($attrs['bottom']) ? $attrs['bottom'] : '15%',
151+
'unlimited' => true,
152+
];
153+
}
154+
if (isset($attrs['subtitle']) && $attrs['subtitle'] == 'true') {
155+
$config['subtitle'] = [
156+
'url' => isset($attrs['subtitleurl']) ? $attrs['subtitleurl'] : null,
157+
'type' => isset($attrs['subtitletype']) ? $attrs['subtitletype'] : 'webvtt',
158+
'fontSize' => isset($attrs['subtitlefontsize']) ? $attrs['subtitlefontsize'] : '25px',
159+
'bottom' => isset($attrs['subtitlebottom']) ? $attrs['subtitlebottom'] : '10%',
160+
'color' => isset($attrs['subtitlecolor']) ? $attrs['subtitlecolor'] : '#b7daff',
161+
];
162+
}
163+
$json = json_encode($config);
164+
return "<div class=\"dplayer\" data-config='{$json}'></div>";
182165
}
183166

184167
public static function addEditorButton()
185168
{
186-
$dir = Helper::options()->pluginUrl . '/DPlayer/dist/editor.js';
169+
$dir = Helper::options()->pluginUrl . '/DPlayer/assets/editor.js';
187170
echo "<script type=\"text/javascript\" src=\"{$dir}\"></script>";
188171
}
189172

190173
public static function config(Typecho_Widget_Helper_Form $form)
191174
{
192175
$theme = new Typecho_Widget_Helper_Form_Element_Text(
193176
'theme', null, '#FADFA3',
194-
_t('默认主题颜色'), _t('播放器默认的主题颜色, #372e21、#75c、red、blue,该设定会被[dplayer]标签中的theme属性覆盖,默认为 #FADFA3'));
177+
_t('默认主题颜色'), _t('播放器默认的主题颜色,例如 #372e21、#75c、red、blue,该设定会被[dplayer]标签中的theme属性覆盖,默认为 #FADFA3'));
195178
$api = new Typecho_Widget_Helper_Form_Element_Text(
196-
'api', null, 'https://api.prprpr.me/dplayer/v3/',
197-
_t('弹幕服务器地址'), _t('用于保存视频弹幕,默认为 https://api.prprpr.me/dplayer/v3/'));
179+
'api', null, '',
180+
_t('弹幕服务器地址'), _t('用于保存视频弹幕,例如 https://api.prprpr.me/dplayer/v3/'));
198181
$hls = new Typecho_Widget_Helper_Form_Element_Radio('hls', array('0' => _t('不开启HLS支持'), '1' => _t('开启HLS支持')), '0', _t('HLS支持'), _t("开启后可解析 m3u8 格式视频"));
199182
$flv = new Typecho_Widget_Helper_Form_Element_Radio('flv', array('0' => _t('不开启FLV支持'), '1' => _t('开启FLV支持')), '0', _t('FLV支持'), _t("开启后可解析 flv 格式视频"));
200183
$form->addInput($theme);

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,26 @@
99
[dplayer url="http://xxx.com/xxx.mp4" pic="http://xxx.com/xxx.jpg"/]
1010
```
1111

12-
关闭弹幕
12+
开启弹幕
1313
```
14-
[dplayer url="http://xxx.com/xxx.mp4" pic="http://xxx.com/xxx.jpg" danmu="false"/]
14+
[dplayer url="http://xxx.com/xxx.mp4" pic="http://xxx.com/xxx.jpg" danmu="true"/]
1515
```
1616

1717
开启自动播放
1818
```
1919
[dplayer url="http://xxx.com/xxx.mp4" pic="http://xxx.com/xxx.jpg" autoplay="true"/]
2020
```
2121

22-
添加额外弹幕源(例:bilibili弹幕)
22+
更多参数待补充
23+
24+
### FAQ
25+
26+
#### 1. Pjax页面切换?
27+
28+
重新加载播放器回调函数
2329
```
24-
[dplayer url="http://xxx.com/xxx.mp4" pic="http://xxx.com/xxx.jpg" autoplay="true" addition="https://api.prprpr.me/dplayer/bilibili?aid=7286894"/]
30+
loadDPlayer();
2531
```
2632

27-
### 设置截图
28-
![](https://raw.githubusercontent.com/volio/DPlayer-for-typecho/master/assets/screenshot.png)
29-
3033
### LICENSE
3134
MIT © [Volio](https://niconiconi.org)

dist/editor.js renamed to assets/editor.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ $(function () {
66
$(document).on('click', '#wmd-dplayer-button', function () {
77
$('body').append(
88
'<div id="DPlayer-Panel">' +
9-
'<div class="wmd-prompt-background" style="position: absolute; top: 0px; z-index: 1000; opacity: 0.5; height: 875px; left: 0px; width: 100%;"></div>' +
9+
'<div class="wmd-prompt-background" style="position: absolute; top: 0; z-index: 1000; opacity: 0.5; height: 875px; left: 0; width: 100%;"></div>' +
1010
'<div class="wmd-prompt-dialog">' +
1111
'<div>' +
1212
'<p><b>插入视频</b></p>' +
1313
'<p>在下方输入参数</p>' +
14-
'<p><input type="text" id="DP-url" value="" placeholder="链接"></input></p>' +
15-
'<p><input type="text" id="DP-pic" value="" placeholder="封面图"></input></p>' +
16-
'<p><input type="text" id="DP-addition" value="" placeholder="额外弹幕源"></input></p>' +
14+
'<p><input type="text" id="DP-url" value="" placeholder="链接"/></p>' +
15+
'<p><input type="text" id="DP-pic" value="" placeholder="封面图"/></p>' +
16+
'<p><input type="text" id="DP-addition" value="" placeholder="额外弹幕源"/></p>' +
1717
'<p><input type="checkbox" id="DP-danmu" checked>开启弹幕</input></p>' +
1818
'<p><input type="checkbox" id="DP-autoplay">自动播放</input></p>' +
1919
'</div>' +
@@ -31,40 +31,38 @@ $(function () {
3131
});
3232
//ok
3333
$(document).on('click', '#ok', function () {
34-
var DP_url = document.getElementById('DP-url').value,
35-
DP_pic = document.getElementById('DP-pic').value,
36-
DP_danmu = document.getElementById('DP-danmu').checked ? true : false,
37-
DP_autoplay = document.getElementById('DP-autoplay').checked ? true : false,
38-
DP_addition = document.getElementById('DP-addition').value;
39-
var tag = '[dplayer url="' + DP_url + '" pic="' + DP_pic + '" ';
40-
if (!DP_danmu) tag += 'danmu="' + DP_danmu + '" ';
41-
if (DP_autoplay) tag += 'autoplay="' + DP_autoplay + '" ';
42-
if (DP_addition) tag += 'addition="' + DP_addition + '" ';
34+
var url = document.getElementById('DP-url').value,
35+
pic = document.getElementById('DP-pic').value,
36+
danmu = !!document.getElementById('DP-danmu').checked,
37+
autoplay = !!document.getElementById('DP-autoplay').checked;
38+
var tag = '[dplayer url="' + url + '" pic="' + pic + '" ';
39+
if (!danmu) tag += 'danmu="' + danmu + '" ';
40+
if (autoplay) tag += 'autoplay="' + autoplay + '" ';
4341
tag += '/]\n';
4442

45-
myField = document.getElementById('text');
43+
var editor = document.getElementById('text');
4644

4745
if (document.selection) {
48-
myField.focus();
46+
editor.focus();
4947
sel = document.selection.createRange();
5048
sel.text = tag;
51-
myField.focus();
49+
editor.focus();
5250
}
53-
else if (myField.selectionStart || myField.selectionStart == '0') {
54-
var startPos = myField.selectionStart;
55-
var endPos = myField.selectionEnd;
51+
else if (editor.selectionStart || editor.selectionStart === '0') {
52+
var startPos = editor.selectionStart;
53+
var endPos = editor.selectionEnd;
5654
var cursorPos = startPos;
57-
myField.value = myField.value.substring(0, startPos)
55+
editor.value = editor.value.substring(0, startPos)
5856
+ tag
59-
+ myField.value.substring(endPos, myField.value.length);
57+
+ editor.value.substring(endPos, myField.value.length);
6058
cursorPos += tag.length;
61-
myField.focus();
62-
myField.selectionStart = cursorPos;
63-
myField.selectionEnd = cursorPos;
59+
editor.focus();
60+
editor.selectionStart = cursorPos;
61+
editor.selectionEnd = cursorPos;
6462
}
6563
else {
66-
myField.value += tag;
67-
myField.focus();
64+
editor.value += tag;
65+
editor.focus();
6866
}
6967

7068
$('#DPlayer-Panel').remove();

assets/player.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var dPlayers = [];
2+
var loadDPlayer = function () {
3+
var load = function (d, conf) {
4+
conf.container = d;
5+
dPlayers.push(new DPlayer(conf));
6+
};
7+
for (var i = 0; i < dPlayers.length; i++) {
8+
dPlayers[i].destroy();
9+
}
10+
dPlayers = [];
11+
for (var j = 0, k = document.querySelectorAll('.dplayer'); j < k.length; j++) {
12+
load(k[j], JSON.parse(k[j].dataset.config));
13+
}
14+
};
15+
16+
document.addEventListener('DOMContentLoaded', loadDPlayer, !1);

assets/screenshot.png

-19.6 KB
Binary file not shown.

assets/screenshot_editor.png

-24.7 KB
Binary file not shown.

dist/util.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)