Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/CSS-Tricks/The-Printliminator.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'dist/chrome/popup.js')
-rw-r--r--dist/chrome/popup.js145
1 files changed, 145 insertions, 0 deletions
diff --git a/dist/chrome/popup.js b/dist/chrome/popup.js
new file mode 100644
index 0000000..75efc84
--- /dev/null
+++ b/dist/chrome/popup.js
@@ -0,0 +1,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( '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( '#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( "_printliminator_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( '.no_graphics' );
+ el.removeEventListener( 'click', commands.remove );
+ el.addEventListener( 'click', commands.remove );
+
+ // Print
+ el = document.querySelector( '.print' );
+ el.removeEventListener( 'click', commands.print );
+ el.addEventListener( 'click', commands.print );
+
+ // Add print stylesheet
+ el = document.querySelector( '.stylize' );
+ el.removeEventListener( 'click', commands.stylize );
+ el.addEventListener( 'click', commands.stylize );
+
+ // Undo
+ el = document.querySelector( '.undo' );
+ el.removeEventListener( 'click', commands.undo );
+ el.addEventListener( 'click', commands.undo );
+
+ // keyboard
+ el = document.querySelector( '.toggle' );
+ el.removeEventListener( 'click', commands.keyboard );
+ el.addEventListener( 'click', commands.keyboard );
+
+ });
+});