├── README.md
└── Plugin.php
/README.md:
--------------------------------------------------------------------------------
1 | # typembed
2 |
3 | Typembed 是为 Typecho 添加对在线视频支持的一款插件(支持手机、平板等设备HTML5播放)。目前支持YouTube、优酷、搜狐视频、土豆、56、腾讯视频、新浪视频、酷6、华数、乐视 等网站。
4 |
5 | 与 WordPress 支持的 oembed 可以无缝切换,换博客系统也不用担心内容不一致。
6 |
7 | 你可以直接粘贴视频播放页完整的URL到编辑器(单独一行),就可以加载视频播放器(不能在编辑器里实时渲染,文章发布之后可以看到效果)。
8 |
9 | 项目主页:https://7yper.com/3636
10 |
11 | 效果展示:https://7yper.com
12 |
13 | 关注微信公众号,获得更新提醒
14 |
15 | 
16 |
--------------------------------------------------------------------------------
/Plugin.php:
--------------------------------------------------------------------------------
1 | contentEx = array('Typembed_Plugin', 'parse');
23 | Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('Typembed_Plugin', 'parse');
24 | }
25 |
26 | public static function parse($content, $widget, $lastResult){
27 | $content = empty($lastResult) ? $content : $lastResult;
28 | if ($widget instanceof Widget_Archive){
29 | $content = preg_replace_callback('/
(?:(?:]+>)?(?(?:(http|https):\/\/)+[a-z0-9_\-\/\.\?%#=]+)(?:<\/a>)?)<\/p>/si', array('Typembed_Plugin', 'parseCallback'), $content);
30 | }
31 | return $content;
32 | }
33 |
34 | public static function parseCallback($matches){
35 | $is_music = array('music.163.com');
36 |
37 | try {
38 | $jump_play = Typecho_Widget::widget('Widget_Options')->plugin('Typembed')->jump_play;
39 | } catch(Typecho_Plugin_Exception $e) {
40 | $jump_play = 0;
41 | }
42 |
43 | $providers = array(
44 | // video
45 | 'www.youtube.com' => array(
46 | '#https?://www\.youtube\.com/watch\?v=(?[a-z0-9_=\-]+)#i',
47 | 'https://www.youtube.com/v/{video_id}',
48 | 'https://www.youtube.com/embed/{video_id}',
49 | ),
50 | 'youtu.be' => array(
51 | '#https?://youtu\.be/(?[a-z0-9_=\-]+)#i',
52 | 'https://www.youtube.com/v/{video_id}',
53 | 'https://www.youtube.com/embed/{video_id}',
54 | ),
55 | 'www.bilibili.com' => array(
56 | '#https?://www\.bilibili\.com/video/(?:[av|BV]+)(?:(?[a-zA-Z0-9_=\-]+)/(?:index_|\#page=)(?[a-zA-Z0-9_=\-]+)|(?[a-zA-Z0-9_=\-]+))#i',
57 | '',
58 | '//player.bilibili.com/player.html?aid={video_id}&bvid={video_id}&cid=&page=1',
59 | ),
60 | 'v.youku.com' => array(
61 | '#https?://v\.youku\.com/v_show/id_(?[a-z0-9_=\-]+)#i',
62 | '',
63 | 'https://player.youku.com/embed/{video_id}?client_id=d0b1b77a17cded3b',
64 | ),
65 | 'v.qq.com' => array(
66 | '#https?://v\.qq\.com/(?:[a-z0-9_\./]+\?vid=(?[a-z0-9_=\-]+)|(?:[a-z0-9/]+)/(?[a-z0-9_=\-]+))#i',
67 | '',
68 | 'https://v.qq.com/iframe/player.html?vid={video_id}',
69 | ),
70 | 'www.dailymotion.com' => array(
71 | '#https?://www\.dailymotion\.com/video/(?[a-z0-9_=\-]+)#i',
72 | '',
73 | 'https://www.dailymotion.com/embed/video/{video_id}',
74 | ),
75 | 'www.acfun.cn' => array(
76 | '#https?://www\.acfun\.cn/v/ac(?\d+)#i',
77 | '',
78 | 'https://www.acfun.cn/player/ac{video_id}',
79 | ),
80 | 'my.tv.sohu.com' => array(
81 | '#https?://my\.tv\.sohu\.com/us/(?:\d+)/(?\d+)#i',
82 | '',
83 | 'https://tv.sohu.com/upload/static/share/share_play.html#{video_id}_0_0_9001_0',
84 | ),
85 | 'www.56.com' => array(
86 | '#https?://(?:www\.)?56\.com/[a-z0-9]+/(?:play_album\-aid\-[0-9]+_vid\-(?[a-z0-9_=\-]+)|v_(?[a-z0-9_=\-]+))#i',
87 | '',
88 | 'https://www.56.com/iframe/{video_id}',
89 | ),
90 | 'www.wasu.cn' => array(
91 | '#https?://www\.wasu\.cn/play/show/id/(?\d+)#i',
92 | '',
93 | 'https://www.wasu.cn/Play/iframe/id/{video_id}',
94 | ),
95 | // music
96 | 'music.163.com' => array(
97 | '#https?://music\.163\.com/\#/song\?id=(?\d+)#i',
98 | '',
99 | 'https://music.163.com/outchain/player?type=2&id={video_id}&auto=0&height=90',
100 | ),
101 | );
102 | $video_url = $matches['video_url'];
103 | $parse = parse_url($video_url);
104 | $site = $parse['host'];
105 | if(!in_array($site, array_keys($providers))){
106 | return '' . $matches['video_url'] . '
';
107 | }
108 | preg_match_all($providers[$site][0], $matches['video_url'], $match);
109 | $id = $match['video_id'][0] == '' ? $match['video_id2'][0] : $match['video_id'][0];
110 | if(self::isMobile()){
111 | try{
112 | $width = Typecho_Widget::widget('Widget_Options')->plugin('Typembed')->mobile_width;
113 | $height = Typecho_Widget::widget('Widget_Options')->plugin('Typembed')->mobile_height;
114 | }catch(Typecho_Plugin_Exception $e){
115 | $width = '100%';
116 | $height = '500';
117 | }
118 | }else{
119 | try{
120 | $width = Typecho_Widget::widget('Widget_Options')->plugin('Typembed')->width;
121 | $height = Typecho_Widget::widget('Widget_Options')->plugin('Typembed')->height;
122 | }catch(Typecho_Plugin_Exception $e){
123 | $width = '100%';
124 | $height = '250';
125 | }
126 | }
127 | if(in_array($site, $is_music)){
128 | $height = '110px';
129 | }
130 | if($jump_play){
131 | $html = sprintf(
132 | '',
137 | $video_url, $width, $height);
138 | }else{
139 | $url = str_replace('{video_id}', $id, $providers[$site][2]);
140 | $html = sprintf(
141 | '',
142 | $url, $width, $height);
143 | }
144 | return ''.$html.'
';
145 | }
146 |
147 | /**
148 | * 禁用插件方法,如果禁用失败,直接抛出异常
149 | *
150 | * @static
151 | * @access public
152 | * @return void
153 | * @throws Typecho_Plugin_Exception
154 | */
155 | public static function deactivate(){}
156 |
157 | /**
158 | * 获取插件配置面板
159 | *
160 | * @access public
161 | * @param Typecho_Widget_Helper_Form $form 配置面板
162 | * @return void
163 | */
164 | public static function config(Typecho_Widget_Helper_Form $form){
165 | try{
166 | $typembed_code = Typecho_Widget::widget('Widget_Options')->plugin('Typembed')->typembed_code;
167 | }catch(Typecho_Plugin_Exception $e){
168 | $typembed_code = '';
169 | }
170 | $width = new Typecho_Widget_Helper_Form_Element_Text('width', NULL, '100%', _t('播放器宽度'));
171 | $form->addInput($width);
172 | $height = new Typecho_Widget_Helper_Form_Element_Text('height', NULL, '500', _t('播放器高度'));
173 | $form->addInput($height);
174 | $mobile_width = new Typecho_Widget_Helper_Form_Element_Text('mobile_width', NULL, '100%', _t('移动设备播放器宽度'));
175 | $form->addInput($mobile_width);
176 | $mobile_height = new Typecho_Widget_Helper_Form_Element_Text('mobile_height', NULL, '250', _t('移动设备播放器高度'));
177 | $form->addInput($mobile_height);
178 | $jump_play = new Typecho_Widget_Helper_Form_Element_Radio('jump_play', array(
179 | 1 => _t('启用'),
180 | 0 => _t('关闭')
181 | ), 0, _t('跳转播放'), _t('手机端不支持H5播放的视频,将跳转到源网站播放
升级到最新版本。关注微信公众号Typer。'));
182 | $form->addInput($jump_play->addRule('enum', _t('必须选择一个模式'), array(0, 1)));
183 | $typembed_code_text = new Typecho_Widget_Helper_Form_Element_Fake('typembed_code', NULL, '', _t('高级功能激活码'), '');
184 | $form->addInput($typembed_code_text);
185 | }
186 |
187 | /**
188 | * 个人用户的配置面板
189 | *
190 | * @access public
191 | * @param Typecho_Widget_Helper_Form $form
192 | * @return void
193 | */
194 | public static function personalConfig(Typecho_Widget_Helper_Form $form){}
195 |
196 | /**
197 | * 移动设备识别
198 | *
199 | * @return boolean
200 | */
201 | private static function isMobile(){
202 | $user_agent = $_SERVER['HTTP_USER_AGENT'];
203 | $mobile_browser = Array(
204 | "mqqbrowser", // 手机QQ浏览器
205 | "opera mobi", // 手机opera
206 | "juc","iuc", 'ucbrowser', // uc浏览器
207 | "fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod",
208 | "iemobile", "windows ce", // windows phone
209 | "240x320","480x640","acer","android","anywhereyougo.com","asus","audio","blackberry",
210 | "blazer","coolpad" ,"dopod", "etouch", "hitachi","htc","huawei", "jbrowser", "lenovo",
211 | "lg","lg-","lge-","lge", "mobi","moto","nokia","phone","samsung","sony",
212 | "symbian","tablet","tianyu","wap","xda","xde","zte"
213 | );
214 | $is_mobile = false;
215 | foreach ($mobile_browser as $device) {
216 | if (stristr($user_agent, $device)) {
217 | $is_mobile = true;
218 | break;
219 | }
220 | }
221 | return $is_mobile;
222 | }
223 | }
224 |
--------------------------------------------------------------------------------