OpenBullet2 icon indicating copy to clipboard operation
OpenBullet2 copied to clipboard

KeyNotFoundException

Open aydynx opened this issue 2 years ago • 6 comments

Version of the software

0.2.1

Operating system

Windows 10

Browser / Native

Chrome 98

What happened?

The below block gives this error KeyNotFoundException KeyNotFoundException The given key was not present in the dictionary." but it works fine in my NodeJs 17.4.0 console.

Relevant LoliCode if needed

BLOCK:Script
LABEL:get proxies from links
INTERPRETER:NodeJS
INPUT proxyLinksList
BEGIN SCRIPT
const fs = require("fs");
const http = require("http");
const https = require("https");

proxyLinksList = proxyLinksList.toString().split("\n");

function getProxyLinks() {
  for (let i = 0; i < proxyLinksList.length; i++) {
    if (proxyLinksList[i].startsWith("https")) {
      https.get(proxyLinksList[i], (res) => {
        res.setEncoding("utf8");
        let body = "";
        res.on("data", (chunk) => {
          body += chunk;
        });
        res.on("end", () => {
          const proxyRegex = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}/g;
          const proxies = body.match(proxyRegex);
          return proxies;
        });
      });
    } else {
      http.get(proxyLinksList[i], (res) => {
        res.setEncoding("utf8");
        let body = "";
        res.on("data", (chunk) => {
          body += chunk;
        });
        res.on("end", () => {
          const proxyRegex = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}/g;
          const proxies = body.match(proxyRegex);
          return proxies;
        });
      });
    }
  }
}

let scriptResults = getProxyLinks();
END SCRIPT
OUTPUT String @scriptResults
ENDBLOCK

aydynx avatar Mar 03 '22 23:03 aydynx

Can you please post a sample input and sample output of the script? Otherwise I wouldn't know how to debug this. If it's something private maybe send it to me via email, you can find my email in the readme of the repo.

openbullet avatar Mar 14 '22 18:03 openbullet

Can you please post a sample input and sample output of the script? Otherwise I wouldn't know how to debug this. If it's something private maybe send it to me via email, you can find my email in the readme of the repo.

Hello, the input should be an array of links [“https://google.com”, “https://proxyscrape.com”], and the output should be an array of all the proxies found on the input array [“1.1.1.1”, “2.2.2.2”].

aydynx avatar Mar 14 '22 18:03 aydynx

You put String as type of the output array, have you tried putting ListOfStrings?

openbullet avatar Mar 14 '22 21:03 openbullet

After testing a bit, it appears that the request is not being awaited and the script immediately ends. Please read this to learn how to synchronize requests in node.js https://usefulangle.com/post/170/nodejs-synchronous-http-request

Instead of making the request sync, you could also make it return a Promise and then await it, read this https://github.com/openbullet/OpenBullet2/issues/99 to see how this works in practice. Let me know if you have any more doubts.

I will leave this issue open since I noticed that OB2 isn't happy with the ListOfString return type in scripts.

openbullet avatar Mar 16 '22 07:03 openbullet

After testing a bit, it appears that the request is not being awaited and the script immediately ends. Please read this to learn how to synchronize requests in node.js https://usefulangle.com/post/170/nodejs-synchronous-http-request

Instead of making the request sync, you could also make it return a Promise and then await it, read this #99 to see how this works in practice. Let me know if you have any more doubts.

I will leave this issue open since I noticed that OB2 isn't happy with the ListOfString return type in scripts.

Do you mean i should learn how to make asynchronous requests? Either way, changing the script to use the async/await syntax seems to have fixed it, although like still having problems with the output type. However, there are workarounds, such as getting the output as a string separated by new lines, and splitting it through the built in ob2 function...

aydynx avatar Mar 17 '22 23:03 aydynx

Yeah, for now just do that, I will fix the ListOfStrings and DictionaryOfStrings output types, sorry about it 😅

openbullet avatar Mar 18 '22 20:03 openbullet