This is how I added support for Bilibili videos (MyBB v1.8.38).
Important note: The editor in this forum is recognizing all occurrences of
[ video
(without space) as a video, and won't let me post that way because it accuses me of having 8 videos while only 1 is allowed here, so I used
[\video
instead. You should remove the \ in your code.
jscripts/bbcodes_sceditor.js
Add in the
mybbCmd
variable after the
Twitch
section:
'Bilibili': {
'match': /bilibili\.com\/video\/([^\/?]+)(?:\?p=(\d+)\/?)?/,
'url': function (videoId, page) {
videoId = new RegExp('av.+', 'i').test(videoId) ? videoId.slice(2) : videoId;
return `//player.bilibili.com/player.html?aid=${videoId}${new RegExp('bv.+', 'i').test(videoId) ? `&bvid=${videoId}` : ''}${page ? `&page=${page}` : ''}&as_wide=1&high_quality=1&autoplay=0`;
},
'html': '<iframe src="{url}" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" ' +
'style="width: 640px; height: 430px; max-width: 100%" height="378" width="620" data-mybb-vt="{type}" data-mybb-vsrc="{src}"></iframe>'
}
Edit the code after the
mybbCmd
variable for the Sceditor
allowedIframeUrls
:
for (var i in mybbCmd.video) {
var item = mybbCmd.video[i];
if(!(item.url instanceof Function)) { //MangaD - don't add functions to allowed urls
$.sceditor.defaultOptions.allowedIframeUrls.push(item.url);
}
}
// MangaD - add bilibili to allowed iframe url's
$.sceditor.defaultOptions.allowedIframeUrls.push("//player.bilibili.com/player.html");
Edit the
html
key of the video BBCode command:
// Add MyBB video command
$.sceditor.formats.bbcode.set('video', {
allowsEmpty: true,
allowedChildren: ['#', '#newline'],
tags: {
iframe: {
'data-mybb-vt': null
}
},
format: function ($element, content) {
return '[\video=' + $($element).data('mybb-vt') + ']' + $($element).data('mybb-vsrc') + '[/video]';
},
html: function (token, attrs, content) {
var params = mybbCmd.video[Object.keys(mybbCmd.video).find(key => key.toLowerCase() === attrs.defaultattr)];
var matches, url;
var n = (attrs.defaultattr == 'dailymotion') ? 2 : 1;
if (typeof params !== "undefined") {
matches = content.match(params['match']);
// MangaD - Support function in url
if (params['url'] instanceof Function && matches) {
url = params['url'](...matches.slice(1));
} else {
url = matches ? params['url'] + matches[n] : false;
}
//url = matches ? params['url'] + matches[n] : false;
}
if (url) {
return params['html'].replace('{url}', url).replace('{src}', content).replace('{type}', attrs.defaultattr);
}
return $.sceditor.escapeEntities(token.val + content + (token.closing ? token.closing.val : ''));
}
});
Admin CP - MyCode
In Admin CP add custom MyCode (in this order!):
Title: Bilibili BV with part
Short Description: Bilibili videos that have BV in URL and a part
Regular Expression: \[\video=bilibili\]https:\/\/(?:www|m)\.bilibili\.com\/video\/[bB][vV]([^\/?]+)\?p=(\d+)\/?\[\/video\]
Replacement:<iframe src="//player.bilibili.com/player.html?aid=bv$1&bvid=bv$1&page=$2&as_wide=1&high_quality=1&autoplay=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" width="638" height="447"></iframe>
Test: [\video=bilibili]https://www.bilibili.com/video/BV1P64y1S76p?p=2[/video]
Title: Bilibili BV
Short Description: Bilibili videos that have BV in the URL
Regular Expression: \[\video=bilibili\]https:\/\/(?:www|m)\.bilibili\.com\/video\/[bB][vV]([^/]+)\/?\[\/video\]
Replacement:<iframe src="//player.bilibili.com/player.html?aid=bv$1&bvid=bv$1&as_wide=1&high_quality=1&autoplay=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" width="638" height="447"></iframe>
Test: [\video=bilibili]https://www.bilibili.com/video/bv1EJ411s7ZG[/video]
Title: Bilibili AV with part
Short Description: Bilibili videos that have AV in URL and a part
Regular Expression: \[\video=bilibili\]https:\/\/(?:www|m)\.bilibili\.com\/video\/[aA][vV]([^\/?]+)\?p=(\d+)\/?\[\/video\]
Replacement:<iframe src="//player.bilibili.com/player.html?aid=$1&page=$2&as_wide=1&high_quality=1&autoplay=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" width="638" height="447"></iframe>
Test: Didn't test this one
Title: Bilibili AV
Short Description: Bilibili videos that have AV in the URL
Regular Expression: \[\video=bilibili\]https:\/\/(?:www|m)\.bilibili\.com\/video\/[aA][vV]([^/]+)\/?\[\/video\]
Replacement:<iframe src="//player.bilibili.com/player.html?aid=$1&as_wide=1&high_quality=1&autoplay=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" width="638" height="447"></iframe>
Test: [\video=bilibili]https://www.bilibili.com/video/av80220911[/video]
Tip: for 100% width (without resize working)
<iframe src="//player.bilibili.com/player.html?aid=$1&as_wide=1&high_quality=1&autoplay=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" style="width:100%;" onload="setTimeout(() => {this.style.height=(this.clientWidth/1.427293065)+'px';}, 1000);" onmouseover="this.style.height=(this.clientWidth/1.427293065)+'px';"></iframe>