* some subtitles fixes
[lhc/web/wiklou.git] / js2 / remoteMwEmbed.js
1 /*
2 * this file exposes some of the functionality of mwEmbed to wikis
3 * that do not yet have js2 enabled
4 */
5
6 var urlparts = getRemoteEmbedPath();
7 var mwEmbedHostPath = urlparts[0];
8 var reqAguments = urlparts[1];
9
10 addOnloadHook( function(){
11 //only do rewrites if MV_EMBED / js2 is "off"
12 if( typeof MV_EMBED_VERSION == 'undefined' ) {
13 doPageSpecificRewrite();
14 }
15 });
16
17 function doPageSpecificRewrite() {
18 // Add media wizard
19 if( wgAction == 'edit' || wgAction == 'submit' ) {
20 load_mv_embed( function() {
21 loadExternalJs( mwEmbedHostPath + '/editPage.js' + reqAguments );
22 } );
23 }
24
25 //timed text dispaly:
26 if(wgPageName.indexOf("TimedText") === 0){
27 load_mv_embed( function() {
28 loadExternalJs( mwEmbedHostPath + '/mwEmbed/libTimedText/mvTimeTextEdit.js' + reqAguments );
29 } );
30 }
31
32 // Firefogg integration
33 if( wgPageName == "Special:Upload" ){
34 load_mv_embed( function() {
35 loadExternalJs( mwEmbedHostPath + '/uploadPage.js' + reqAguments );
36 } );
37 }
38
39 // Special api proxy page
40 if( wgPageName == 'MediaWiki:ApiProxy' ){
41 var wgEnableIframeApiProxy = true;
42 load_mv_embed( function() {
43 js_log("Wiki:ApiProxy::");
44 loadExternalJs( mwEmbedHostPath + '/apiProxyPage.js' + reqAguments );
45 });
46 }
47
48 // OggHandler rewrite for view pages:
49 var vidIdList = [];
50 var divs = document.getElementsByTagName( 'div' );
51 for( var i = 0; i < divs.length; i++ ) {
52 if( divs[i].id && divs[i].id.substring( 0, 11 ) == 'ogg_player_' ) {
53 vidIdList.push( divs[i].getAttribute( "id" ) );
54 }
55 }
56 if( vidIdList.length > 0 ) {
57 load_mv_embed( function() {
58 mvJsLoader.embedVideoCheck( function() {
59 // Do utility rewrite of OggHandler content:
60 rewrite_for_OggHandler( vidIdList );
61 } );
62 } );
63 }
64 }
65 // will be depreciated in favor of updates to OggHandler
66 function rewrite_for_OggHandler( vidIdList ){
67 function procVidId( vidId ){
68 //don't process empty vids
69 if(!vidId)
70 return ;
71 js_log('vidIdList on: ' + vidId +' length: ' + vidIdList.length + ' left in the set: ' + vidIdList );
72
73 // Grab the thumbnail and src of the video
74 var pimg = $j( '#' + vidId + ' img' );
75 var poster_attr = 'poster = "' + pimg.attr( 'src' ) + '" ';
76 var pwidth = $j( '#' + vidId).width();
77 var pheight = $j( '#' + vidId ).height();
78
79 var tag_type = 'video';
80
81 // Check for audio
82 if( pheight == '22' || pheight == '52') {
83 //set width to parent width:
84 tag_type = 'audio';
85 poster_attr = '';
86 }
87
88 // Parsed values:
89 var src = '';
90 var duration_attr = '';
91 var wikiTitleKey = $j( '#'+vidId + ' img').filter(':first').attr('src').split('/');
92 wikiTitleKey = unescape( wikiTitleKey[ wikiTitleKey.length - 2 ] );
93 var re = new RegExp( /videoUrl(&quot;:?\s*)*([^&]*)/ );
94 src = re.exec( $j( '#'+vidId ).html() )[2];
95
96 var re = new RegExp( /length(&quot;:?\s*)*([^,]*)/ );
97 var dv = re.exec( $j( '#'+vidId ).html() )[2];
98 if( dv ){
99 duration_attr = 'durationHint="'+ dv +'" ';
100 }
101
102 var re = new RegExp( /offset(&quot;:?\s*)*([^&]*)/ );
103 offset = re.exec( $j( '#'+vidId ).html() )[2];
104 var offset_attr = offset ? 'startOffset="' + offset + '"' : '';
105
106 if( src ) {
107 var html_out = '';
108
109 var common_attr = ' id="mwe_' + vidId +'" '+
110 'wikiTitleKey="' + wikiTitleKey + '" ' +
111 'src="' + src + '" ' +
112 duration_attr +
113 offset_attr + ' ';
114
115 if( tag_type == 'audio' ){
116 html_out='<audio' + common_attr + ' style="width:' + pwidth + 'px;"></audio>';
117 }else{
118 html_out='<video' + common_attr +
119 poster_attr + ' ' +
120 'style="width:' + pwidth + 'px;height:' + pheight + 'px;">' +
121 '</video>';
122 }
123 //set the video tag inner html and update the height
124 $j( '#' + vidId ).html( html_out )
125 .css('height', pheight + 30);
126
127 }
128 rewrite_by_id( 'mwe_' + vidId, function(){
129 if( vidIdList.length != 0 ){
130 setTimeout( function(){
131 procVidId( vidIdList.pop() )
132 }, 1);
133 }
134 });
135 };
136 //process each item in the vidIdList (with setTimeout to avoid locking)
137 procVidId( vidIdList.pop() );
138 }
139 function getRemoteEmbedPath() {
140 for( var i = 0; i < document.getElementsByTagName( 'script' ).length; i++ ) {
141 var s = document.getElementsByTagName( 'script' )[i];
142 if( s.src.indexOf( '/remoteMwEmbed.js' ) != -1 ) {
143 var reqStr = '';
144 var scriptPath = '';
145 if( s.src.indexOf( '?' ) != -1) {
146 reqStr = s.src.substr( s.src.indexOf( '?' ) );
147 scriptPath = s.src.substr( 0, s.src.indexOf( '?' ) ).replace( '/remoteMwEmbed.js', '' );
148 } else {
149 scriptPath = s.src.replace( '/remoteMwEmbed.js', '' )
150 }
151 // Use the external_media_wizard path:
152 return [scriptPath, reqStr];
153 }
154 }
155 }
156
157 function load_mv_embed( callback ) {
158 // Inject mv_embed if needed
159 if( typeof $mw == 'undefined' ) {
160 var mvurl = mwEmbedHostPath + '/mwEmbed/mv_embed.js' + reqAguments ;
161 importScriptURI( mvurl );
162 }
163 check_for_mv_embed( callback );
164 }
165
166 function check_for_mv_embed( callback ) {
167 if( typeof $mw == 'undefined' ) {
168 setTimeout( function(){
169 check_for_mv_embed( callback );
170 }, 25 );
171 } else {
172 callback();
173 }
174 }