~glacambre/firenvim

36a47bdf983c6b9745e4a9f6fc73c4041712177b — lolimay 2 months ago 7f8c020
improve: add callback as an optional argument to handle eval_js result
3 files changed, 8 insertions(+), 4 deletions(-)

M autoload/firenvim.vim
M src/nvimproc/Neovim.ts
M src/utils/utils.ts
M autoload/firenvim.vim => autoload/firenvim.vim +2 -1
@@ 17,7 17,8 @@ function! firenvim#get_chan() abort
endfunction

function! firenvim#eval_js(js) abort
        call rpcnotify(firenvim#get_chan(), 'firenvim_eval_js', a:js)
        let callback_name = get(a:, 1, '')
        call rpcnotify(firenvim#get_chan(), 'firenvim_eval_js', a:js, callback_name)
endfunction

" Asks the browser extension to release focus from the frame and focus the

M src/nvimproc/Neovim.ts => src/nvimproc/Neovim.ts +4 -1
@@ 65,7 65,10 @@ export async function neovim(
                    .then(() => { if (hasFocus && !document.hasFocus()) { window.focus(); } });
                break;
            case "firenvim_eval_js":
                page.evalInPage(args[0]);
                const result = await page.evalInPage(args[0]);
                if (args[1]) {
                    request("nvim_call_function", [args[1], [JSON.stringify(result)]]);
                }
                break;
            case "firenvim_focus_page":
                page.focusPage();

M src/utils/utils.ts => src/utils/utils.ts +2 -2
@@ 25,10 25,10 @@ export function executeInPage(code: string): Promise<any> {
    return new Promise((resolve, reject) => {
        const script = document.createElement("script");
        const eventId = (new URL(browser.runtime.getURL(""))).hostname + Math.random();
        script.innerHTML = `((evId) => {
        script.innerHTML = `(async (evId) => {
            try {
                let result;
                result = ${code};
                result = await ${code};
                window.dispatchEvent(new CustomEvent(evId, {
                    detail: {
                        success: true,