在最近的一個項目中,我使用了以下技術:
void VideoWebPage::urlLoaded(bool ok)
{
static const QString javascript =
"function installCallbacks() " \
"{ " \
" var videoTags = document.getElementsByTagName('object'); " \
" for (var i = 0; i < videoTags.length; ++i) " \
" { " \
" if (videoTags[i].type == 'application/x-qt-plugin') " \
" { " \
" if (videoTags[i].playing) " \
" { " \
" videoTags[i].playing.connect(playingSlot); " \
" } " \
" } " \
" } " \
"} " \
\
"function playingSlot(videoId) " \
"{ " \
" var playEvent=document.createEvent('Events'); " \
" playEvent.initEvent('play', true, false); " \
" document.getElementById(videoId).dispatchEvent(playEvent); " \
"} " \
"installCallbacks(); ";
mainFrame()->evaluateJavaScript(javascript);
}
這個方法查找所有<object>
標籤和playing
信號連接到JavaScript函數playingSlot()
。 playingSlot()
函數依次創建名稱爲play
的Event
對象,並將其作爲普通DOM事件進行分派。然後 HTML文件看起來像:
<html>
<head>
<script language="text/javascript">
void init()
{
document.getElementById('id1').addEventListener('play', onPlay);
}
void onPlay(event)
{
alert('playing');
}
</script>
</head>
<body onload='init()'>
<object id='id1' type='application/x-qt-plugin'>
</object>
</body>
</html>
這當然是使用Qt的插件小工具的工作。我沒有用純HTML進行測試(即沒有使用Qt插件的地方)。