Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to Open in Browser if File "Untitled" #50

Open
flakrat opened this issue Jun 30, 2017 · 6 comments
Open

Fails to Open in Browser if File "Untitled" #50

flakrat opened this issue Jun 30, 2017 · 6 comments

Comments

@flakrat
Copy link

flakrat commented Jun 30, 2017

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.

@sompylasar
Copy link

sompylasar commented Jan 2, 2018

Same for .json file (not reliably reproduced). Also #52 #54 #56

@sompylasar
Copy link

The issue isn't reliably reproducible for me.
The error is here, filePath happens to not be a string in some circumstances:

open: (filePath) ->
opn(filePath).catch (error) ->

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
Copy link

sompylasar commented Jan 2, 2018

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):

@subscriptions.add atom.commands.add '.tree-view .file',
'open-in-browser:open-tree-view', @openTreeView.bind(this)

openTreeView: ({target}) ->
@open target.dataset.path

@sompylasar
Copy link

For opening from the text editor (not from tree view) it relies on editor to return a file path:

@subscriptions.add atom.commands.add 'atom-text-editor',
'open-in-browser:open', @openEditor.bind(this)

openEditor: ->
@open @getFilePath()

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

@jluee
Copy link

jluee commented May 20, 2020

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

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

@AnnaFBD
Copy link

AnnaFBD commented Sep 12, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants