Notification.js

Notification.js by Andrew Dodson

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/34465/225717/Notificationjs.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Notification.js
// @version      1.1.1
// @description  Notification.js by Andrew Dodson
// @author       Andrew Dodson
// @grant        none
// ==/UserScript==


/**
 * Notification JS
 * Shims up the Notification API
 *
 * @author Andrew Dodson
 * @website http://adodson.com/notification.js/
 */

//
// Does the browser support the the Notification API?
// .. and does it have a permission property?
//

(function(window, document){

	var PERMISSION_GRANTED = 'granted',
		PERMISSION_DENIED = 'denied',
		PERMISSION_UNKNOWN = 'unknown';
	
	var a = [], iv, i=0;

	//
	// Swap the document.title with the notification
	//
	function swaptitle(title){
	
		if(a.length===0){
			a = [document.title];
		}

		a.push(title);

		if(!iv){
			iv = setInterval(function(){

				// has document.title changed externally?
				if(a.indexOf(document.title) === -1 ){
					// update the default title
					a[0] = document.title;
				}
				
				document.title = a[++i%a.length];
			}, 1000);
		}
	}

	function swapTitleCancel(){

		// dont do any more if we haven't got anything open
		if(a.length===0){
			return;
		}
		
		// if an IE overlay is present, kill it
		if("external" in window && "msSiteModeClearIconOverlay" in window.external ){
			window.external.msSiteModeClearIconOverlay();
		}

		clearInterval(iv);
		
		iv = false;
		document.title = a[0];
		a = [];
	}
	
	//
	// Add aevent handlers
	function addEvent(el,name,func){
		if(name.match(" ")){
			var a = name.split(' ');
			for(var i=0;i<a.length;i++){
				addEvent( el, a[i], func);
			}
		}
		if(el.addEventListener){
			el.removeEventListener(name, func, false);
			el.addEventListener(name, func, false);
		}
		else {
			el.detachEvent('on'+name, func);
			el.attachEvent('on'+name, func);
		}
	}


	function check_permission(){
		// Check whether the current desktop supports notifications and if they are authorised,
		// PERMISSION_GRANTED (yes they are supported and permission is granted),
		// PERMISSION_DENIED (yes they are supported, permission has not been granted),
		// -1 (Notifications are not supported)
		
		// IE9
		if(("external" in window) && ("msIsSiteMode" in window.external)){
			return window.external.msIsSiteMode()? PERMISSION_GRANTED : PERMISSION_UNKNOWN;
		}
		else if("webkitNotifications" in window){
			return window.webkitNotifications.checkPermission() === 0 ? PERMISSION_GRANTED : PERMISSION_DENIED;
		}
		else if("mozNotification" in window.navigator){
			return PERMISSION_GRANTED;
		}
		else {
			return PERMISSION_UNKNOWN;
		}
	}

	function update_permission(){
		// Define the current state
		window.Notification.permission = check_permission();
		return window.Notification.permission;
	}


	if(!Object(window.Notification).permission){

		//
		// Bind event handlers to the body
		addEvent(window, "focus scroll click", swapTitleCancel);

		// Assign it.
		window.Notification = function(message, options){

			// ensure this is an instance
			if(!(this instanceof window.Notification)){
				return new window.Notification(message,options);
			}

			var n, self = this;

			//
			options = options || {};

			this.body = options.body || '';
			this.icon = options.icon || '';
			this.lang = options.lang || '';
			this.tag = options.tag || '';
			this.close = function(){

				// remove swapTitle
				swapTitleCancel();

				// Close
				if(Object(n).close){
					n.close();
				}

				self.onclose();
			};
			this.onclick = function(){};
			this.onclose = function(){};

			//
			// Swap document.title
			//
			swaptitle(message);

			//
			// Create Desktop Notifications
			//
			if(("external" in window) && ("msIsSiteMode" in window.external)){
				if(window.external.msIsSiteMode()){
					window.external.msSiteModeActivate();
					if(this.icon){
						window.external.msSiteModeSetIconOverlay(this.icon, message);
					}
				}
			}
			else if("webkitNotifications" in window){
				if(window.webkitNotifications.checkPermission() === 0){
					n = window.webkitNotifications.createNotification(this.icon, message, this.body );
					n.show();
					n.onclick = function(){

						// Fire any user bound events to the onclick function
						self.onclick();

						// redirect the user back to the page
						window.focus();
						setTimeout( function(){ n.cancel(); }, 1000);
					};
				}
			}
			else if( "mozNotification" in window.navigator ){
				var m = window.navigator.mozNotification.createNotification( message, this.body, this.icon );
				m.show();
			}
		};

		window.Notification.requestPermission = function(cb){
			// Setup
			// triggers the authentication to create a notification
			cb = cb || function(){};
	
			// IE9
			if(("external" in window) && ("msIsSiteMode" in window.external)){
				try{
					if( !window.external.msIsSiteMode() ){
						window.external.msAddSiteMode();
						cb( PERMISSION_UNKNOWN );
					}
				}
				catch(e){}
				cb( update_permission() );
			}
			else if("webkitNotifications" in window){
				window.webkitNotifications.requestPermission(function(){
					cb( update_permission() );
				});
			}
			else {
				cb( update_permission() );
			}
		};

		// Get the current permission
		update_permission();
	}
})(window, document);