fs.writeFile('./dist/preview.html', dom.window.document.querySelector("html").innerHTML, (err) => { if (err) throw err; console.log(chalk.whiteBright.bgBlack.bold(" Created [HTML] Preview File !! ")); });
function extract(data) { data.forEach(function (item) { let extension = path.extname(item); if (extension === ".svg") { let fileName = item.replace(/-([a-z])/g, function (i) { return i[1].toUpperCase(); }); let fileData = fs.readFileSync("./icons/" + item).toString(); let dom = new JSDOM(fileData); let svgDefs = dom.window.document.querySelector("defs"); if(svgDefs != undefined){ dom.window.document.querySelector("svg").removeChild(svgDefs); } let svgStyle= dom.window.document.querySelector("style"); if(svgStyle != undefined){ dom.window.document.querySelector("svg").removeChild(svgStyle); } let svgTitle = dom.window.document.querySelector("title"); if(svgTitle != undefined){ dom.window.document.querySelector("svg").removeChild(svgTitle); } let fileDataCore = dom.window.document.querySelector("svg").innerHTML.replace(/\n/g, ""); svgObject[fileName.slice(0, -4)] = fileDataCore; } }); }
context('without any meta tags', function() { before(function() { global.document = new JSDOM(HTML_WITHOUT_META).window.document; options = parser.parse(); }); it('expected to return empty object', function() { expect(options).to.be.empty; }); });
ipc.on('inviteUrl', function (event, steamid) { request('https://steamcommunity.com/profiles/' + steamid, function (err, resp, body) { if (!err && resp.statusCode === 200) { let dom = new JSDOM(body, { runScripts: "outside-only" }) let joinBtn = dom.window.document.querySelector('.profile_in_game_joingame a'); event.sender.send('inviteUrl', (joinBtn !== null) ? joinBtn.href : '') } }) })
downloadPage('https://medium.com/topic/javascript', (content) => { const document = new JSDOM(content).window.document; Array.from(document.querySelectorAll('a')) .map((el) => el.text) .filter(s => s.trim() != '') .forEach((s) => console.log(s)); });
get document(): JSDOM { return this.window.document; }
// if DOMParser is not defined then assign jsdom DOMParser(); (function(global) { var jsdom = require('jsdom'), dom = new jsdom.JSDOM(''); global.DOMParser = dom.window.DOMParser; })(global);
function parseHtmlToDOM(html) { const htmlDOM = new JSDOM(html); return jQuery(htmlDOM.window); }
topics.forEach(topic => { downloadPage(`https://medium.com/topic/${topic}`, (content) => { const articles = findArticles(new JSDOM(content).window.document); Object.values(articles) .forEach(printArticle); }); });
context('without options', function() { before(function() { const resourceLoader = new jsdom.ResourceLoader({userAgent: USER_AGENT_IPHONE_IOS9}); global.window = new JSDOM('<html></html>', { resource: resourceLoader }).window; global.document = window.document; global.getComputedStyle = global.document.defaultView.getComputedStyle; smartbanner = new SmartBanner(); }); it('expected to throw error', function() { expect(() => smartbanner.publish()).to.throw('No options detected. Please consult documentation.'); }); });
const parseBookmarkContent = (raw) => { try { const doc = new JSDOM(raw); const reader = new Readability(doc.window.document); const parsed = reader.parse(); return parsed } catch (error) { throw new Error("Parsing bookmark failed") } }
downloadPage('https://medium.com/topic/javascript', (content) => { const document = new JSDOM(content).window.document; Array.from(document.querySelectorAll('a')) .map((el) => url.parse(el.href).pathname) .forEach((u) => console.log(u)); });
context('when on iPad', function() { before(function() { const resourceLoader = new jsdom.ResourceLoader({ userAgent: USER_AGENT_IPAD }); global.window = new JSDOM(HTML_IOS, { resources: resourceLoader }).window; global.document = window.document; global.getComputedStyle = window.getComputedStyle; global.Event = window.Event; smartbanner = new SmartBanner(); }); it('expected to add iOS template to body', function() { smartbanner.publish(); let html = document.querySelector('.js_smartbanner').outerHTML; expect(html).to.eql(IOS_BODY); }); });
context('without smartbanner meta tags', function() { before(function() { global.document = new JSDOM(HTML_WITHOUT_SMARTBANNER_META).window.document; options = parser.parse(); }); it('expected to return empty object', function() { expect(options).to.be.empty; }); });