Fivefold / linkding-injector

Injects search results from the linkding bookmark service into search pages like google and duckduckgo
MIT License
190 stars 7 forks source link

Support for Shaarli #7

Open sebw opened 1 year ago

sebw commented 1 year ago

Hey there!

Love this extension! Somehow Linkding still has a couple of issues that doesn't make it production ready for me.

I've use Shaarli for years and I would love a similar extension compatible with it.

I've looked at your code (even though I'm not versed in building browser extensions) and it looks like it would be fairly simple to transpose API calls from Linkding to Shaarli API calls.

Only difference is around auth, Shaarli works on a JWT token: https://github.com/shaarli/api-documentation/blob/master/api-authentication.md

API call for search is fairly classic: https://bookmark.example.com/api/v1/links?searchterm=linux&limit=10

Output:

[
    {
        "id": 4956,
        "url": "https:\/\/addons.mozilla.org\/en-GB\/firefox\/addon\/blah\/",
        "shorturl": "PJJIcg",
        "title": "the title",
        "description": "the description",
        "tags": [
            "extension",
            "firefox"
        ],
        "private": false,
        "created": "2022-12-18T22:13:45+01:00",
        "updated": "2022-12-18T22:14:00+01:00"
    }
]

I can spin up a Shaarli instance if needed.

Fivefold commented 1 year ago

Hey @sebw!

A few thoughts:

  1. Yeah, looks like most of the work would probably be on the configuration side (user UI for choosing linkding or Shaarli API and configuring that).

  2. This is likely not relevant to you personally but would probably mean the extension should be renamed to bookmark-injector or similar down the line.

  3. Of course the injected results would still be styled like linkding, but theming support is not essential and not something I'm willing to tackle for now.

Is this a feature request only or do you intend to make a PR? In case of the latter I would ask you to wait until the next release. I'm in the process of changing the extension to support manifest v3 and that will include changes in a lot of files.

sebw commented 1 year ago

Hey @Fivefold

  1. one extension covering multiple bookmarking services would be even better!
  2. agree bookmark-injector would make sense
  3. I like the theme as it is!
  4. as much as I'd love to help out, I have never built an extension before and never wrote anything in node but I can definitely test, document or translate stuff! I actually gave a shot to building the extension, but the resulting ZIP wouldn't load (firefox reporting it corrupted, but can still install in debug mode, but quickly fail at the configuration step... I see the testConnection and get a 200 but get an error still).
Fivefold commented 1 year ago

Maybe you can write and test a JavaScript function for encoding/generation of the JWT token as shown for PHP at the shaarli docs https://github.com/shaarli/api-documentation/blob/master/api-authentication.md#php You can do this completely outside of the extension context.

This would take some of the work off me. It will still most likely take weeks until I can actually get to implement it though.

sebw commented 1 year ago

Here's a working example using jsonwebtoken library.

let jwt = require('jsonwebtoken');
let now = Math.floor(Date.now() / 1000)

const shaarli_api_secret = 'shaarli-api-secret';
const payload = {
  "iat": now
};

let token = jwt.sign(payload, shaarli_api_secret, { algorithm: 'HS512'});

console.log(token)
Fivefold commented 1 year ago

Thank you very much, @sebw! Looks good and easy.

Unfortunately I just found out using node modules in browser extensions requires bundling them with the extension. I'm using rollup as a bundler, mostly because I started from the code of the official linkding extension and it seemed to do the job well. I'm very inexperienced with rollup. It can probably bundle node modules too but it would likely take a lot of time for me to set it up just for encoding the JWT token. I might take a shot at it nontheless.

Ideally, I'm looking for a vanilla JavaScript solution, although I'm not sure if JavaScript supports base64 encoding or sha512 hashing out of the box or how much trouble it would be to implement them.

If you do know rollup that would be even better and cleaner though.

gingerbeardman commented 1 year ago

Just checking out the project and though I might be of help

Fivefold commented 1 year ago

Thank you @gingerbeardman, this looks good! I wasn't aware of the subtlecrypto API. Will try it out the next time I take a shot at this.

In the meantime I tried to get rollup to bundle node modules with the help of ChatGPT but even then I was unable to get the bundling working properly. Apparently it should be enough to add preferBuiltins: false to the resolve Plugin in rollup.js. https://github.com/Fivefold/linkding-injector/blob/a66b0e883d6df9ef37a8290d23a7b20025902507/rollup.config.js#L56 https://github.com/Fivefold/linkding-injector/blob/a66b0e883d6df9ef37a8290d23a7b20025902507/rollup.config.js#L81

The resolve plugin itself is from @rollup/plugin-node-resolve and is specifically there to bundle node modules. Unfortunately I still got ReferenceErrors when trying to access stuff from imported node modules.