Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Print HTML elements or pages in modern browsers. :printer:
Printd opens your Browser Print Dialog to print HTML elements inside a blank document or pages by URL.
1KB
gzipped with no dependencies).npm run sample
yarn add printd
npm install printd --save
UMD file is also available on unpkg:
<script src="https://unpkg.com/printd/printd.umd.min.js"></script>
You can use the library via window.printd
.
import { Printd } from 'printd'
const cssText = `
h1 {
color: black;
font-family: sans-serif;
}
`
const d = new Printd()
d.print( document.getElementById('myelement'), [ cssText ] )
window.document.body
head
elements array.body
elements array.Example:
// custom base element example
const base = document.createElement('base')
base.setAttribute('href', 'https://your-cdn.dev')
// define options to use
const options = {
parent: document.getElementById('myparent'),
headElements: [ base ]
}
const d = new Printd(options)
Function to print an HTMLElement
.
d.print (element, styles, scripts, callback)
Print parameters:
HTMLElement
object to print.document.head
)document.body
)HTMLIFrameElement
reference. It already contains contentWindow
and contentDocument
references.HTMLElement
copy (cloned node) reference of current element to print.const d = new Printd()
d.print( document.getElementById('h1'), [`h1 { font-family: serif; }`] )
Callback option is suitable when you plan to print elements or pages with assets (images, fonts, etc) but you need to wait for them. Your callback will be triggered only when your assets are loaded.
const d = new Printd()
// Tip: texts & urls are supported
const styles = [
'https://your-cdn.dev/style.css',
`.code { font-family: monospace; }`
]
const scripts = [
'https://your-cdn.dev/script.js',
`(() => console.log('Hello from IFrame!'))()`
]
// Get an HTMLElement reference
const el = document.getElementById('mycode-block')
// Trigger the print dialog on demand
const printCallback = ({ launchPrint }) => launchPrint()
d.print(el, styles, scripts, printCallback)
Function to print an URL.
PrintURL parameters:
const d = new Printd()
d.printURL('http://127.0.0.1/', ({ launchPrint }) => {
console.log('Content loaded!')
// fire printing!
launchPrint()
})
Printd provides convenient print event methods on top of the Browser beforeprint
and afterprint
events.
However note that they only work when printing custom HTML elements via the print()
method.
const d = new Printd()
d.onBeforePrint((event) => console.log(event, "called before printing!"))
d.onAfterPrint((event) => console.log(event, "called after printing!"))
d.print(document.getElementById('myelement'))
Gets the current HTMLIFrameElement
reference.
Examples:
const d = new Printd()
const iframe = d.getIFrame()
// a) Subscribe to IFrame load event
iframe.addEventListener('load', () => console.log('iframe loaded!'))
// b) Subscribe to Window `beforeprint` or `afterprint` events
const { contentWindow } = iframe
contentWindow.addEventListener('beforeprint', () => console.log('before print!'))
contentWindow.addEventListener('afterprint', () => console.log('after print!'))
References:
For Webkit-based browsers, it can create an equivalent result using window.matchMedia('print')
.
if (contentWindow.matchMedia) {
const mediaQueryList = contentWindow.matchMedia('print')
mediaQueryList.addListener((mql) => {
if (mql.matches) {
console.log('before print!')
} else {
console.log('after print!')
}
})
}
References:
Since Printd uses an underlying iframe it's highly recommended to ensure that only your content will be displayed. As a fallback you could remove the hidden iframe after some printing.
Here some interesting security advices that you want to take at look:
Feel free to send some Pull request or issue.
MIT license
© 2017-present Jose Quintana
FAQs
Print HTML elements in modern browsers.
We found that printd demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.