swf2js auto-apply script

Once this script finds the embedded FLASH .swf file, use swf2js to convert it to HTML5 and display it in the browser. The success or failure of the conversion depends on the swf2js library.

Ekde 2021/01/08. Vidu La ĝisdata versio.

// ==UserScript==
// @name                swf2js auto-apply script
// @name:ja             swf2js 自動適用スクリプト
// @name:zh-CN          swf2js 自动应用脚本
// @name:zh-TW          swf2js 自動應用腳本
// @description         Once this script finds the embedded FLASH .swf file, use swf2js to convert it to HTML5 and display it in the browser. The success or failure of the conversion depends on the swf2js library.
// @description:ja      FLASH の .swf ファイルの埋め込みを見つけたら、 swf2js を使ってそれを HTML5 に変換し、ブラウザ上で表示させます。 変換の成否は、 swf2js ライブラリーに依存します。
// @description:zh-CN   一旦此脚本找到了嵌入式FLASH .swf文件,请使用swf2js将其转换为HTML5并在浏览器中显示。转换的成功或失败取决于swf2js库。
// @description:zh-TW   一旦此腳本找到了嵌入式FLASH .swf文件,請使用swf2js將其轉換為HTML5並在瀏覽器中顯示。轉換的成功或失敗取決於swf2js庫。
// @version             1
// @include             *
// @grant               none
// @namespace https://greasyfork.org/users/724967
// ==/UserScript==
(() => {
    'use strict';
    let counter = 0;
    // reverse this to process nested elements from the inside.
    const loaderCodes = [...document.querySelectorAll('embed[type="application/x-shockwave-flash"][src$=".swf"], object[type="application/x-shockwave-flash"][src$=".swf"]')].reverse().map((elm) => {
        const { width, height, src } = elm;
        const widthStyle = !width ? '' : `width:${width}${(/^\d+$/.test(width) ? 'px' : '')}`;
        const heightStyle = !height ? '' : `height:${height}${(/^\d+$/.test(height) ? 'px' : '')}`;
        const containerId = `swf2js_container_id_${++counter}`;
        const container = document.createElement('div');
        container.id = containerId;
        container.style = `display:inline-block;${widthStyle};${heightStyle};`;
        elm.parentNode.insertBefore(container, elm);
        elm.parentNode.removeChild(elm);
        return `((containerId) => { if (document.getElementById(containerId)) { window.swf2js.load('${src.replace(/(?=\\|')/g, '\\')}', { tagId: containerId }); }; })('${containerId}');`;
    });
    if (loaderCodes.length !== 0) {
        // execute swf2js on browser scope
        const swf2jsLoadedCallback = () => {
            const s = document.createElement('script');
            s.type = 'text/javascript';
            s.innerHTML = loaderCodes.reverse().join('');
            document.body.appendChild(s);
        }
        if (!window.swf2js) {
            const swf2jsScriptElm = document.createElement('script');
            swf2jsScriptElm.type = 'text/javascript';
            swf2jsScriptElm.src = 'https://gitcdn.xyz/cdn/swf2js/swf2js/d9ec7ebe4a886d5337417a595caba49effdea5dd/swf2js.js';
            swf2jsScriptElm.onload = swf2jsLoadedCallback;
            document.body.appendChild(swf2jsScriptElm);
        } else {
            window.setTimeout(swf2jsLoadedCallback, 1);
        }
    }
})();