zadam / trilium

Build your personal knowledge base with Trilium Notes
GNU Affero General Public License v3.0
27.2k stars 1.9k forks source link

[question] how to use 3rd js library? #1560

Closed leeyaunlong closed 3 years ago

leeyaunlong commented 3 years ago

I write a plugin, it's work fine. I try to save some data to trilium desktop local cookie, so I import jquery js-cookie under my script. But it cannot work. Detail as below

note structure :

my script note 
    js-cookie

code

let tmpDS =$.cookie("ds");

developer tool will return below error , how to solve it ?

image

zadam commented 3 years ago

This depends on the library. Some libraries are sensitive on where they run, some work fine.

This is actually jQuery plugin, not sure how will that interact with internal jquery instance.

But in the case of cookies I would just paste some random function from stack overflow, it's not very difficult, e.g.

function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}
leeyaunlong commented 3 years ago

Thanks, zadam.

I use below way solved it.

import js file , named cookie_js, then

const jq_cookie = require('cookie_js')

Is it a right way to call 3rd js libs ? Why I need to this ? (the trilium example code can directly use the import jschat object)

zadam commented 3 years ago

If child nodes are named without special characters then they are automatically exported as variables into the namespace. In the hindsight this was probably not a good idea since it's not very intuitive.

I recommend using the CommonJS style require() as in your example.