* fixed rewrite ogg handler
[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
28 }
29
30 // Firefogg integration
31 if( wgPageName == "Special:Upload" ){
32 load_mv_embed( function() {
33 loadExternalJs( mwEmbedHostPath + '/uploadPage.js' + reqAguments );
34 } );
35 }
36
37 // Special api proxy page
38 if( wgPageName == 'MediaWiki:ApiProxy' ){
39 var wgEnableIframeApiProxy = true;
40 load_mv_embed( function() {
41 js_log("Wiki:ApiProxy::");
42 loadExternalJs( mwEmbedHostPath + '/apiProxyPage.js' + reqAguments );
43 });
44 }
45
46 // OggHandler rewrite for view pages:
47 var vidIdList = [];
48 var divs = document.getElementsByTagName( 'div' );
49 for( var i = 0; i < divs.length; i++ ) {
50 if( divs[i].id && divs[i].id.substring( 0, 11 ) == 'ogg_player_' ) {
51 vidIdList.push( divs[i].getAttribute( "id" ) );
52 }
53 }
54 if( vidIdList.length > 0 ) {
55 load_mv_embed( function() {
56 mvJsLoader.embedVideoCheck( function() {
57 // Do utility rewrite of OggHandler content:
58 rewrite_for_OggHandler( vidIdList );
59 } );
60 } );
61 }
62 }
63 // will be depreciated in favor of updates to OggHandler
64 function rewrite_for_OggHandler( vidIdList ){
65 function procVidId( vidId ){
66 js_log('vidIdList length: ' + vidIdList.length + ' left in the set: ' + vidIdList );
67
68 // Grab the thumbnail and src of the video
69 var pimg = $j( '#' + vidId + ' img' );
70 var poster_attr = 'poster = "' + pimg.attr( 'src' ) + '" ';
71 var pwidth = pimg.attr( 'width' );
72 var pheight = pimg.attr( 'height' );
73
74 var tag_type = 'video';
75
76 // Check for audio
77 if( pwidth == '22' && pheight == '22' ) {
78 //set width to parent width:
79 pwidth = $j( '#' + vidId ).width();
80 pheight = '100';
81 tag_type = 'audio';
82 poster_attr = '';
83 }
84
85 // Parsed values:
86 var src = '';
87 var duration = '';
88 var wikiTitleKey = $j( '#'+vidId + ' img').filter(':first').attr('alt').replace(/ /g, '_');
89 var re = new RegExp( /videoUrl(&quot;:?\s*)*([^&]*)/ );
90 src = re.exec( $j( '#'+vidId ).html() )[2];
91
92 var re = new RegExp( /length(&quot;:?\s*)*([^&]*)/ );
93 duration = re.exec( $j( '#'+vidId ).html() )[2];
94
95 var re = new RegExp( /offset(&quot;:?\s*)*([^&]*)/ );
96 offset = re.exec( $j( '#'+vidId ).html() )[2];
97 var offset_attr = offset ? 'startOffset="' + offset + '"' : '';
98
99 if( src ) {
100 var html_out = '';
101
102 var common_attr = ' id="vid_' + i +'" '+
103 'wikiTitleKey="' + wikiTitleKey + '" ' +
104 'src="' + src + '" ' +
105 'durationHint="' + duration + '" ' +
106 offset_attr + ' ';
107
108 if( tag_type == 'audio' ){
109 html_out='<audio' + common_attr + '></audio>'
110 }else{
111 html_out='<video' + common_attr +
112 poster_attr + ' ' +
113 'style="width:' + pwidth + 'px;height:' + pheight + 'px;">' +
114 '</video>';
115 }
116 //set the video tag inner html and update the height
117 $j( '#' + vidId ).html( html_out )
118 .css('height', pheight + 30);
119
120 }
121 rewrite_by_id( vidId, function(){
122 if(vidIdList.length != 0){
123 alert('did first rewite now doing another');
124 setTimeout( function(){
125 procVidId( vidIdList.pop() )
126 }, 1);
127 }
128 });
129 };
130 //process each item in the vidIdList (with setTimeout to avoid locking)
131 setTimeout( function(){
132 procVidId( vidIdList.pop() )
133 }, 1);
134 }
135 function getRemoteEmbedPath() {
136 for( var i = 0; i < document.getElementsByTagName( 'script' ).length; i++ ) {
137 var s = document.getElementsByTagName( 'script' )[i];
138 if( s.src.indexOf( '/remoteMwEmbed.js' ) != -1 ) {
139 var reqStr = '';
140 var scriptPath = '';
141 if( s.src.indexOf( '?' ) != -1) {
142 reqStr = s.src.substr( s.src.indexOf( '?' ) );
143 scriptPath = s.src.substr( 0, s.src.indexOf( '?' ) ).replace( '/remoteMwEmbed.js', '' );
144 } else {
145 scriptPath = s.src.replace( '/remoteMwEmbed.js', '' )
146 }
147 // Use the external_media_wizard path:
148 return [scriptPath, reqStr];
149 }
150 }
151 }
152
153 function load_mv_embed( callback ) {
154 // Inject mv_embed if needed
155 if( typeof $mw == 'undefined' ) {
156 var mvurl = mwEmbedHostPath + '/mwEmbed/mv_embed.js' + reqAguments ;
157 importScriptURI( mvurl );
158 }
159 check_for_mv_embed( callback );
160 }
161
162 function check_for_mv_embed( callback ) {
163 if( typeof $mw == 'undefined' ) {
164 setTimeout( function(){
165 check_for_mv_embed( callback );
166 }, 25 );
167 } else {
168 callback();
169 }
170 }