// ==SiteScript==
// @siteName NhacSo
// @siteUrl http://nhacso.net/
// @author mayan
// @authorUrl
// @scriptUrl
// @description
// @date 2012/07/05
// @version 0.1
// ==/SiteScript==
function CravingSiteScript() {
this._initialize();
}
CravingSiteScript.prototype = {
_xhr: null,
_initialize: function() {},
_getXmlHttpRequest: function() {
if ( this._xhr != null ) {
return this._xhr;
}
var xhr = null;
var these = [
function() { return new XMLHttpRequest(); }
, function() { return new ActiveXObject( "Msxml2.XMLHTTP" ); }
, function() { return new ActiveXObject( "Microsoft.XMLHTTP" ); }
, function() { return new ActiveXObject( "Msxml2.XMLHTTP.4.0" ); }
];
for ( var i = 0, length = these.length; i < length; i++ ) {
var func = these[ i ];
try {
xhr = func();
break;
}
catch( e ) {}
}
this._xhr = xhr;
return this._xhr;
},
_load: function( url, data, method ) {
var req = this._getXmlHttpRequest();
var mtd = ( method == null ) ? "GET" : "POST";
req.open( mtd, url, false );
if ( mtd == "POST" ) {
req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
}
req.send( data );
return req.responseText;
},
getResponseText: function( url, data, method ) {
return this._load( url, data, method );
},
getResponseJSON: function( url, data, method ) {
var text = this._load( url, data, method );
return eval( "("+text+")" );
},
/// Math
random: function( limit ) {
return Math.floor( Math.random() * limit );
},
/// String
decodeHtml: function( str ) {
return str.replace( /&(quot|#0*34);/ig, "\"" )
.replace( /&(amp|#0*38);/ig, "&" )
.replace( /&(apos|#0*39);/ig, "'" )
.replace( /&(lt|#0*60);/ig, "<" )
.replace( /&(gt|#0*62);/ig, ">" )
.replace( /&(nbsp|#0*160);/ig, " " )
.replace( /&(frasl|#8260);/ig, "/" );
}
}
var _NhacSo = {};
_NhacSo.addArtistName = true;
_NhacSo.addAlbumName = true;
function isSiteUrl( url ) {
if ( url.match( /http:\/\/(www\.|tinhca\.)?nhacso\.net\/(xem-video|nghe-nhac|nghe-album|nghe-playlist)\/[^\/]*\.[^.]+\.html/ ) ) return true;
if ( url.match( /http:\/\/(www\.|tinhca\.)?nhacso\.net\/embed\/(video|song|album|playlist)\/[^\/?]+/ ) ) return true;
if ( url.match( /http:\/\/st\.nhacso\.net\/flash\/[^?]+\.swf(\?.+)/ )
&& RegExp.$1.match( /[?&]xmlPath=[^]+/ ) ) return true;
return false;
}
function getVideoDetail( url ) {
url = url.replace( /http:\/\/(www\.|tinhca\.)nhacso\.net\//, "http://nhacso.net/" );
if ( url.match( /http:\/\/nhacso\.net\/(xem-video|nghe-nhac|nghe-album|nghe-playlist)\/[^\/]*\.([^.]+)\.html/ ) ) {
var vid = RegExp.$2;
var typeU = RegExp.$1;
var typeP = "page";
} else if ( url.match( /http:\/\/nhacso\.net\/embed\/(video|song|album|playlist)\/([^\/?]+)/ ) ) {
vid = RegExp.$2;
typeU = RegExp.$1;
typeP = "iframe";
} else if ( url.match( /http:\/\/st\.nhacso\.net\/flash\/[^?]+\.swf(\?.+)/ )
&& RegExp.$1.match( /[?&]xmlPath=([^]+)/ ) ) {
typeP = "swf";
var xmlurl = RegExp.$1;
} else {
return null;
}
var craving = new CravingSiteScript();
if ( typeP == "page" || typeP == "iframe" ) {
try {
var text = craving.getResponseText( url );
} catch( e ) {
text = "";
}
if ( text && typeP == "page" ) {
if ( text.match( /]*?rel\s*=\s*("|')video_src\2[^>]*)>/ )
&& RegExp.$1.match( /\s+href\s*=\s*("|')(.*?)\1/ )
&& RegExp.$2.match( /[?&]xmlPath=([^]+)/ ) ) {
xmlurl = RegExp.$1.replace( /&/g, "&" );
} else if (
text.match( /]*?name\s*=\s*("|')idLink(Share|Playlist)ForForum\2[^>]*)>/ )
&& RegExp.$1.match( /\s+value\s*=\s*("|')(.*?)\1/ )
&& RegExp.$2.match( /[?&]xmlPath=([^]+)/ ) ) {
xmlurl = RegExp.$1.replace( /&/g, "&" );
}
}
if ( text && typeP == "iframe" ) {
if ( text.match( /xmlPath\s*:\s*("|')(.*?)\1/ ) ) {
xmlurl = RegExp.$2;
}
}
}
if ( !xmlurl ) {
if ( typeU == "xem-video" || typeU == "video" )
xmlurl = "http://nhacso.net/flash/video/xnl/1/id/" + vid;
if ( typeU == "nghe-nhac" || typeU == "song" )
xmlurl = "http://nhacso.net/flash/song/xnl/1/id/" + vid;
}
if ( !xmlurl ) return null;
if ( !typeU ) typeU = xmlurl.split( "/" )[4];
if ( !typeU ) typeU = "unknown";
try {
text = craving.getResponseText( xmlurl );
} catch( e ) {
text = "";
}
if ( !text ) return null;
switch( typeU ) {
case "xem-video":
case "video":
if ( text.match( /()?<\/sourceUrl>/ ) )
var realUrl = RegExp.$2;
if ( !realUrl ) return null;
if ( text.match( /()?<\/name>/ ) )
var title = RegExp.$2;
if ( !title ) title = "nhacso_" + vid;
title = title.replace(/[\\\/:*?"<>|]/g, "_");
return { videoTitle0: title, videoUrl0: realUrl };
case "nghe-nhac":
case "song":
case "nghe-album":
case "album":
case "nghe-playlist":
case "playlist":
if ( text.match( /()?<\/name>/ ) )
var albumname = RegExp.$2;
var arrRet = [];
var objReg = /([\s\S]+?)<\/song>/gm;
var count = 0;
while ( objReg.test( text ) ) {
var strS = RegExp.$1;
var obj = {};
count++;
if ( strS.match( /()?<\/mp3link>/ ) )
obj.realUrl = RegExp.$2;
if ( !obj.realUrl ) continue;
if ( strS.match( /()?<\/name>/ ) )
obj.title = RegExp.$2;
if ( !obj.title ) obj.title = "nhacso_" + vid + "_" + count;
if ( strS.match( /()?<\/artist>/ ) )
obj.artist = RegExp.$2;
if ( _NhacSo.addArtistName = true && obj.artist ) obj.title = obj.artist + " - " + obj.title;
if ( _NhacSo.addAlbumName = true && albumname ) obj.title = obj.title + " ~ " + albumname;
obj.title = obj.title.replace(/[\\\/:*?"<>|]/g, "_");
arrRet.push( obj );
}
if ( arrRet.length <= 0 ) return null;
var objRet = {};
for ( var i = 0; i < arrRet.length; i++ ) {
objRet[ "videoTitle" + i ] = arrRet[i].title;
objRet[ "videoUrl" + i ] = arrRet[i].realUrl;
}
return objRet;
default:
if ( text.match( /<(sourceUrl|mp3link)>()?<\/\1>/ ) )
realUrl = RegExp.$3;
if ( !realUrl ) return null;
if ( text.match( /()?<\/name>/ ) )
title = RegExp.$2;
if ( !title ) title = "nhacso_" + vid;
title = title.replace(/[\\\/:*?"<>|]/g, "_");
return { videoTitle0: title, videoUrl0: realUrl };
}
return null;
}