Created
May 25, 2016 13:44
-
-
Save pacochi/44442c2279cfb2ae0cff3893a4158f57 to your computer and use it in GitHub Desktop.
はてなブログで MML プレイヤーを出します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 元のは Ten.js ないと動かなかった & d.hatena.ne.jp でしか読み込めなかった | |
(function() { | |
if (typeof(Hatena) == 'undefined') Hatena = {}; | |
if (typeof(Hatena.MMLPlayer) == 'function') return; | |
var hatenaD = 'http://d.hatena.ne.jp/images/'; | |
var buttonCSS = { 'cursor': 'pointer', 'margin-right': '4px' }; | |
Hatena.MMLPlayer = function(mmlCode) { | |
this.initialize(mmlCode); | |
}; | |
Hatena.MMLPlayer.prototype = { | |
initialize: function(mmlCode) { | |
this.mmlCode = mmlCode.addClass('lang-mml'); | |
this.playButton = $('<img>').addClass('mml-player-play') | |
.attr({ alt: 'mmlstart', src: hatenaD + 'mml_play.gif' }).css(buttonCSS) | |
.click(() => { this.playButtonClickHandler(); }); | |
this.pauseButton = $('<img>').addClass('mml-player-pause') | |
.attr({ alt: 'mmlpause', src: hatenaD + 'mml_pause.gif' }).css(buttonCSS) | |
.click(() => { this.pauseButtonClickHandler(); }).hide(); | |
this.stopButton = $('<img>').addClass('mml-player-stop') | |
.attr({ alt: 'mmlstop', src: hatenaD + 'mml_stop.gif' }).css(buttonCSS) | |
.click(() => { this.stopButtonClickHandler(); }); | |
this.mmlPlayer = $('<div>').addClass('mml-player').css('margin', '8px 0') | |
.append(this.playButton).append(this.pauseButton).append(this.stopButton) | |
.insertBefore(this.mmlCode); | |
this.mmlSource = this.mmlCode.text(); | |
this.mml = new JSMML(); | |
this.mml.onFinish = () => { this.playFinishHandler(); }; | |
}, | |
playButtonClickHandler: function() { | |
this.playButton.hide(); | |
this.pauseButton.show(); | |
this.mml.play(this.mml.pauseNow ? false : this.mmlSource); | |
}, | |
pauseButtonClickHandler: function() { | |
this.playFinishHandler(); | |
this.mml.pause(); | |
}, | |
stopButtonClickHandler: function() { | |
this.playFinishHandler(); | |
this.mml.stop(); | |
}, | |
playFinishHandler: function() { | |
this.pauseButton.hide(); | |
this.playButton.show(); | |
} | |
}; | |
// このスクリプトが実行された時点では多分まだ jQuery が読み込まれてない | |
// http://staff.hatenablog.com/entry/2013/10/24/155959 | |
document.addEventListener('DOMContentLoaded', () => { | |
JSMML.onLoad = () => { | |
// class に lang-xx がないやつ | |
$('pre[class="code"]').each(function() { | |
var mmlCode = $(this); | |
// このスクリプトを読み込んでるエントリ内のみ処理 | |
if (mmlCode.parents('div.entry-content').find('script[src$="mml_player.js"]').length > 0) | |
new Hatena.MMLPlayer(mmlCode); | |
}); | |
}; | |
JSMML.init(hatenaD + 'JSMML.swf'); | |
}); | |
})(); | |
/* | |
http://d.hatena.ne.jp/js/mml_player.js から引っ張ってきた JSMML | |
ドメイン失効してるから補足しとくと id:secondlife さん (元はてな社員) | |
id:tekisuke さんの FlMML http://flmml.codeplex.com/ ってのを使用してるらしい | |
*/ | |
/* | |
* Author: Yuichi Tateno | |
* http://rails2u.com/ | |
* | |
* The MIT Licence. | |
*/ | |
JSMML = (function() { | |
return function(swfurl) { | |
this.mmlPlayer = document.getElementById(JSMML.mmlID); | |
this.initialize.call(this); | |
} | |
})(); | |
JSMML.VESION = '1.0.2'; | |
JSMML.setSWFVersion = function(v) { JSMML.SWF_VERSION = v }; | |
JSMML.SWF_VERSION = 'SWF don\'t load, yet.'; | |
JSMML.toString = function() { | |
return 'JSMML VERSION: ' + JSMML.VESION + ', SWF_VERSION: ' + JSMML.SWF_VERSION; | |
}; | |
JSMML.swfurl = 'JSMML.swf'; | |
JSMML.mmlDivID = 'jsmmlDiv'; | |
JSMML.mmlID = 'jsmml'; | |
JSMML.onLoad = function() {}; | |
JSMML.loaded = false; | |
JSMML.instances = {}; | |
JSMML.init = function(swfurl) { | |
if (! document.getElementById(JSMML.mmlDivID)) { | |
// init | |
var swfname = (swfurl ? swfurl : JSMML.swfurl) + '?' + (new Date()).getTime(); | |
var div = document.createElement('div'); | |
div.id = JSMML.mmlDivID; | |
div.style.display = 'inline'; | |
div.width = 1; | |
div.height = 1; | |
document.body.appendChild(div); | |
if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { | |
var o = document.createElement('object'); | |
o.id = JSMML.mmlID; | |
o.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'; | |
o.width = 1; | |
o.height = 1; | |
o.setAttribute('data', swfname); | |
o.setAttribute('type', 'application/x-shockwave-flash'); | |
var p = document.createElement('param'); | |
p.setAttribute('name', 'allowScriptAccess'); | |
p.setAttribute('value', 'always'); | |
o.appendChild(p); | |
div.appendChild(o); | |
} else { | |
// IE | |
var object = '<object id="' + JSMML.mmlID + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="1" height="1"><param name="movie" value="' + swfname + '" /><param name="bgcolor" value="#FFFFFF" /><param name="quality" value="high" /><param name="allowScriptAccess" value="always" /></object>'; | |
div.innerHTML = object; | |
} | |
} | |
} | |
// call from swf | |
JSMML.initASFinish = function() { | |
JSMML.loaded = true; | |
JSMML.onLoad(); | |
} | |
JSMML.eventInit = function() { | |
JSMML.init(); | |
} | |
JSMML.prototype = { | |
initialize: function() { | |
this.onFinish = function() {}; | |
this.pauseNow = false; | |
}, | |
uNum: function() { | |
if (!this._uNum) { | |
this._uNum = this.mmlPlayer._create(); | |
JSMML.instances[this._uNum] = this; | |
} | |
return this._uNum; | |
}, | |
play: function(_mml) { | |
if (!_mml && this.pauseNow) { | |
this.mmlPlayer._play(this.uNum()); | |
} else { | |
if (_mml) this.score = _mml; | |
this.mmlPlayer._play(this.uNum(), this.score); | |
} | |
this.pauseNow = false; | |
}, | |
stop: function() { | |
this.mmlPlayer._stop(this.uNum()); | |
}, | |
pause: function() { | |
this.pauseNow = true; | |
this.mmlPlayer._pause(this.uNum()); | |
}, | |
destroy: function() { | |
this.mmlPlayer._destroy(this.uNum()); | |
delete JSMML.instances[this.uNum()]; | |
} | |
/* | |
, | |
time: function() { | |
return this.mmlPlayer._time(this.uNum()); | |
} | |
*/ | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ダイアリーから移行して鳴らなくなった MML の記事内に下記をコピペするとプレイヤーが復活します。
(はてな記法での書き方なので、別の編集方法を採用している場合は別途読み替えてください。)