js實現擷取網站搜尋來原始碼

來源:互聯網
上載者:User
本文主要和大家分享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 mediaSubpide = "";//註冊渠道。(直接賦值)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 = "網址開啟或站內跳轉";  //媒體            mediaSubpide =  "";       //媒體細分            // 無法擷取referrer的來源的情況有:            //1、個別平台外鏈使用noreferrer;            //2、https跳轉http;            //3、網頁重新導向...            //可在外鏈中植入可判定的參數如www.xxx.com&tag=zhihu            if(channelInfo.indexOf("zhihu")>=0){                media = "知乎";                mediaSubpide = "";            }        }else if(channelInfo.indexOf("baidu.com") != -1){            media = "百度";                       //媒體            if(channelInfo.indexOf('utm_medium=cpc') != -1){                mediaSubpide = "sem";         //媒體細分            }else if(channelInfo.indexOf('utm_medium=cpc') == -1){                mediaSubpide = "seo";         //媒體細分            }else{                mediaSubpide = "其它";      //媒體細分            }        }else if(channelInfo.indexOf('haosou.com') != -1 || channelInfo.indexOf('so.com') != -1){            media = "好搜";                       //媒體            mediaSubpide = "無法判斷";        //媒體細分        }else if(channelInfo.indexOf('sogou.com') != -1){            media = "搜狗";                       //媒體            mediaSubpide = "無法判斷";        //媒體細分        }else if(channelInfo.indexOf('sm.cn') != -1){            media = "神馬";                       //媒體            mediaSubpide = "無法判斷";        //媒體細分        }else if(channelInfo.indexOf('bing.com') != -1){            media = "必應";                       //媒體            mediaSubpide = "無法判斷";        //媒體細分        }else if(channelInfo.indexOf('google.com') != -1){            media = "google";                       //媒體            mediaSubpide = "無法判斷";        //媒體細分        }else if(channelInfo.indexOf('douban.com') != -1){            media = "豆瓣";                       //媒體            mediaSubpide = "無法判斷";        //媒體細分        }else if(channelInfo.indexOf("zhihu.com") != -1){            media = "知乎";            mediaSubpide = "無法判斷";                }           else if(channelInfo.indexOf("toutiao") != -1){            media = "今日頭條";            mediaSubpide = "無法判斷";                }        else{            media = "其它";                       //媒體            mediaSubpide = "無法判斷";        //媒體細分        }            var mediaInfo=new Array(media,mediaSubpide);        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+' ');

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.