!function(t,n,e){var a=!0,r={isVisible:function(t){var n=t.getBoundingClientRect(),e=n.width||n.right||0-n.left||0,a=n.height||n.bottom||0-n.top||0;return e>0&&a>0},isInSight:function(e){const a=e.parentNode,r=this.getDataset(a).pid,i=this.getDataset(e).src;if(-1!=n.cookie.indexOf("c_adb=")&&-1===i.indexOf("adBottom"))return!1;const c=t.innerWidth,s=["476","479","480"];if(c<1720&&-1!=s.indexOf(r))return!1;if("indexSuperise_"+r in localStorage)return!1;var o=e.getBoundingClientRect();if(this.isVisible(e)){var d=t.innerHeight;return o.top<=d+100}return!1},loadImg:function(t){var n=this;if(!t.src){var e=n.getDataset(t).src;t.src=e}},getDataset:function(t){if(t.dataset)return t.dataset;var n=t.attributes,e={};name,matchStr;for(var a=0;a=500&&(t.apply(a,r),n=e)}},checkImgs:function(){for(var t=this,n=e(".pre-img-lasy"),r=0;r.svgfont {display: inline-block;width: 1em;height: 1em;fill: currentColor;vertical-align: -0.1em;font-size:16px;}") } catch(e) { console && console.log(e) } } ready(appendSvg) })(window);(function(window){ /** * @brief Notifier * * @param type 1. 一直提示 2,更新提示内容 3, 控制显示上限。超过个数删除最早的 4,超时更新 * @param param 本参数在type为3,4时才有效, type为3表示可以最多显示通知的个数 4, 表示多少秒后删除了 * * @return */ function Notifier() {}; window.Notifier = Notifier; type = 1; queue = []; t = 5; c = 3; _notifier = undefined ; if(undefined !== window.webkitNotifications) { _notifier = window.webkitNotifications; }else if (undefined !== window.Notification) { _notifier = window.Notification; } else { // console.log('error not found notification!') } window.Notifier.ModelAll = function() { type = 1; } window.Notifier.ModelUpdate = function() { type = 2; } window.Notifier.ModelCount = function(ct) { if(ct !== undefined) c = ct; type = 3; } window.Notifier.ModelTimeout = function(timeout) { if(timeout !== undefined) t = timeout; type = 4; } window.Notifier.HasSupport = function() { if(undefined === _notifier) { return false; } return true; } window.Notifier.GetPermission = function() { return _notifier.checkPermission(); } window.Notifier.IsGetPermission = function() { return (_notifier === 0); } window.Notifier.Disable = function() { return (_notifier.checkPermission === 2); } window.Notifier.RequestPermission = function(cb) { _notifier.requestPermission(function() { if(cb) {cb(_notifier.checkPermission() == 0)} }); } //type = 1;关闭上一个 window.Notifier.Close = function(type) { if(type = 1) { tmp = queue.pop(); } else { tmp = queue.shift(); } _closeItem(tmp); } window.Notifier.ClosePre = function () { tmp = queue.pop(); _closeItem(tmp); } window.Notifier.CloseLast = function () { tmp = queue.shift(); _closeItem(tmp); } window.Notifier.CloseAll = function() { while(queue.length > 0) { var tmp = queue.shift(); _closeItem(tmp); } } window.Notifier.Notify = function(icon, title, body) { if (this.IsGetPermission() == 0) { var popup = _createNotificationAndShow(icon, title, body); if(undefined == popup) { return false; } switch(type) { case 2: if(queue.length > 0) { tmp = queue.pop(); _closeItem(tmp); } break; case 3: while(queue.length >= c) { tmp = queue.shift(); _closeItem(tmp); } break; case 4: setTimeout(function(){_closeItem(popup);}, t*1000); break; } var q = queue; popup.onclose = function(){ var cur = q.indexOf(popup); if(cur >= 0) { q.splice(cur, 1); } }; popup.onclick = function(){}; queue.push(popup); return true; } else { RequestPermission(); } return false; } function _createNotificationAndShow(icon, title, body) { if(undefined != window.webkitNotifications && _notifier.name === window.webkitNotifications.name) { var n = _notifier.createNotification(icon, title, body); n.show(); return n; }else if (undefined !== window.Notification && _notifier.name === window.Notification.name) { return new _notifier(title, {icon:icon, body: body}); } else { // console.log('error not found notification!') // alert(title +"\n\n"+body); return undefined; } } function _closeItem(n) { if(undefined == n) { return } if(n.cancel) { n.cancel(); } else { n.close(); } } })(window); 'use strict'; (function(){ const applicationServerPublicKey = 'BCYaMwiS92AJlv9Eg2YXSFwuI3ppbydkz31gOI5NS7YtOp05n7qUHEyb_iijzQcjgWqrsGSj2K18F21G9DYL4-U'; let isSubscribed = false; let swRegistration = null; var linkUrl = '' function getBrowserInfo() { var agent = navigator.userAgent.toLowerCase(); var regStr_ie = /msie [\d.]+;/gi; var regStr_ff = /firefox\/[\d.]+/gi var regStr_chrome = /chrome\/[\d.]+/gi; var regStr_saf = /safari\/[\d.]+/gi; var isIE = agent.indexOf("compatible") > -1 && agent.indexOf("msie" > -1); //判断是否IE<11浏览器 var isEdge = agent.indexOf("edge") > -1 && !isIE; //判断是否IE的Edge浏览器 var isIE11 = agent.indexOf('trident') > -1 && agent.indexOf("rv:11.0") > -1; if (isIE) { var reIE = new RegExp("msie (\\d+\\.\\d+);"); reIE.test(agent); var fIEVersion = parseFloat(RegExp["$1"]); if (fIEVersion == 7) { return "IE/7"; } else if (fIEVersion == 8) { return "IE/8"; } else if (fIEVersion == 9) { return "IE/9"; } else if (fIEVersion == 10) { return "IE/10"; } } //isIE end if (isIE11) { return "IE/11"; } //firefox if (agent.indexOf("firefox") > 0) { return agent.match(regStr_ff); } //Safari if (agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0) { return agent.match(regStr_saf); } //Chrome if (agent.indexOf("chrome") > 0) { return agent.match(regStr_chrome); } } function urlB64ToUint8Array(base64String) { const padding = '='.repeat((4 - base64String.length % 4) % 4); const base64 = (base64String + padding) .replace(/\-/g, '+') .replace(/_/g, '/'); const rawData = window.atob(base64); const outputArray = new Uint8Array(rawData.length); for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); } return outputArray; } if ('serviceWorker' in navigator && 'PushManager' in window) { var _hmt = window._hmt || []; var hasSub = getCookie('c_hasSub') // 判断域名的操作 如果域名是blog 和download var flagBox1 = window.location.host.indexOf('blog')>=0 ?'true':'false' var flagBox2 = window.location.host.indexOf('download')>=0 ?'true':'false' var fs = window.RequestFileSystem || window.webkitRequestFileSystem; if (!fs) { console.log("check failed"); handleRight() } else { fs(window.TEMPORARY,100,function(){ //console.log('非隐身模式'); handleRight() },function(){ //console.log('隐身模式'); return false }); } navigator.serviceWorker.addEventListener('message', function (event) { console.log('收到sw的消息event====',event) if (event.data === 'showBox') { _hmt.push(['_trackEvent', '推送消息弹框', '消息', '', '展示推送弹框']) } if (event.data === 'openLink') { _hmt.push(['_trackEvent', '打开消息链接', '消息', '', '打开消息链接']) } }); } else { //火狐浏览器隐身模式下 serviceWorker属性都不支持 safari不支持 可以全站推送 走此逻辑 var flagBox1 = window.location.host.indexOf('blog')>=0 ?'true':'false' var flagBox2 = window.location.host.indexOf('download')>=0 ?'true':'false' if(flagBox1 === 'true' || flagBox2 === 'true' ){ if ('serviceWorker' in navigator) { // 注册service worker navigator.serviceWorker.register('/sw.js').then(function (registration) { return askPermission(); }).then(function () { console.log('safari授权成功了====') var subscription = '-' handleAjax(subscription) // workOrderSync() }) navigator.serviceWorker.addEventListener('message', function (event) { console.log('收到sw的消息event11====',event) if (event.data === 'showBox') { _hmt.push(['_trackEvent', '推送消息弹框', '消息', '', '展示推送弹框']) } if (event.data === 'openLink') { _hmt.push(['_trackEvent', '打开消息链接', '消息', '', '打开消息链接']) } }); } } } // function askPermission() { return new Promise(function (resolve, reject) { var permissionResult = Notification.requestPermission(function (result) { resolve(result); }); if (permissionResult) { permissionResult.then(resolve, reject); } }).then(function (permissionResult) { if (permissionResult !== 'granted') { throw new Error('We weren\'t granted permission.'); } }); } // 操作权限 function handleRight() { if((hasSub!=='true'&&flagBox1 === 'true')||(hasSub!=='true'&&flagBox2 === 'true')){ // 没有授权的操作 // if((hasSub!=='true')||(hasSub!=='true')){ var Notification = window.Notification || window.mozNotification || window.webkitNotification; navigator.serviceWorker.register('/sw.js') .then(function(swReg) { //console.log('注册成功了') swRegistration = swReg; workOrderSync(swRegistration) // sw注册后调用检查用户是否订阅通知的函数 initialiseUI(); Notification.requestPermission(function(status){ if(status==='denied'&& getCookie('firstDie')!=='1'){ // 隐身模式下不弹框 默认直接拒绝授权 // && getCookie('firstDie')!=='1' //用户拒绝授权 再次选择 弹出授权框拒绝才能再次记录 _hmt.push(['_trackEvent', '弹出框', '拒绝授权', '', 'rejectRightBox']) // console.log('执行rejectRightBox') setCookie('firstDie','1',1) } else if(status==='granted'){ // 用户同意授权 // 授权过的就不再进入这个里面了 //执行了授权 只能清缓存 弹出授权框允许才能再次记录 //debugger subscribeUser() } }) }) .catch(function(error) { //console.log('隐身模式下注册不成功') var browser = getBrowserInfo()[0] || ''; //console.log('browser',browser) _hmt.push(['_trackEvent', 'serviceWorker', '报错', '', 'serviceWorker报错']) _hmt.push(['_trackEvent', '浏览器版本精确版本', browser, '', '真实不能注册的浏览器版本']) // $.get('https://statistic.csdn.net/notification/error?brower='+browser) }); // 可以手动获取权限 但是否去选择弹出是不知道的 // navigator.serviceWorker.ready.then(function(swRegistration) { // console.log('workOrderSync执行了') // return swRegistration.sync.register("workOrderSync"); // }); }else{ navigator.serviceWorker.ready.then(function(swRegistration) { // //console.log('刷新走了else====进入了workSync') // return swRegistration.sync.register("workOrderSync"); // var options = { // body: 'heihei', // icon: 'img/logo.png', // badge: 'img/badge.png' // }; // var title = 'haha666'; // swRegistration.showNotification(title, options); workOrderSync(swRegistration) }); // 注册过的和不是blog 或者 download 域名下如果接口数据有返回 的可以直接拿数据展示 } } function workOrderSync(swRegistration) { var opts = { method:"POST", //请求方法 body:{}, //请求体 headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, credentials: "include" } // setInterval(function () { // // https://test-notification.csdn.net/notifiction/fe/getbyusername // fetchData(opts,swRegistration) // }, 20000) // fetchData(opts,swRegistration) } function fetchData(opts,swRegistration){ // 测试环境地址 https://test-notify.csdn.net/notifiction/fe/getbyusername fetch("https://msg.csdn.net/v1/chrome/notification/view",opts).then(res=> { return res.json() }).then(res=> { //console.log('res1===',res); var jsonData = res.data //console.log('jsonData===',jsonData); var title = jsonData.title; linkUrl = jsonData.url || '' var username= jsonData.username || '' var options = { body: jsonData.message, icon: 'img/logo.png', badge: 'img/badge.png', data:linkUrl // actions: [{ // action: '', // title: '' // }] }; var browser = getBrowserInfo()[0] || ''; console.log('浏览器信息输出browser',browser) if(JSON.stringify(jsonData)!=='{}'&&res.status){ if(browser&&browser.indexOf('safari/')>-1){ var notification = new Notification(title, options); console.log('走了safari逻辑里面====',notification) notification.addEventListener('click', function (e) { window.location.href = linkUrl }); } else { swRegistration.showNotification(title, options) } _hmt.push(['_trackEvent', '展示推送消息弹框', '消息', username ]) } }).catch(err=> { //console.log(err); }) } // 检查用户当前有没有订阅 function initialiseUI() { swRegistration.pushManager.getSubscription() .then(function(subscription) { isSubscribed = !(subscription === null); if (isSubscribed) { } else { } updateBtn(); }); } // cookie function setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toUTCString(); document.cookie = cname + "=" + cvalue + "; " + expires+";domain=csdn.net;path=/" // console.log(d) } //获取cookie function getCookie(cname) { var name = cname + "="; 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); if(c.indexOf(name) != -1) return c.substring(name.length, c.length); } return ""; } function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=getCookie(name); if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toUTCString()+';domain=csdn.net;path=/'; } //启用我们的按钮,以及更改用户是否订阅的文本 function updateBtn() { // 只执行一次 如何监听 if (Notification.permission === 'denied') { return; } if(Notification.permission === 'default'){ _hmt.push(['_trackEvent', '弹出框', '展示授权弹框', '', 'showRightBox']) //console.log('执行showRightBox') delCookie('firstDie') delCookie('c_hasSub') } if(Notification.permission === 'granted'){ } if (isSubscribed) { } else { } } function subscribeUser() { const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey); // 传递公钥给sw服务器 //debugger swRegistration.pushManager.subscribe({ userVisibleOnly: true, // 默认允许订阅后发送通知 applicationServerKey: applicationServerKey }) .then(function(subscription) { // setCookie('firstEnter','1',1) handleAjax(subscription) _hmt.push(['_trackEvent', '弹出框', '允许授权', '', 'agreeRightBox']) // console.log('执行agreeRightBox2') isSubscribed = true //updateBtn() }) .catch(function(err) { // updateBtn() _hmt.push(['_trackEvent', '请求接口', '失败', '', '接口报错']) }); } function handleAjax(subscription){ var xhr; if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码 xhr = new XMLHttpRequest(); } else { // IE6, IE5 浏览器执行代码 xhr = new ActiveXObject("Microsoft.XMLHTTP"); } //设置请求的类型及url // application/x-www-form-urlencoded xhr.open('post', 'https://msg.csdn.net/chrome/v1/browse_info/save_browse_info' ); //发送请求 // https://msg.csdn.net/chrome/v1/browse_info/save_browse_info // 变更请求地址之前: https://gw.csdn.net/cui-service/v1/browse_info/save_browse_info //post请求一定要添加请求头才行不然会报错 open后send前 xhr.setRequestHeader("Content-type","application/json"); var jsonData = '' if(typeof(subscription) === 'string'){ jsonData ={'subscription':subscription} } else { jsonData ={'subscription':JSON.stringify(subscription)} } xhr.withCredentials = true; xhr.send(JSON.stringify(jsonData)); xhr.onreadystatechange = function () { // 这步为判断服务器是否正确响应 if (xhr.readyState == 4 && xhr.status == 200) { //console.log(xhr.responseText); setCookie('c_hasSub',true,1) } }; } }(window))