You are not logged in.

#1 2021-11-30 09:57:30

rethus
Member
Registered: 2016-02-16
Posts: 12

notify-send with kde-klipper script didn't work

I've created a script for KDE Klipper Action to upload an image, that are in the clipper to imgBB.com.
On the CLI it works nice, and shows the "notify-send" messages, I do create out of the script at my desktop.
The Script starts with `#!/usr/bin/node` and is an JS-Script.

If I run it as clipper-action, it doesn't show the message at all, and no image was uploaded.

BUT I've created an additional test as bash-script:

#!/bin/bash
notify-send --expire-time=5000 --icon=dialog-information "Klipper Info:" "TEST 123"

this works.

So I think the issue seems to be caused by using node. But I don't have any clue what exactly is the issue here.

I wonder how I could get a usable response from this clipper-action, what happend there.

Offline

#2 2021-11-30 10:06:03

karabaja4
Member
From: Croatia
Registered: 2008-09-14
Posts: 1,048
Website

Re: notify-send with kde-klipper script didn't work

Can you post the full Node.js script you mentioned?

Offline

#3 2021-12-03 13:19:09

rethus
Member
Registered: 2016-02-16
Posts: 12

Re: notify-send with kde-klipper script didn't work

Sure, here it is:

#!/usr/bin/node
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const request = require('request');
const fs = require('fs');

const apiURL = 'api.imgbb.com';
const apiKey = 'xyz123123123';

const apiPath = '/1/upload?key=' + apiKey;

async function clipboard() {
    let randomName = 'Screenshot_'+Math.random().toString(36).substring(7);
    const imageFileType = '.jpg';
    const {
        stdout,
        stderr
    } = await exec(`xclip -selection clipboard -o -t image/jpeg -o > /tmp/${randomName}${imageFileType}`);

    let img = new Buffer.from(stdout).toString('base64');

    var options = {
        url: `https://${apiURL}${apiPath}`,
        headers: {
            "Content-Type": "multipart/form-data"
        },
        formData : {
            image: {
                value:  fs.createReadStream(`/tmp/${randomName}${imageFileType}`),
                options: {
                filename: `${randomName}${imageFileType}`,
                contentType: 'image/jpeg'
                }
            },
        }
    };
    request.post(options, function(err, res, body) {
        const parsedBody = JSON.parse(body);
        const {status, success} = parsedBody;
        if(status===200 && success){
            // exec(`echo -n "${parsedBody.data}"`);
            exec(`qdbus org.kde.klipper /klipper setClipboardContents '${parsedBody.data.url}'`);
            exec(`notify-send --expire-time=5000 --icon=dialog-information "Klipper Info:" "URL des Uploads\n\n${parsedBody.data.url}"`);
        }else{
           exec(`notify-send --expire-time=5000 --icon=dialog-error "imgBB.com:" "Upload fehlgeschlagen!\n\nMessage: ${parsedBody.error.message}"`); 
        }
    });
}
clipboard();

What it does:

  • Check the Klipper if the current entry is an image

  • If yes, save the contant as image to /tmp

  • Send the created Image via API-Request to imgBB

  • If successfully finished, notify with the url which was generated, if not notify with an error

Last edited by rethus (2021-12-03 14:56:33)

Offline

#4 2021-12-03 19:30:24

karabaja4
Member
From: Croatia
Registered: 2008-09-14
Posts: 1,048
Website

Re: notify-send with kde-klipper script didn't work

Few things to check:

  • Did you chmod +x your script.js?

  • Does it run correctly if you run "node script.js" from the terminal?

Few unrelated pointers on your script:

You have two -o flags on xclip.
Not a good practice to mix var and const.
If imgbb API returns an error, body parameter will probably be null, JSON.parse(null) is null, and after that deconstructing a null will throw an error.
You are not awaiting the exec calls and thus not handling (however unlikely) errors.

Also, imgbb.com seems down for me at the moment so unable to test your script in action.

Last edited by karabaja4 (2021-12-03 19:32:58)

Offline

#5 2021-12-08 08:44:21

rethus
Member
Registered: 2016-02-16
Posts: 12

Re: notify-send with kde-klipper script didn't work

Sorry for the Delay, was a bit busy in the last days.

I'd still made it `+x` to be able to execute it in cli.
Yes it works like a charm, if I only execute it (as I have the SHEBANG `#!/bin/node` as first line. But if I remove it, it also work with `node <scriptname`.

Offline

Board footer

Powered by FluxBB