Skip to content

await is only valid in async functions #7

@sheep94lion

Description

@sheep94lion

The customRequestHandler return error: await is only valid in async function.
Removing await keyword from the code seems working.
trilium version: 0.48.1-beta
My revised code:

// HowTo:
//  1- Add this file to trilium as a `JS Backend` code note.
//  2- Set label `customRequestHandler` to 'singlefile2trilium'.

const template = `
<script>
    var iframe = document.getElementById('r-iframe');

    function pageY(elem) {
        return elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop;
    }

    function resizeIframe() {
        var height = document.documentElement.clientHeight;
        height -= pageY(iframe) + 20 ;
        height = (height < 0) ? 0 : height;
        iframe.style.height = height + 'px';
    }

    iframe.onload = resizeIframe;
    window.onresize = resizeIframe;
</script>

<iframe id="r-iframe" style="width:100%" src="data:text/html;charset=utf-8,%%CONTENT%%" sandbox=""></iframe>
`;

const {req, res} = api;
const {title, url, content} = req.body;

if (req.method == 'POST') {
    api.log("==========================");
    // create render note
    const renderNote = (api.createNewNote({
        parentNoteId: "IIgUiHfe3hu0",
        title: title,
        content: '',
        type: 'render'
    })).note;
    renderNote.setLabel('clipType', 'singlefile2trilium');
    renderNote.setLabel('pageUrl', url);
    renderNote.setLabel('pageTitle', title);

    // create child `content.html`
    var wrapped_content = template.replace("%%CONTENT%%", encodeURIComponent(content));
    const htmlNote = (api.createNewNote({
        parentNoteId: renderNote.noteId,
        title: 'content.html',
        content: wrapped_content,
        type: 'code',
        mime: 'text/html'
    })).note;
    htmlNote.setLabel('archived');

    // link renderNote to htmlNote
    renderNote.setRelation('renderNote', htmlNote.noteId);

    res.send(201); // http 201: created
}
else {
    res.send(400); // http 400: bad request
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions