The tool function and initialization method of Youku JS script

Source: Internet
Author: User
Tags rtrim

Defining log output functions
(function () {if (window[' console ')} {return;} window[' console '] = {log:function () {},clear:function () {},debug:function () {},error:function () {},info:function () {} , Count:function () {},time:function () {},trace:function () {},warn:function () {}}}) ();
Defines the global method that implements the address of the response that loads the CDN
(function () {var url = ' bad ' url, watch browser console error. ', Local = window. Local, err = function (msg) {if (console && console.error) {console.error (' [CDN function Error] ' + msg + '). ); }}, CDN = function (path, type) {//path rule if (Path.charat (0)! = '/') {err (' @param path:rel Ative to root start by/'); return URL; }//global block if (!            Local) {err (' @see beta-18932:template funciton {nova->globajs} '); return URL;}            var key = ' Release_tag ', Version = Local[key];            Version define if (!version) {err (' @see Local: ' + key + ' not defined '); return URL;}            Server Define key = Type.touppercase () + ' server ', server = Local[key];            if (!server) {err (' @see Local: ' + key + ' not defined. '); return URL;}  Bad server define if (!server.match (/^ (HTTP|HTTPS)/)} {Err (' @see Local: ' + key + ' is server, add Protocol '); return URL;  }          if (Server.match (/\/$/)) {err (' @see Local: ' + key + ' is server, not ending by \ \ '); return URL;}            url = server+ '/' + version + path;        return URL;        };        Window.cdn_jsurl = function (path) {return CDN (path, ' JS ');};        Window.cdn_cssurl = function (path) {return CDN (path, ' CSS ');}; Window.cdn_imgurl = function (path) {return CDN (path, ' img ');};}) ();
        Full station monitoring connection events
Listen for the entire station link Click event, check for the existence of the Data-from property, and add the from parameter to the HREF attribute (function (o) {if (!o) {return false;} if (!o.event) {return false;} var checkfromdomain = function (href) {var domain = ["youku.com/v_show/", "youku.com/show_page/", "youku.com/u/u"];for ( var i=0,length=domain.length; i<length; i++) {if (Href.indexof (Domain[i])!==-1) {return true;}}} O.nova_init_hook_data_from = function () {O.event.observe (document, "click", Function (EV) {if (!ev) ev = O.event;var target = Ev.target | | Ev.srcelement;if (!target) {return;} if (target.tagname! = ' A ' && target.parentnode) {target = Target.parentnode;}  if (Target.tagname = = ' A ') {var data_from = Target.getattribute ("Data-from"); var from = Data_from | | window.pvdatafrom;var href = Target.getattribute ("href"), if (from && from! = ") {if (href && href.indexof (' http ') = = = 0 &&am P Href.indexof ('? from ') = = =-1 && href.indexof (' &from ') = = = 1 && checkfromdomain (href)) {from = encode Uricomponent (from), if (Href.indexof ('? ')!==-1) {TargEt.setattribute ("href", href+ ' &from= ' +from);} Else{target.setattribute ("href", href+ '? from= ' +from);}}});} }) (window);
Generate a timestamp + 6-bit random number string
var getpvid = function (len) {var randchar=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "s", "T", "U", "V", "w", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" "," K "," L "," M "," N "," O "," P "," Q "," R "," S "," T "," U "," V "," W "," X "," Y "," Z "];var i=0;var r=" ", var d=new Date (); for (i=0;i< len;i++) {         var index=parseint (Math.random () *math.pow (10,6))%randchar.length;          R+=randchar[index];} Return D.gettime () +r;}
Jsonp Asynchronous Call method
var jsonp = function (src, callback) {if (typeof (Arguments[0])! = ' string ') {return;} var callback = typeof (Arguments[1]) = = ' function '? Callback:function () {};var head = document.getElementsByTagName (' head ') [0];var script = Document.createelement (' Script '); script.type = ' text/javascript '; script.src = src;head.appendchild (script); if (!/*@[email protected]*/0) { Script.onload = function () {callback (); This.parentNode.removeChild (this);}} Else{script.onreadystatechange = function () {if (this.readystate = = ' Loaded ' | | this.readystate = = ' complete ') {callback ( ); This.parentNode.removeChild (this);}}}
Filter Empty strings
function LTrim (s) {return s.replace (/^ (\s*| *)/, "");} function RTrim (s) {return s.replace (/(\s*| *) $/, "");} function Trim (s) {return LTrim (RTrim (s));}
Determine if the value is null
/** * Determine if the variable is null * undefined, NULL, ', FALSE, 0, [], {} return True, otherwise false */function empty (v) {    switch (typeof v) {        C Ase ' undefined ': return true;        Case ' String '    : if (Trim (v). Length = = 0) return true;        Case ' Boolean '   : if (!v) return true;        Case ' number '    : if (0 = = = v) return true;        Case ' object '    :            if (null = = = v) return true;            if (undefined!== v.length && v.length==0) return true;            For (var k in V) {return false;} return true;            break;    }    return false;}
Determine if you are logged in by cookie
var islogin = function () {var username = '; var ca = Document.cookie.split (';'); for (Var i=0;i < ca.length;i++) {var c = ca[i];while (C.charat (0) = = ") c = c.substring (1,c.length); if (C.indexof (' yktk= ') ) = = 0) {var u_info = decode64 (decodeURIComponent (c). Split ("|") [3]); if (U_info.indexof (",") >-1 && u_info.indexof ("NN:") >-1 && u_info.indexof ("ID:") >-1) {Userna me = U_info.split (",") [1].split (":") [1]; if (username! = ") Break;}}} return (username = = ")? False:true;}
Decide whether to email
function isemail (mail) {return (New RegExp (/^\w+ (-\w+) | ( \.\w+)) *\@[a-za-z0-9]+ ((\.| -) [a-za-z0-9]+] *\. [A-za-z] {1,4}$/). Test (mail));}



function U8_16 (_1) {
var i, Len, C;
var char2, Char3;
var ary = [];
len = _1.length;
i = 0;
while (I < Len) {
c = _1.charcodeat (i++);
Switch (c >> 4) {
Case 0:
Case 1:
Case 2:
Case 3:
Case 4:
Case 5:
Case 6:
Case 7:
0xxxxxxx
Ary.push (_1.charat (i-1));
Break
Case 12:
Case 13:
110x xxxx 10xx xxxx
CHAR2 = _1.charcodeat (i++);
Ary.push (String.fromCharCode ((C & 0x1F) << 6) | (Char2 & 0x3F)));
Break
Case 14:
1110 xxxx 10xx xxxx 10xx xxxx
CHAR2 = _1.charcodeat (i++);
CHAR3 = _1.charcodeat (i++);
Ary.push (String.fromCharCode ((C & 0x0F) << 12) | ((Char2 & 0x3F) << 6) | ((Char3 & 0x3F) << 0)));
Break
}
}
Return Ary.join (");
}
function Decode64 (_1) {
if (!_1) return ';
var _2 = "Abcdefghijklmnop" + "qrstuvwxyzabcdef" + "GHIJKLMNOPQRSTUV" + "wxyz0123456789+/" + "=";
var _3 = "";
var _4, _5, _6;
var _7, _8, _9, _a;
var i = 0;
_1 = _1.replace (/[^a-za-z0-9\+\/\=]/g, "");
do {
_7 = _2.indexof (_1.charat (i++));
_8 = _2.indexof (_1.charat (i++));
_9 = _2.indexof (_1.charat (i++));
_a = _2.indexof (_1.charat (i++));
_4 = (_7 << 2) | (_8 >> 4);
_5 = ((_8 &) << 4) | (_9 >> 2);
_6 = ((_9 & 3) << 6) | _a;
_3 = _3 + String.fromCharCode (_4);
if (_9! = 64) {
_3 = _3 + String.fromCharCode (_5);
}
if (_a! = 64) {
_3 = _3 + String.fromCharCode (_6);
}
} while (I < _1.length);
Return u8_16 (_3);
}
function Encode64 (str)
{
if (!STR) return ';
str = str.tostring ();
var base64encodechars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64decodechars = new Array (
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 62,-1,-1,-1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,-1,-1,-1,-1,-1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1, 1
);
var out, I, Len;
var C1, C2, C3;
len = str.length;
i = 0;
out = "";
while (I < Len) {
C1 = Str.charcodeat (i++) & 0xFF;
if (i = = len)
{
Out + = Base64encodechars.charat (C1 >> 2);
Out + = Base64encodechars.charat ((C1 & 0x3) << 4);
Out + = "=";
Break
}
C2 = Str.charcodeat (i++);
if (i = = len)
{
Out + = Base64encodechars.charat (C1 >> 2);
Out + = Base64encodechars.charat (((C1 & 0x3) << 4) | ((C2 & 0xF0) >> 4));
Out + = Base64encodechars.charat ((C2 & 0xF) << 2);
Out + = "=";
Break
}
C3 = Str.charcodeat (i++);
Out + = Base64encodechars.charat (C1 >> 2);
Out + = Base64encodechars.charat (((C1 & 0x3) << 4) | ((C2 & 0xF0) >> 4));
Out + = Base64encodechars.charat (((C2 & 0xF) << 2) | ((C3 & 0xC0) >>6));
Out + = Base64encodechars.charat (C3 & 0x3F);
}
return out;
}
function Encodeuid (UID)
{
if (!uid) return ';
if (UID << 2 > 0) {
var enuid = ' U ' + encode64 (UID << 2);
}else{
UID exceeds limit
var enuid = ' U ' + encode64 (UID * 4);
}
return enuid;
}

The tool function and initialization method of Youku JS script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.