yuanoook / Mathworks Cloud

// ==UserScript==
// @namespace     https://openuserjs.org/users/yuanoook
// @name          Mathworks Cloud
// @description   Open mathworks examples and use mathworks cloud for free.
// @copyright     2020, yuanoook (https://openuserjs.org/users/yuanoook)
// @license       MIT
// @version       0.0.4
// @include       https://www.mathworks.com/help/matlab/ref/disp.html*
// @grant none
// ==/UserScript==

// ==OpenUserJS==
// @author yuanoook
// ==/OpenUserJS==

const wait = (seconds = 1) => new Promise(
    resolve => setTimeout(resolve, seconds * 1000)
)

const sisyphus = async function (func) {
    while (true) {
        await wait()
        try {
            return func()
        } catch (error) {
            console.error(error)
        }
    }
}

const sisyphusSelect = selector => sisyphus(() => {
    console.log(`Searching ${selector} ...`)
    const result = $(selector)

    if (!result.length) throw new Error(`Didn't get ${selector}`)

    return result
})

;const openExample = async function () {
    ;(await sisyphusSelect('#expand_panel_body_budkcnd-4 button.analyticsOpenWith')).click()
    ;(await sisyphusSelect('[style*="top: 2%;"]'))
        .attr('id', 'mathworks_cloud_ide')
        .append(`<style>
            #mathworks_cloud_ide {
                top: 0% !important;
                left: 0% !important;
                width: 100% !important;
                height: 100% !important;
            }
        </style>`)
}

;(async function () {
    await openExample()
})()