Pass data TO content script

Scenario: Command in plugin creates data. ContentScript should fetch that data for each note and display it.

In Joplin 1.5.14 we can call command from content script, what about returning data from that command - is it possible now?

In theory it should be possible. Did you try?

I tried few things, but didn't dig enough into Joplin internals:

Inside content script:

  • onclick="mycontent=webviewApi.executeCommand('command') returns undefined (maybe i need to try await for it)
  • webviewApi directly inside content script is undefined
  • tried setting variable in onStart section of plugin and access it from ContentScript but didn't found way to do it.

So these were few guesses i would appreciate a hint :slight_smile:

I haven't specify - its for MarkdownIt plugin, not Code Mirror.

Yes you would need to await the call, but it's possible it's just not working. I'll take a look at some point.

1 Like

I've looked into it but that would be tricky to implement, and although I wonder if it's really necessary. Your plugin set the view HTML, so when the command is executed, you could simply set the HTML again to whatever is needed. That would actually be a nice one way data flow, which is generally easier to manage.

Or if there's something else, I'd need a specific example with code, as maybe there's some other way to achieve this.

I'm looking for doing MarkdownIt version of automatic Backlinks to notes - backlinks plugin (instead of manually like its now).

function plugin(md, _options) {
    console.log(_options)
    

    function render_footnote_block_open2(tokens, idx, options) {

        return `<div id="backref">back</div><a href="#" onclick="let cont='asdf';async function a(){cont=await webviewApi.executeCommand('getCont');webviewApi.executeCommand('getCont').then(res=>console.log(res));document.getElementById('backref').innerHTML=cont};a();">asdf</a>`
    //return `<div id="backref">back</div><a href="#" onclick="document.getElementById('backref').innerHTML='test manual';">asdf</a>`
        
      }
      

    md.renderer.rules.footnote_block_open2   = render_footnote_block_open2;
    

    function footnote_tail2(state) {
 

        token = new state.Token('footnote_block_open2', '', 1);
        state.tokens.push(token);
  }
  md.core.ruler.after('inline', 'footnote_tail2', footnote_tail2);
		await joplin.commands.register({
			name: 'getCont',
			label: 'returns backlinks html',
			execute: async () => {
				console.log("called getCont()");

                // Here i would create HTML and pass it to markdown
				return "<b> testing html from command</b>"
			},
		});

Its possible to do it with panel view instead of Markdown plugin,
but that would clutter user interface.
Just thinking out loud but maybe ContentScript could have access to joplin.data methods?

In the next pre-release there will be support for bi-directional messages between content script and plugin, so you'll be able to use that to indirectly access any part of the API.

Note that I will remove webviewApi.executeCommand because it's no longer necessary (I didn't document it so maybe you were the only one using it)

2 Likes

Great :smiley: !
That's good news