open-in-browser icon indicating copy to clipboard operation
open-in-browser copied to clipboard

Fails to Open in Browser if File "Untitled"

Open flakrat opened this issue 7 years ago • 6 comments

open-in-browser will produce the following error when attempting to open a document that hasn't yet been saved to disk.

Error: Expected a target
Error: Expected a `target`
    at module.exports (/Users/mhanby/.atom/packages/open-in-browser/node_modules/opn/index.js:9:25)
    at Object.open (/Users/mhanby/.atom/packages/open-in-browser/lib/open-in-browser.coffee:24:5)
    at Object.openEditor (/Users/mhanby/.atom/packages/open-in-browser/lib/open-in-browser.coffee:18:6)
    at CommandRegistry.module.exports.CommandRegistry.handleCommandEvent (/Applications/Atom.app/Contents/Resources/app/src/command-registry.js:265:35)
    at /Applications/Atom.app/Contents/Resources/app/src/command-registry.js:3:65
    at KeymapManager.module.exports.KeymapManager.dispatchCommandEvent (/Applications/Atom.app/Contents/Resources/app/node_modules/atom-keymap/lib/keymap-manager.js:610:22)
    at KeymapManager.module.exports.KeymapManager.handleKeyboardEvent (/Applications/Atom.app/Contents/Resources/app/node_modules/atom-keymap/lib/keymap-manager.js:401:28)
    at WindowEventHandler.module.exports.WindowEventHandler.handleDocumentKeyEvent (/Applications/Atom.app/Contents/Resources/app/src/window-event-handler.js:100:42)
    at HTMLDocument.<anonymous> (/Applications/Atom.app/Contents/Resources/app/src/window-event-handler.js:3:65)

Steps to reproduce:

  • Create a new document: cmd-n
  • Add some HTML code <html><p>blah blah blah</p></html>
  • Launch open-in-browser (right click method or keybind)

Work around is to save the file first. Under most circumstances this probably isn't an issue since you are probably already working with a file that's been saved. In this case, however, I'm just working with a temporary window in order to copy and paste the working code into an online HTML editor.

I wonder if there's a way to trap when a file is in a modified state, if yes then do something else like prompt to save or pop up a message that changes must be saved before they will be visible in the browser.

The above would also be helpful for files that do exist on disk but when recent changes haven't been saved prior to launching the browser.

flakrat avatar Jun 30 '17 17:06 flakrat

Same for .json file (not reliably reproduced). Also https://github.com/magbicaleman/open-in-browser/issues/52 https://github.com/magbicaleman/open-in-browser/issues/54 https://github.com/magbicaleman/open-in-browser/issues/56

sompylasar avatar Jan 02 '18 21:01 sompylasar

The issue isn't reliably reproducible for me. The error is here, filePath happens to not be a string in some circumstances: https://github.com/magbicaleman/open-in-browser/blob/0ab814320f8f27ab22624a590b84d96dd55e14bf/lib/open-in-browser.coffee#L23-L24

That causes the promise from opn to be rejected with this Error: Expected a `target` error: https://github.com/sindresorhus/opn/blob/b9f7e569f5a3d5d520047df3e9f10a42d52fd9d4/index.js#L6-L8

module.exports = (target, opts) => {
	if (typeof target !== 'string') {
		return Promise.reject(new Error('Expected a `target`'));

sompylasar avatar Jan 02 '18 21:01 sompylasar

This plugin relies on event.target.dataset.path to be a non-empty string in the .tree-view .file event handler (when opened from the tree view context menu): https://github.com/magbicaleman/open-in-browser/blob/0ab814320f8f27ab22624a590b84d96dd55e14bf/lib/open-in-browser.coffee#L12-L13

https://github.com/magbicaleman/open-in-browser/blob/0ab814320f8f27ab22624a590b84d96dd55e14bf/lib/open-in-browser.coffee#L20-L21

sompylasar avatar Jan 02 '18 21:01 sompylasar

For opening from the text editor (not from tree view) it relies on editor to return a file path: https://github.com/magbicaleman/open-in-browser/blob/0ab814320f8f27ab22624a590b84d96dd55e14bf/lib/open-in-browser.coffee#L10-L11

https://github.com/magbicaleman/open-in-browser/blob/0ab814320f8f27ab22624a590b84d96dd55e14bf/lib/open-in-browser.coffee#L17-L18

but for "Untitled" files which haven't yet been saved – this issue – there's no path returned.

sompylasar avatar Jan 02 '18 21:01 sompylasar

Screen Shot 2020-05-20 at 8 21 32 AM

Can someone help me understand how to fix this issue I'm having?

jluee avatar May 20 '20 12:05 jluee

I know this error is old... but I found this page when I was trying to figure out what was going on. Eventually, I noticed that this error is only caused when you right click outside of the file name itself - so it's failing to pick up the filename you want to open in a browser. If you make sure you click on the filename itself and then on 'open in browser' it seems to solve the problem.

AnnaFBD avatar Sep 12 '20 15:09 AnnaFBD