Simple, cross-browser print event listeners (before and after) with no dependencies
// ES2015
import PrintScout from 'printscout';
// CommonJS
const PrintScout = require('printscout').default;
// script
const PrintScout = window.PrintScout;
// create a new scout
const scout = new PrintScout();
// add listeners in a traditional way
const beforePrintHandler = () => {
console.log('I fire before print render');
};
scout.on('beforeprint', beforePrintHandler);
// or you can do it dynamically, the handler you passed is returned when added,
// either with the shorthand methods or the standard ones
const afterPrintHandler = scout.after(() => {
console.log('I fire after print render');
});
// and remove them as you would any other listener
scout.off('beforeprint', beforePrintHandler);
// or with the convenience function attached to the handler returned from adding it
afterPrintHandler.off();
scout.on(eventName: string, handler: function): function
Adds the handler
to the list of handlers that will fire when the specific eventName
is called. The valid eventName
values are beforeprint
, before
(shorthand for beforeprint
), afterprint
, and after
(shorthand for afterprint
).
const scout = new PrintScout();
const handler = scout.on('afterprint', event => console.log(event));
Additionally, the handler
passed receives an additional off
method attached to it, which will remove the listener when called directly.
handler.off(); // will no longer log the event
scout.off([eventName: string[, handler: function]]): void
Removes the handler
from the list of handlers that will fire when the specific eventName
is dispatched. If no handler
is passed, then all handlers for a given eventName
are removed. If no eventName
is passed, then all handlers are removed. The valid eventName
values are the same as for on.
const scout = new PrintScout();
const handler = scout.on('beforeprint', console.log('about to print...'));
...
scout.off('beforeprint', handler);
scout.after(handler: function): function
Shorthand method for on with the eventName
set to afterprint
. Like on
, returns the handler
passed.
const scout = new PrintScout();
const handler = scout.after(event => console.log(event));
scout.before(handler: function): function
Shorthand method for on with the eventName
set to beforeprint
. Like on
, returns the handler
passed.
const scout = new PrintScout();
const handler = scout.before(event => console.log(event));
- Chrome (all versions)
- Firefox (all versions)
- Edge (all versions)
- Opera 15+
- IE 9+
- Safari 6+
- iOS 8+
- Android 4+
Standard stuff, clone the repo and npm install
dependencies. The npm scripts available:
build
=> run webpack to build developmentdist
file with NODE_ENV=developmentbuild:minified
=> run webpack to build productiondist
file with NODE_ENV=productiondev
=> run webpack dev server to run example app / playgrounddist
=> runsbuild
andbuild-minified
lint
=> run ESLint against all files in thesrc
folderprepublish
=> runscompile-for-publish
prepublish:compile
=> runlint
,test:coverage
,transpile:es
,transpile:lib
,dist
test
=> run AVA test functions withNODE_ENV=test
test:coverage
=> runtest
but withnyc
for coverage checkertest:watch
=> runtest
, but with persistent watchertranspile:lib
=> run babel against all files insrc
to create files inlib
transpile:es
=> run babel against all files insrc
to create files ines
, preserving ES2015 modules (forpkg.module
)