Skip to content

adding inline stylesheet and javascript? #10

@dark-kitt

Description

@dark-kitt

Hey,

first of all, great work! Everything works like expected.
What do you think about adding this lines to your plugin?
To inline the Stylesheets and the JavaScript files.

first

const urlModule = require('url');
const URL = urlModule.URL;

underneath
const delayPageLoad,
const puppeteerOptions etc...

const stylesheetContents = {};
const javascriptContents = {};

the first part of your chain() function behind (page) => page.on('pageerror', ...

`stash stylesheet and javascript`,
(page) => page.on('response', async resp => {
        const responseUrl = resp.url();
        const sameOrigin = new URL(responseUrl).origin === new URL(source).origin;
        const isStylesheet = resp.request().resourceType() === 'stylesheet';
        const isJavaScript = resp.request().resourceType() === 'script';
        if (sameOrigin && isStylesheet) {
            stylesheetContents[responseUrl] = await resp.text();
        }
        if (sameOrigin && isJavaScript) {
            javascriptContents[responseUrl] = await resp.text();
        }
    }),

the second part in .then(([browser, page]) => chain(...
behind () => page.waitFor(delayPageLoad),...

`inline stylesheet`,
() => page.$$eval('link[rel="stylesheet"]', (links, content) => {
        links.forEach(link => {
            const ctx = content[link.href];
            if (ctx) {
                const style = document.createElement('style');
                style.textContent = ctx;
                link.replaceWith(style);
            }
        });
    }, stylesheetContents),

`inline javascript`,
() => page.$$eval('script[type="text/javascript"]', (links, content) => {
        links.forEach(link => {
            const ctx = content[link.src];
            if (ctx) {
                const script = document.createElement('script');
                script.textContent = ctx;
                link.replaceWith(script);
            }
        });
    }, javascriptContents),

Let me know about your thoughts :)

cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions