forked from foundation/inky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinky-browser.js
43 lines (37 loc) · 1.02 KB
/
inky-browser.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
var cheerio = require('cheerio');
var Inky = require('../lib/inky');
var inky;
window.setupInky = function(opts, cb) {
opts = opts || {};
opts.cheerio = Inky.mergeCheerioOpts(opts.cheerio);
if (typeof inky === 'undefined') {
inky = new Inky(opts);
}
// This transform function takes in an element and calls a callback.
function transform(html, callback) {
var convertedHtml = inky.releaseTheKraken(html, opts.cheerio);
callback(null, convertedHtml);
}
cb(transform);
}
if(typeof(window) !== 'undefined') {
window.runInky = function(opts, elem) {
if(typeof(elem) === 'undefined') {
elem = opts;
opts = {};
}
window.setupInky(opts, function(transform) {
transform(elem.outerHTML, function(err, html) {
if(err === null) {
elem.outerHTML = html;
} else {
console.log(err);
}
});
});
}
var elems = document.body.getElementsByTagName('container')
for(var i = 0; i < elems.length; i++) {
window.runInky(elems[i]);
}
}