Tạo quảng cáo bật lên buộc nhấp chuột trên Blogger

Code force click popup quảng cáo trên Blogger
Vui lòng chờ 0 giây...
Cuộn xuống hoặc nhấp vào đây để chuyển đến liên kết
Chúc mừng! Liên kết được tạo

Trong bài viết này mình sẽ chia sẻ cho các bạn đoạn code JavaScript Force Click Popup quảng cáo cho Blogger. Nó có thể giúp các bạn tăng thêm thu nhập từ việc bắt người dùng click vào quảng cáo của Google được hiển thị trong popup để có thể tiếp tục xem trang hay đọc bài viết.

Tạo quảng cáo bật lên buộc nhấp chuột trên Blogger
Tạo quảng cáo bật lên buộc nhấp chuột trên Blogger

Code Force Click Popup quảng cáo này sẽ lấy random id quảng cáo của bạn để tránh bị giới hạn quảng cáo, các bạn cũng có thể cài đặt cookie để quảng cáo có thể hiển thị lại sau 1 thời gian mà người dùng đã click cái này để tránh bị spam click cũng như giảm khả năng bị Google Adsense ban.

Mình cũng không khuyến khích các bạn dùng code này vì có thể nó làm khó chịu cho người dùng và một ngày nào đó có thể bị Google Adsense ban.

Cách thực hiện

Cách thực hiện các bạn không cần biết nhiều về những kiến thức HTML, CSS hoặc JS vì mình đã code sẵn cho các bạn. Các bạn cần làm theo chính xác các bước hướng dẫn của mình là được.

Bước 1: Trước hết Đăng nhập vào Trang tổng quan Blogger.

Bước 2: Trên bảng điều khiển Blogger, nhấp vào Chủ đề.

Bước 3: Nhấp vào biểu tượng mũi tên xuống bên cạnh nút 'TÙY CHỈNH'.

Bước 4: Nhấp vào Chỉnh sửa HTML, bạn sẽ được chuyển đến trang chỉnh sửa.

Bước 5: Bây giờ tìm kiếm mã </body> và dán các mã JS sau ngay trên vào nó.

<script>/*<![CDATA[*/
  (function (config) {
    const cookie = {
        get: name => {
            const match = document.cookie.match(new RegExp(`(?:^|; )${name.replace(/([.$?*|{}()[\]\\/+^])/g, "\\$1")}=([^;]*)`));
            return match ? decodeURIComponent(match[1]) : undefined;
        },
        set: (name, value, options = {}) => {
            options = { path: "/", ...options };
            if (options.expires instanceof Date) {
                options.expires = options.expires.toUTCString();
            }
            let updatedCookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
            for (const [key, val] of Object.entries(options)) {
                updatedCookie += `; ${key}`;
                if (val !== true) {
                    updatedCookie += `=${val}`;
                }
            }
            document.cookie = updatedCookie;
        },
        remove: name => {
            cookie.set(name, "", { 'max-age': -1 });
        }
    };

    function adExists() {
        return document.getElementById('nldblog-ad') !== null;
    }

    function handleCallback(callback, params) {
        if (typeof callback === 'function') {
            callback(params);
        }
    }

    function createAdContainer(callback) {
        const adContainer = document.createElement('div');
        adContainer.id = 'nldblog-ad';
        adContainer.classList.add('fcAd');
        adContainer.innerHTML = `
            <div class='fcFx'>
                <button class='fcBt hidden'>
                    <svg viewBox='0 0 24 24'><path d='M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z'/></svg>
                    <span>${config.message}</span>
                </button>
                <div class='fcCn'>
                    <ins class='adsbygoogle' data-ad-client='ca-pub-${config.publisherID}' data-ad-format='auto' data-ad-slot='${config.adUnits[Math.floor(Math.random() * config.adUnits.length)]}' data-full-width-responsive='false' style='display:block'></ins>
                </div>
            </div>`;
        document.body.appendChild(adContainer);

        const adStatus = { shown: false, clicked: null, error: null };
        let checkAdInterval = setInterval(() => {
            const iframe = adContainer.querySelector('.fcCn ins.adsbygoogle iframe');
            const adStatusAttr = adContainer.querySelector('.fcCn ins.adsbygoogle').getAttribute('data-ad-status');
            if (adStatusAttr === 'unfilled') {
                clearInterval(checkAdInterval);
                handleCallback(callback, adStatus);
            } else if (adStatusAttr === 'filled' && iframe !== null) {
                clearInterval(checkAdInterval);
                adContainer.querySelector('.fcBt').classList.remove('hidden');
                window.addEventListener('blur', () => {
                    if (document.activeElement === iframe) {
                        adStatus.shown = true;
                        adStatus.clicked = true;
                        adContainer.remove();
                        cookie.set(config.cookie, 1, { secure: true, 'max-age': config.maxage });
                        handleCallback(callback, adStatus);
                    }
                });
            }
        }, 100);

        setTimeout(() => {
            const adIns = adContainer.querySelector('.fcCn ins.adsbygoogle');
            if (adIns.getAttribute('data-adsbygoogle-status') !== 'done' || (adIns.getAttribute('data-adsbygoogle-status') === 'done' && adIns.getAttribute('data-ad-status') !== 'filled')) {
                handleCallback(callback, adStatus);
                adContainer.remove();
            }
        }, 5000);

        try {
            (adsbygoogle = window.adsbygoogle || []).push({});
        } catch (error) {
            adStatus.error = error;
            handleCallback(callback, adStatus);
        }
    }

    function initializeAds(callback) {
        if (cookie.get(config.cookie) !== undefined) {
            handleCallback(callback, { shown: false, clicked: null });
            return;
        }
        if (adExists()) {
            handleCallback(callback, { shown: false, clicked: null });
            return;
        }
        if (document.querySelectorAll(`script[src*='client=ca-pub-${config.publisherID}']`).length === 0) {
            const script = document.createElement('script');
            script.crossOrigin = 'anonymous';
            script.async = true;
            script.src = `https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-${config.publisherID}`;
            script.onload = () => createAdContainer(callback);
            script.onerror = () => handleCallback(callback, { shown: false, clicked: null });
            document.head.appendChild(script);
        } else {
            createAdContainer(callback);
        }
    }

    window.popupAds = {
        init: initializeAds,
        show: initializeAds
    };

    const style = document.createElement('style');
    style.textContent = `
        .fcBt { outline: none; border: none; padding: 0; margin: 0; position: absolute; top: 30px; right: 30px; width: 30px; height: 30px; display: flex; align-items:center; justify-content:center; border-radius:50%; background:#fff; box-shadow:0 0 20px rgba(0,0,0,.07); cursor: pointer; }
        .fcBt svg { width: 18px; height: 18px; fill: #686868 }
        .fcBt:hover span { opacity: 1; visibility: visible; top: calc(30px + 10px); }
        .fcBt span { position:absolute; top:30px; right:-15px; background:#fff; max-width:300px; width:180px; border-radius:10px; padding:10px; font-size:13px; line-height:1.1rem; box-shadow:0 0 20px rgba(0,0,0,.07); opacity:0; visibility:hidden; transition:all .2s ease; pointer-events: none; }
        .fcBt span::before { content:''; position: absolute; width:10px; height:10px; background:#fff; top:-4px; right:25px; transform:rotate(45deg); border-radius:2px; }
        .fcAd { position: fixed; top: -30%; right: -30%; bottom: -30%; left: -30%; background: rgba(0,0,0,.3); z-index: 999; }
        .fcFx { position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 999; padding: 20px; display: flex; align-items: center; justify-content: center; }
        .fcCn { background: #fff; padding: 10px; width: 100%; border-radius: 8px; max-width: 768px; }`;
    document.head.appendChild(style);

    popupAds.init();
})({
    publisherID: 'google-adsense-id',
    adUnits: ['id-adsense-slot-1', 'id-adsense-slot-2', 'id-adsense-slot-3'],
    cookie: "__CLICK",
    message: "Click on Ads to support developer.",
    maxage: 24 * 60 * 60
}); /*]]>*/</script>

Các bạn nhớ thay thế các vị trí được đánh dấu trong đoạn code như:

Đánh dấu Thông tin
publisherID: Là id google adsense của bạn không có ca-pub-.
adUnits: Là id của các quảng cao khi bạn tạo, bạn muốn thêm bao nhiêu id cũng được.
cookie: Cookie và sẽ hiện thị lại popup
message: Là thông báo khi người dùng bấm nút close.
maxage: Là thời gian hết hạn cookie và sẽ hiện thị lại popup sau khi hết hạn cookie được tính bằng đơn vị giây.

Bước 6: Lưu các thay đổi bằng cách nhấp vào biểu tượng này .

Trên đây là hướng dẫn tạo quảng cáo bật lên buộc nhấp chuột trên Blogger.
Nếu có bất cứ thắc mắc nào hãy comment ngay phía dưới cho mình biết nhé!

Copyright © www.toishare.com

Tôi là Nhật Nguyễn, một kỹ sư xây dựng. Nhưng thích tìm hiểu về mã nguồn lập trình web ☏.