youku的js脚本的工具函数和初始化方法
定义日志输出函数
(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(){}
}
})();
定义全局的方法,实现加载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: relative 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'); };
})();
全站监听连接事件
//监听全站的链接点击事件,检查是否存在data-from属性,并添加from参数至href属性中
(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 && href.indexOf('?from') === -1 && href.indexOf('&from') === -1 && checkFromDomain(href)) {
from = encodeURIComponent(from);
if(href.indexOf('?') !== -1){
target.setAttribute("href", href+'&from='+from);
}else{
target.setAttribute("href", href+'?from='+from);
}
}
}
}
});
}
})(window);
生成时间戳+6位随机数的字符串
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异步调用方法
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(!/*@cc_on!@*/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);
}
}
}
}
过滤空字符串
function ltrim(s){ return s.replace( /^(\s*| *)/, ""); }
function rtrim(s){ return s.replace( /(\s*| *)$/, ""); }
function trim(s){ return ltrim(rtrim(s));}
判断是否为空值
/**
* 判断变量是否空值
* undefined, null, '', false, 0, [], {} 均返回true,否则返回false
*/
function empty(v){
switch (typeof v){
case 'undefined' : return true;
case 'string' : if(trim(v).length == 0) return true; break;
case 'boolean' : if(!v) return true; break;
case 'number' : if(0 === v) return true; break;
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;
}
根据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){
username = u_info.split(",")[1].split(":")[1];
if(username != '') break;
}
}
} return (username == '') ? false : true;
}
判断是否为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 & 15) << 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, -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超过限制
var enUid = 'U' + encode64(uid * 4);
}
return enUid;
}
youku的js脚本的工具函数和初始化方法的更多相关文章
- 【JS】403- JavaScript 工具函数大全(新)
前言 一线大厂笔试题灵感来源 目录: 第一部分:数组 第二部分:函数 第三部分:字符串 第四部分:对象 第五部分:数字 第六部分:浏览器操作及其它 筛选自以下两篇文章: <127 Helpful ...
- JS开发常用工具函数 总结
js原生工具库 1.isStatic:检测数据是不是除了symbol外的原始数据 */ function isStatic(value) { return( typeof value === 'str ...
- js常用的工具函数
JS选取DOM元素的方法注意:原生JS选取DOM元素比使用jQuery类库选取要快很多1.通过ID选取元素document.getElementById('myid');2.通过CLASS选取元素do ...
- js 常用的工具函数
1 类型判断 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } i ...
- JS开发常用工具函数
1.isStatic:检测数据是不是除了symbol外的原始数据 function isStatic(value) { return ( typeof value === 'string' || ty ...
- 初始化加载和导航时脚本执行的函数(初始化脚本执行环境)page.evaluateOnNewDocument
/** * Copyright 2017 Google Inc., PhantomJS Authors All rights reserved. * * Licensed under the Apac ...
- 【转】第6篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:自动注册JS脚本+自动反射方法分析
作者: 牛A与牛C之间 时间: 2013-11-21 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第6篇:Xilium CefGlue 关于 CLR Object 与 JS ...
- IE11开发人员工具 js脚本debugger调试
微软在浏览器IE11中对F12开发人员工具的UI进行了彻底的换脸,虽然有诸多的不习惯但也是有方便之处的,比如我们用到的最多的断点调试,在F12调试状态下会加载出页面的js脚本文件,而需要对某个文件中的 ...
- Dynamics CRM 365 不用按钮工具,直接用js脚本控制按钮的显示隐藏
Dynamics CRM 365 不用按钮工具,直接用js脚本控制按钮的显示隐藏: try { // 转备案按钮 let transferSpecialRequestButton = parent.p ...
随机推荐
- BZOJ2741: 【FOTILE模拟赛】L
2741: [FOTILE模拟赛]L Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1170 Solved: 303[Submit][Status] ...
- sql点滴39—解决数据库日志文件过大的问题
随着数据库使用时间增长,日志文件也在不停的增大,这里介绍几种方法减小这个文件的方法. 1.直接删除log文件(一般不建议) 分离数据库.分离数据库之前一定要做好数据库的全备份,选择数据库——右键——任 ...
- mysql 查看死锁和去除死锁
1.查询是否锁表show OPEN TABLES where In_use > 0; 2.查询进程 show processlist 3. 查询到相对应的进程,然后 kill id 验证(ki ...
- linux驱动程序之电源管理之linux的电源管理架构(3)
设备电源管理 Copyright (c) 2010 Rafael J. Wysocki<rjw@sisk.pl>, Novell Inc. Copyright (c) 2010 Alan ...
- Pascal's Triangle II
1.题目描述 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Re ...
- Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers
/* 可以在筛选质数的同时,算出每组数据中能被各个质数整除的个数, 然后算出[0,s]的个数 [l,r] 的个数即为[0,r]的个数减去[0,l]个数. */ #include <stdio.h ...
- puppet重申证书
直接上步骤,由于测试用的是PE3.X版本,在网上搜的命令几乎与PE相关的puppet命令不同了, 1.在PE-Client操作,停止pe-puppet,pe-mcollective资源; puppet ...
- iOS DES 加密转base64
//用法 加密转base 64 NSString *str = [self base64StringFromText:@"qingjoin" withKey:@"ke ...
- iptables与tcpdump谁更靠近网卡
结论是tcpdump,用tcpdump抓包试试就知道了. // 将所有访问8080端口的包丢弃 iptables -A INPUT -s -j DROP // 查看iptables的策略 iptabl ...
- SRM566 1000pts
绍一的模拟赛题 [题意] 小Z养了$