-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathquick-exit.js
106 lines (81 loc) · 2.41 KB
/
quick-exit.js
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(function( window ) {
'use strict';
var history = window.history;
var exitElement;
var accesskeyLinks = {};
function quickExit( event, target ) {
target = target || event.target;
var href = target.href;
window.document.body.style.opacity = 0;
window.document.title = 'New Tab';
// clears current frame only
while ( document.firstChild ) {
document.removeChild( document.firstChild );
}
if ( history && history.replaceState ) {
history.replaceState( null, 'Home', '/' );
}
// href = this.href || target.href;
while ( ! href && target !== exitElement && target.parentNode ) {
target = target.parentNode;
href = target.href;
}
href = href || target.getElementsByTagName( 'a' )[ 0 ].href || 'about:blank';
/* jshint -W040 */
window.location = href;
/* jshint +W040 */
event.preventDefault();
return false;
}
function quickExitKeyboard( event ) {
if ( accesskeyLinks[ event.keyCode ]) {
if ( /^(INPUT|TEXTAREA|SELECT|BUTTON)$/i.test( event.target.tagName )) {
return;
}
quickExit.call( event.target, event, accesskeyLinks[ event.keyCode ] );
}
}
function setupEventHandlers( addEventListener ) {
var i;
var accesskey, link;
function getAccesskey( element ) {
var KEYS = {
esc: 27
};
var accesskey = element.getAttribute( 'data-accesskey' ) || element.getAttribute( 'accesskey' );
if ( /^[a-zA-Z]$/.test( accesskey )) {
return accesskey.toUpperCase().charCodeAt( 0 );
} else if ( accesskey ) {
return KEYS[ accesskey.toLowerCase() ];
}
return null;
}
link = exitElement.getElementsByTagName( 'a' );
for ( i = 0; i < link.length; i++ ) {
accesskey = getAccesskey( link[ i ]);
if ( accesskey ) {
accesskeyLinks[ accesskey ] = link[ i ];
}
}
exitElement[ addEventListener ]( 'click', quickExit, true );
if ( Object.keys( accesskeyLinks ).length ) {
document[ addEventListener ]( 'keydown', quickExitKeyboard, true );
}
}
function init() {
var addEventListener = document.addEventListener ? 'addEventListener' : 'attachEvent';
if ( exitElement ) {
return; // aleady setup
}
exitElement = document.getElementById( 'quick-exit' );
if ( exitElement ) {
// setup now
setupEventHandlers( addEventListener );
} else {
// setup on ready/load
document[ addEventListener ]( 'DOMContentLoaded', init );
document[ addEventListener ]( 'load', init );
}
}
init();
}( window.top ));