Code Monkey home page Code Monkey logo

meom-3rd-party-scripts's People

Contributors

giraffewoods avatar samikeijonen avatar

Watchers

 avatar  avatar  avatar  avatar

meom-3rd-party-scripts's Issues

Delay JS

There is interesting approach to delay 3. party JS.

I'm not suggesting to delay our JS by default but document how it can be done.

Change src attributes to delay attributes.

We already have filters for this:

/**
 * Delay JS by changing `src=` to `delay=` in GTM and Cookiebot.
 *
 * @link https://blog.speedvitals.com/delay-javascript/
 *
 * @return void
 */
function delay_js( $code, $id ) {
    return str_replace( 'src=', 'delay=', $code );
}
add_filter( 'mtps_gtm_head_code', 'delay_js', 10, 2 );
add_filter( 'mtps_cookiebot_code', 'delay_js', 10, 2 );

Add custom JS

I'm testing a little bit modified version of the JS:

/**
 * Change `delay` attributes back to `src` attributes.
 *
 * @return void
 */
function print_delay_js() {
    ?>
    <script>
        var delayLoaded = false;
        var autoLoadDuration = 5;
        var eventList = ["keydown", "mousemove", "wheel", "touchmove", "touchstart", "touchend"];

        var autoLoadTimeout = setTimeout(runScripts, autoLoadDuration * 1000);

        eventList.forEach(function(event) {
            window.addEventListener(event, triggerScripts, { passive: true })
        });

        function triggerScripts() {
            runScripts();
            clearTimeout(autoLoadTimeout);
            eventList.forEach(function(event) {
                window.removeEventListener(event, triggerScripts, { passive: true });
            });
        }

        function runScripts() {
            if (!delayLoaded) {
                document.querySelectorAll("script[data-cookieconsent]").forEach(function(scriptTag) {
                    <?php // Create new script tag and replace "delay=" with "src=". ?>
                    var newNode = document.createElement('script');
                    newNode.setAttribute("data-cookieconsent", "ignore");
                    var newContent = scriptTag.textContent.replace("delay=", "src=");
                    newNode.textContent = newContent;
                    scriptTag.parentNode.replaceChild(newNode, scriptTag);
                });

                document.querySelectorAll("script[delay]").forEach(function(scriptTag) {
                    <?php // Replace "delay=" with "src=" attribute. ?>
                    scriptTag.setAttribute("src", scriptTag.getAttribute("delay"));
                });
            }

            delayLoaded = true;
        }
    </script>
    <?php
}
add_action( 'wp_head', 'print_delay_js' );

Suggestion for new data attribute

In the above code I'm using document.querySelectorAll("script[data-cookieconsent]") to detect GTM code. It doesn't feel like bullet proof because we might not even have Cookiebot on the page.

We should have our own data attribute, for example data-meom-delay and hook into that:

document.querySelectorAll("script[data-meom-delay]").

Possibility to add CookieDeclaration to a page

The CookieDeclaration script looks like this:

<script id="CookieDeclaration" src="https://consent.cookiebot.com/xxxx/cd.js" type="text/javascript" async></script>

It would be nice to have a shortcode or something so we don't have to add the script to the editor.
@SinttiSalminen you had some insight how to add the language version to the script?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.