1
1
<?php
2
2
/**
3
- * 虾米音乐播放器:虾米音乐搜索+引用 (复活版<a href="http://www.yzmb.me" target="_blank">@羽中</a>)
3
+ * 虾米音乐播放器:虾米音乐搜索+引用
4
4
*
5
5
* @package XiaMiPlayer
6
6
* @author 公子
7
- * @version 3.1.3
8
- * @link http ://zh.eming.li/#typecho
7
+ * @version 3.1.4
8
+ * @link https ://imnerd.org
9
9
*/
10
10
class XiaMiPlayer_Plugin implements Typecho_Plugin_Interface
11
11
{
@@ -273,10 +273,9 @@ function show(id, name) {
273
273
else
274
274
$('ul#playlist').append('<li data-id="'+id+'">'+name+'</li>');
275
275
} else {
276
- rm();
277
- var c = '<script type="text/javascript" src="<?php echo $ music ; ?> ?songs='+id+'<?php if ($ color ) echo "&setting= $ color " ; ?> "><\/script>';
278
- $('#text').val($('#text').val() + c);
276
+ var c = '[xiami <?php echo $ music ; ?> ?songs='+id+'<?php if ($ color ) echo "&setting= $ color " ; ?> ]';
279
277
editor(c);
278
+ rm();
280
279
}
281
280
}
282
281
@@ -289,14 +288,18 @@ function insert_link() {
289
288
else
290
289
$('ul#playlist').append('<li data-url="'+link+'">'+name+'</li>');
291
290
} else {
292
- var c = '<script type="text/javascript" src="<?php echo $ music ; ?> ?songs='+link+'|'+name+'<?php if ($ color ) echo "&setting= $ color " ; ?> "><\/script>';
293
- $('#text').val($('#text').val()+c);
291
+ var c = '[xiami <?php echo $ music ; ?> ?songs='+link+'|'+name+'<?php if ($ color ) echo "&setting= $ color " ; ?> ]';
294
292
editor(c);
295
293
rm();
296
294
}
297
295
}
298
296
299
297
function editor(c) {
298
+ var textarea = $('#text'),
299
+ sel = textarea.getSelection(),
300
+ offset = (sel ? sel.start : 0)+c.length;
301
+ textarea.replaceSelection(c);
302
+ textarea.setSelection(offset,offset);
300
303
if (window.frames.length > 0) {
301
304
if (fck = window.frames['text___Frame'])
302
305
var _c = fck.document.getElementsByTagName('iframe')[0].contentDocument.body;
@@ -402,45 +405,51 @@ public static function ParseXiaMi($content, $widget, $lastResult) {
402
405
$ color = '' ;
403
406
break ;
404
407
}
405
- /** Markdown 支持 **/
406
- $ number = preg_match_all ("/<a rel= \"nofollow \" href= \"(http:\/\/(www.)?xiami\.com\/([a-z]+)\/([0-9]+).*?) \"> \\1<\/a>/im " , $ content , $ match );
408
+ /** 短代码替换 **/
409
+ $ number = preg_match_all ('/\[xiami (.*?)\]/si ' , $ content , $ match );
410
+ if ($ number ) {foreach ($ match [0 ] as $ key => $ string ) {
411
+ $ content = str_replace ($ string , '<script type="text/javascript" src=" ' .strip_tags ($ match [1 ][$ key ]).'"></script> ' , $ content );
412
+ }}
413
+
414
+ /** Markdown替换 **/
415
+ $ number = preg_match_all ("/<a href= \"(https:\/\/(www.)?xiami\.com\/([a-z]+)\/([a-zA-Z0-9]+)) \"> \\1<\/a>/im " , $ content , $ match );
407
416
if ($ number ) {foreach ($ match [0 ] as $ key => $ string ) {
408
- $ type = $ match [3 ][$ key ];
417
+ $ type = $ match [3 ][$ key ]== ' song ' ? ' songs ' : $ match [ 3 ][ $ key ] ;
409
418
$ id = $ match [4 ][$ key ];
410
419
$ result = json_decode (file_get_contents ($ ajax .'?type= ' .$ type .'&id= ' .$ id ), true );
411
420
if (in_array ($ type , array ("album " , "collect " ))) {
412
421
if (empty ($ result ['songs ' ])) continue ;
413
422
$ songs = array ();
414
423
foreach ($ result ['songs ' ] as $ song ) $ songs [] = $ song ['song_id ' ];
415
- $ url .= "?songs= " .implode (', ' , $ songs );
416
- if ($ color ) $ url .= "&setting= $ color " ;
417
- $ content = str_replace ($ string , '<script type="text/javascript" src=" ' .$ url .'"></script> ' , $ content );
424
+ $ lurl = $ url . "?songs= " .implode (', ' , $ songs );
425
+ if ($ color ) $ lurl .= "&setting= $ color " ;
426
+ $ content = str_replace ($ string , '<script type="text/javascript" src=" ' .$ lurl .'"></script> ' , $ content );
418
427
} else {
419
428
if (!$ result ['song_id ' ]) continue ;
420
- $ url .= "?songs= " .$ result ['song_id ' ];
421
- if ($ color ) $ url .= "&setting= $ color " ;
422
- $ content = str_replace ($ string , '<script type="text/javascript" src=" ' .$ url .'"></script> ' , $ content );
429
+ $ surl = $ url . "?songs= " .$ result ['song_id ' ];
430
+ if ($ color ) $ surl .= "&setting= $ color " ;
431
+ $ content = str_replace ($ string , '<script type="text/javascript" src=" ' .$ surl .'"></script> ' , $ content );
423
432
}
424
433
}}
425
434
426
- /** 非Markdown 支持 **/
427
- $ number = preg_match_all ("/http :\/\/(www.)?xiami\.com\/([a-z]+)\/([0 -9]+)/im " , $ content , $ match );
435
+ /** 非Markdown替换 **/
436
+ $ number = preg_match_all ("/https :\/\/(www.)?xiami\.com\/([a-z]+)\/([a-zA-Z0 -9]+)/im " , $ content , $ match );
428
437
if ($ number ) {foreach ($ match [0 ] as $ key => $ string ) {
429
- $ type = $ match [2 ][$ key ];
438
+ $ type = $ match [2 ][$ key ]== ' song ' ? ' songs ' : $ match [ 2 ][ $ key ] ;
430
439
$ id = $ match [3 ][$ key ];
431
440
$ result = json_decode (file_get_contents ($ ajax .'?type= ' .$ type .'&id= ' .$ id ), true );
432
441
if (in_array ($ type , array ("album " , "collect " ))) {
433
442
if (empty ($ result ['songs ' ])) continue ;
434
443
$ songs = array ();
435
444
foreach ($ result ['songs ' ] as $ song ) $ songs [] = $ song ['song_id ' ];
436
- $ url .= "?songs= " .implode (', ' , $ songs );
437
- if ($ color ) $ url .= "&setting= $ color " ;
438
- $ content = str_replace ($ string , '<script type="text/javascript" src=" ' .$ url .'"></script> ' , $ content );
445
+ $ lurl = $ url . "?songs= " .implode (', ' , $ songs );
446
+ if ($ color ) $ lurl .= "&setting= $ color " ;
447
+ $ content = str_replace ($ string , '<script type="text/javascript" src=" ' .$ lurl .'"></script> ' , $ content );
439
448
} else {
440
449
if (!$ result ['song_id ' ]) continue ;
441
- $ url .= "?songs= " .$ result ['song_id ' ];
442
- if ($ color ) $ url .= "&setting= $ color " ;
443
- $ content = str_replace ($ string , '<script type="text/javascript" src=" ' .$ url .'"></script> ' , $ content );
450
+ $ surl = $ url . "?songs= " .$ result ['song_id ' ];
451
+ if ($ color ) $ surl .= "&setting= $ color " ;
452
+ $ content = str_replace ($ string , '<script type="text/javascript" src=" ' .$ surl .'"></script> ' , $ content );
444
453
}
445
454
}}
446
455
return $ content ;
0 commit comments