Detect what opened current note?

This might sound kind of strange, but I’m wondering if we through a dataviewjs query (or other javascript incantations) are able to get which link brought us to the current note?

One scenario to examplify what I’m looking for:

  • Let’s say we’ve got two notes, NoteA with a link [[EndNote#first]], and NoteB with a link [[EndNote#second]]. I’m currently in NoteA, and I hit the link
  • Now EndNote opens up, and I’m wondering can I within this note write some javascript code, revealing either of the following:
    • Primary goal: That this time I came from NoteA,
    • Secondary goal: or that the link which opened EndNote this time was in fact the [[EndNote#first]] link?

Is either of these goals achievable somehow? Is there something hidden in the app or dv (or some other variables), which reveals which link triggered the opening of the current note?

( Tertiary(?) goal: Is there some event I could hook on to, before/when the open occurs, where some of this information could be available? )

The use case this could solve for me

If something like this is possible, I’m contemplating on removing loads of my non-existent notes to people, with a common page, and then create aliases all over the places to “headings” within my common page, and then detect where these came from.

Say this page was called “Unknowns”, I could have a link to [[Unknowns#ElonMusk]] or [[Unknowns|Elon Musk]], and hopefully I could know which variant triggered the Unknowns page the last time, and focus on that.

Hmm… Would it be sufficient to actually just use the [[Unknowns#Elon Musk]] link? Maybe, but I would really like to know if this possible using a query, somehow.

If your goal is just to link to a particular heading, then yeah, the heading link is all you need. I’m not sure if the link-origin stuff you’re asking about is meant to help reach the right destination or if that’s something you want in addition to that.

I’m asking for detection on what triggered the opening of the note programmatically.

I know how a heading link works, but I’m asking whether I can see traces of what happened in the javascript context. And that is also why I asked in Developers and API, and not in Help. So why did you move it?

I know. I wasn’t sure why after reading your use case section. I don’t see any explicit mention of what you want to use that info for. It sounded like you might want it to determine where in the linked-to page to focus, and in the last paragraph it sounds like you might be thinking you don’t actually need it because heading links do that (but you’re still curious now that the question has come up). Alternately, it seemed possible to me that what you meant was that you wanted to display that info at the destination — but that seemed less supported by the use-case description.

I moved the post because it sounded like you were trying to accomplish something in your vault, not developing a plugin or interacting with the API. Sorry for misunderstanding.

@holroy you can use:


There might be an even better way, but this works for me. It’s just getting the path of the previously opened file, which would presumably be the one that you were on before clicking into the current note.

Interesting fubction, but I do explicitly want to know the link triggering the open to current file.

Sorry, if I was unclear, but I do want to use out to focus in the current page. Potentially I’m thinking about auto folding other parts, or similar ways to narrow down the focus in the current note.

Seen from my (poorly described) use case I’d want to “hide” the other unknowns. This way I don’t have to have a bunch of non vital person notes in my vault, but can limit that to a catch-all for those that I don’t know that much about.

1 Like

I’m not sure whether I misunderstand you or you misunderstand me, but what I suggested fulfills both your primary and secondary goals.

The only time it would not is if NoteA had multiple links to EndNote, and even then it would still fulfill your primary goal.

I see that the primary goal I described will be fulfilled with getLastOpenFiles(), but will it describe whether it was [[EndNote#first]] or [[EndNote#second]] or similar which was called?

I reckon, without testing as I’m working right now, that’ll it only list NoteA, but hopefully I’m wrong, and it would list the link I’m after with the subpath and/or alias and all.

If there’s only one link to the EndNote file in NoteA, then you can just read the note content (by Javascript) and get the name of the link.

If there are multiple links inside NoteA to the same EndNote file then naturally that won’t work, as I mention in last comment.

Seems sensible, yes. What about cursor position in either the last file, or the current file. Do you think I could use that to track what my “landing point” was?

If for the previous file, I do of course need to read out the link from that position, but I can live that, if all else fails.

Lastly, I’ve seen the link to the obsidian api, and the plugin developers guide, are those two the only/main information we’ve git pertaining to the inner workings of Obsidian?

I mainly open the console and type app. and follow my nose. That’s how I found the getLastOpenFiles() function.

I don’t have a better answer than that sorry, there aren’t really any useful docs that I’ve found (other than this one which is fairly incomplete)

1 Like

Gotcha — I understand now.

FYI following a link to a folded section unfolds it, so you could get a more laborious version of what you want by using the “Fold all” command when you’re done with the Unknowns page. Obviously that’s not ideal but it might be useful for testing the experience, or as a stopgap (or not).

1 Like

For something that wants to track so deeply what’s going on in your note browsing - I would just write your own plugin.

Hook into the note open function, and before note change store your cursor position and previous note info, then output it into the new note (or however you want to output it).

No idea what that hook is, but here’s a jumping off point: Hooking into new file creation

1 Like

@holroy I came across this repo while looking for something else, and it looks perfect for you to get the needed code from:

This plugin for Obsidian remembers the cursor position and scroll for each note. This is very convenient when you switch between notes, go from link to link, go back, you do not need to scroll to the place where you were the last time.

1 Like