Skip to content

Commit

Permalink
improve: add callback as an optional argument to handle eval_js result
Browse files Browse the repository at this point in the history
  • Loading branch information
shiqimei authored and glacambre committed Nov 9, 2020
1 parent 7f8c020 commit 36a47bd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion autoload/firenvim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/nvimproc/Neovim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 36a47bd

Please sign in to comment.