Economic Calendar Custom Filter Button

Adds a button to filter economic calendar for Japan and USA with 2-3 star importance

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Economic Calendar Custom Filter Button
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds a button to filter economic calendar for Japan and USA with 2-3 star importance
// @author       Grok
// @match        https://vn.investing.com/economic-calendar/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to wait for an element to appear
    function waitForElement(selector, callback, maxAttempts = 20, interval = 500) {
        let attempts = 0;
        const intervalId = setInterval(() => {
            const element = document.querySelector(selector);
            if (element) {
                clearInterval(intervalId);
                callback(element);
            } else if (attempts >= maxAttempts) {
                clearInterval(intervalId);
                console.error('Element not found:', selector);
            }
            attempts++;
        }, interval);
    }

    // Function to apply the custom filter
    function applyCustomFilter() {
        // Open the filter wrapper if not already open
        const filtersWrapper = document.getElementById('filtersWrapper');
        if (filtersWrapper && filtersWrapper.style.display !== 'block') {
            window.changeFiltersVisibility();
        }

        // Wait for the country and importance filter boxes
        waitForElement('#calendarFilterBox_country', () => {
            // Uncheck all countries
            const countryCheckboxes = document.querySelectorAll('input[name="country[]"]');
            countryCheckboxes.forEach(checkbox => {
                checkbox.checked = false;
            });

            // Check USA (country5) and Japan (country35)
            const usaCheckbox = document.getElementById('country5');
            const japanCheckbox = document.getElementById('country35');
            if (usaCheckbox) usaCheckbox.checked = true;
            if (japanCheckbox) japanCheckbox.checked = true;

            // Uncheck all importance levels
            const importanceCheckboxes = document.querySelectorAll('input[name="importance[]"]');
            importanceCheckboxes.forEach(checkbox => {
                checkbox.checked = false;
            });

            // Check 2-star (importance2) and 3-star (importance3)
            const importance2Checkbox = document.getElementById('importance2');
            const importance3Checkbox = document.getElementById('importance3');
            if (importance2Checkbox) importance2Checkbox.checked = true;
            if (importance3Checkbox) importance3Checkbox.checked = true;

            // Click the Apply button
            const applyButton = document.getElementById('ecSubmitButton');
            if (applyButton) {
                applyButton.click();
            } else {
                console.error('Apply button not found');
            }
        });
    }

    // Function to create and insert the custom button
    function createCustomButton() {
        waitForElement('#filterStateAnchor', (filterButton) => {
            // Check if button already exists to avoid duplicates
            if (document.getElementById('customFilterButton')) return;

            // Create the new button
            const customButton = document.createElement('a');
            customButton.id = 'customFilterButton';
            customButton.href = 'javascript:void(0);';
            customButton.className = 'newBtn filter LightGray float_lang_base_2';
            customButton.innerHTML = 'Lọc Nhật Bản & Hoa Kỳ (2-3*)';
            customButton.style.marginLeft = '10px';

            // Add click event to apply the filter
            customButton.addEventListener('click', applyCustomFilter);

            // Insert the button after the existing filter button
            filterButton.parentNode.insertBefore(customButton, filterButton.nextSibling);
        });
    }

    // Run when the page loads
    window.addEventListener('load', () => {
        createCustomButton();
    });

    // Re-create button on AJAX updates
    const observer = new MutationObserver(() => {
        createCustomButton();
    });

    waitForElement('#economicCalendarFilters', (filterSection) => {
        observer.observe(filterSection, { childList: true, subtree: true });
    });
})();