1. 程式人生 > >專案原創:js獲取網站搜尋來原始碼

專案原創:js獲取網站搜尋來原始碼

把以下程式碼引入所有頁面,即可實現收集系統來源的功能,在任意頁面中都可以使用全域性的統一的來源資訊

var args = {
    mySite:"localhost",
    subSite:"子網站,比如移動端: m.xxx.com"
}
var index = (function(a) {
    var c = "";
    var d = args.mySite;
    a(function() {
        var g = function() {
            return function(k, l, j) {
                var h = j * 24 * 60 * 60 * 1000;
                var i = new Date();
                i.setTime(i.getTime() + h);
                var e = "; expires=" + i.toGMTString();
                document.cookie = k + "=" + escape(l) + e + "; path=/"
            }
        };
        try {
            if (document.referrer.indexOf(d) <= 0 || document.referrer.indexOf(d) > 10) {
                g().call(window, "regCompanyName", document.referrer.substring(0, 40) + "||" + window.location.href, 30)
            }
        } catch (f) {

        }
    });
    var b = function() {
        try {
            if (getCookie) {
                return getCookie
            }
        } catch (f) {}
        return function(e) {
            if (document.cookie.length > 0) {
                c_start = document.cookie.indexOf(e + "=");
                if (c_start != -1) {
                    c_start = c_start + e.length + 1;
                    c_end = document.cookie.indexOf(";", c_start);
                    if (c_end == -1) {
                        c_end = document.cookie.length
                    }
                    return unescape(document.cookie.substring(c_start, c_end))
                }
            }
            return null
        }
    };
})(jQuery);

//媒體
var media = "";
//媒體細分
var mediaSubdivide = "";
//註冊渠道。(直接賦值)
var registerChannel = "";
//終端,根據使用者使用裝置判斷
var terminal = "";
//資料來源,根據cookie確定
var dataSource = "";
//當前連結
var localUrl = window.location.href;

var userChannel = {
    //媒體
    Media: function(channelInfo){
        channelInfo=channelInfo.toLowerCase();
        if(channelInfo.indexOf("||") == 0 && (channelInfo.indexOf(args.mySite) >= 0)){
            //媒體  無法判斷(如直接搜尋) 
            media = "網址開啟或站內跳轉";  //媒體
            mediaSubdivide =  "";       //媒體細分

            // 無法獲取referrer的來源的情況有:
            //1、個別平臺外鏈使用noreferrer;
            //2、https跳轉http;
            //3、網頁重定向...
            //可在外鏈中植入可判定的引數如www.xxx.com&tag=zhihu
            if(channelInfo.indexOf("zhihu")>=0){
                media = "知乎";
                mediaSubdivide = "";
            }
        }else if(channelInfo.indexOf("baidu.com") != -1){
            media = "百度";                       //媒體
            if(channelInfo.indexOf('utm_medium=cpc') != -1){
                mediaSubdivide = "sem";         //媒體細分
            }else if(channelInfo.indexOf('utm_medium=cpc') == -1){
                mediaSubdivide = "seo";         //媒體細分
            }else{
                mediaSubdivide = "其它";      //媒體細分
            }
        }else if(channelInfo.indexOf('haosou.com') != -1 || channelInfo.indexOf('so.com') != -1){
            media = "好搜";                       //媒體
            mediaSubdivide = "無法判斷";        //媒體細分
        }else if(channelInfo.indexOf('sogou.com') != -1){
            media = "搜狗";                       //媒體
            mediaSubdivide = "無法判斷";        //媒體細分
        }else if(channelInfo.indexOf('sm.cn') != -1){
            media = "神馬";                       //媒體
            mediaSubdivide = "無法判斷";        //媒體細分
        }else if(channelInfo.indexOf('bing.com') != -1){
            media = "必應";                       //媒體
            mediaSubdivide = "無法判斷";        //媒體細分
        }else if(channelInfo.indexOf('google.com') != -1){
            media = "google";                       //媒體
            mediaSubdivide = "無法判斷";        //媒體細分
        }else if(channelInfo.indexOf('douban.com') != -1){
            media = "豆瓣";                       //媒體
            mediaSubdivide = "無法判斷";        //媒體細分
        }else if(channelInfo.indexOf("zhihu.com") != -1){
            media = "知乎";
            mediaSubdivide = "無法判斷";        
        }   
        else if(channelInfo.indexOf("toutiao") != -1){
            media = "今日頭條";
            mediaSubdivide = "無法判斷";        
        }
        else{
            media = "其它";                       //媒體
            mediaSubdivide = "無法判斷";        //媒體細分
        }
    
        var mediaInfo=new Array(media,mediaSubdivide);
        return mediaInfo;
    },
    //終端
    Terminal: function(){
        if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))){
            terminal = "移動端";
            return terminal;
        }else{
            terminal = "PC端";
            return terminal;
        }
    },
    
    //註冊渠道。通過判斷當前頁面url獲取
    RegisterChannel: function(){
        if(localUrl.indexOf(args.mySite)!= -1 ){
            registerChannel = args.mySite+"網站註冊";
            return registerChannel;
        }else if(localUrl.indexOf(args.subSite)!= -1){
            registerChannel = args.subSite+"網站註冊";
            return registerChannel;
        }else{
            registerChannel = "未知";
            return registerChannel;
        }
    }
};

//通用設定cookie方法
function setCookie(name, value, days) {
    var cookieTime = days * 24 * 60 * 60 * 1000;
    var d = new Date();
    d.setTime(d.getTime() + cookieTime);
    var cookieExpires = "; expires=" + d.toGMTString();
    document.cookie = name + "=" + escape(value) + cookieExpires + "; path=/";
}
//通用讀取cookie方法
function getCookie(name) {
    var strCookie = document.cookie;
    var arrCookie = strCookie.split("; ");
    for (var i = 0; i < arrCookie.length; i++) {
        var arr = arrCookie[i].split("=");
        if (arr[0] == name)
            return unescape(arr[1]);
    }
    return "";
}

//排除站內跳轉的情況,以免window.name被錯誤覆蓋
if(document.referrer.indexOf(args.mySite)<=0){
    window.name=document.referrer.substring(0,40)+"||"+window.location.href;
}

//使用案例
dataSource = getCookie('regCompanyName');
if(!dataSource || ""== dataSource || typeof(dataSource)== "undefined") {
    //當cookie無法使用時,優先使用window.name
    dataSource = (window.name) ? window.name : "";
}

var judgeMedia = userChannel.Media(dataSource);
var judgeTerminal = userChannel.Terminal();
var judgeRegisterChannel = userChannel.RegisterChannel();

console.log(judgeMedia+' ',judgeTerminal+' ',judgeRegisterChannel+' ');