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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*globals chrome */
// inject printliminator from popup & control from there.
var commands = {
remove : function() {
chrome.tabs.executeScript( null, {
code: 'thePrintliminator.removeGraphics();'
});
},
print : function() {
document.querySelector( 'li.print' ).classList.add( '/* @echo busy */' );
// ready state is delayed when a file on the page is not found
chrome.tabs.executeScript( null, {
code : 'document.readyState === "complete";'
}, function( result ) {
if ( result && result[ 0 ] === true ) {
window.close();
chrome.tabs.executeScript( null, {
code : 'thePrintliminator.print();'
});
} else {
// keep checking ready state for 20 seconds
// if still not ready, abort, but still call print function
var loopy = function( i ) {
setTimeout(function () {
chrome.tabs.executeScript( null, {
code : 'document.readyState === "complete";'
}, function( result ) {
if ( result && result[ 0 ] === true || i === 1 ) {
i = 0;
window.close();
chrome.tabs.executeScript( null, {
code : 'thePrintliminator.print();'
});
}
if ( --i > 0 ) {
loopy(i);
}
});
}, 1000);
};
// repeat 20 times (20 seconds), then just close the popup
loopy( 20 );
}
});
},
stylize : function() {
chrome.tabs.executeScript( null, {
code : 'thePrintliminator.stylize();'
});
},
keyboard : function() {
var indx,
table = document.querySelector( '#/* @echo keyboard */' ),
mode = table.style.display === 'none';
table.style.display = mode ? '' : 'none';
this.innerHTML = ( mode ? 'Hide' : 'View' ) + ' Keyboard Commands';
},
undo : function() {
chrome.tabs.executeScript( null, {
code : 'thePrintliminator.undo();'
});
},
setLanguage : function(){
// update all text content
commands.getMsg( document.querySelectorAll( '[i18n-text]' ), true );
commands.getMsg( document.querySelectorAll( '[i18n-title]' ), false );
},
getMsg : function( elms, isText ) {
var indx, msgKey, message,
len = elms.length;
for ( indx = 0; indx < len; indx++ ) {
msgKey = elms[ indx ].getAttribute( 'i18n-' + ( isText ? 'text' : 'title' ) );
message = chrome.i18n.getMessage( msgKey );
if ( isText ) {
elms[ indx ].innerHTML = message;
} else {
elms[ indx ].title = message;
}
}
}
};
chrome.windows.getCurrent( function( win ) {
chrome.tabs.query({
windowId : win.id,
active : true
}, function( tabArray ) {
// don't try to open a popup on chrome settings pages
if ( tabArray && /^chrome/.test( tabArray[ 0 ].url || '' ) ) {
return false;
}
// inject css & js only on initial click
chrome.tabs.executeScript( null, {
code : 'document.querySelector( "body" ).classList.contains( "/* @echo enabled */" );',
matchAboutBlank : true
}, function( result ) {
if ( result && !result[ 0 ] ) {
chrome.tabs.insertCSS( null, {
file : 'printliminator.css',
matchAboutBlank : true
});
chrome.tabs.executeScript( null, {
file: 'printliminator.js',
matchAboutBlank : true
}, function() {
chrome.tabs.executeScript( null, {
code : 'thePrintliminator.init();'
});
});
}
// update Language
commands.setLanguage();
});
// Remove graphics
var el = document.querySelector( './* @echo noGraphics */' );
el.removeEventListener( 'click', commands.remove );
el.addEventListener( 'click', commands.remove );
// Print
el = document.querySelector( './* @echo print */' );
el.removeEventListener( 'click', commands.print );
el.addEventListener( 'click', commands.print );
// Add print stylesheet
el = document.querySelector( './* @echo stylize */' );
el.removeEventListener( 'click', commands.stylize );
el.addEventListener( 'click', commands.stylize );
// Undo
el = document.querySelector( './* @echo undo */' );
el.removeEventListener( 'click', commands.undo );
el.addEventListener( 'click', commands.undo );
// keyboard
el = document.querySelector( './* @echo toggle */' );
el.removeEventListener( 'click', commands.keyboard );
el.addEventListener( 'click', commands.keyboard );
});
});
|