deepl-node icon indicating copy to clipboard operation
deepl-node copied to clipboard

oe.URLSearchParams is not a constructor Error

Open dcoales opened this issue 2 years ago • 12 comments

I have tried to use this library but when I run my code I get the following error in the browser console:

index.3240d547.js:10 Uncaught (in promise) TypeError: oe.URLSearchParams is not a constructor
    at je (index.3240d547.js:10:69597)
    at a.Translator.translateText (index.3240d547.js:10:73014)
    at Le (index.3240d547.js:10:71558)

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser Electron/19.1.8
  • Version [e.g. 22]
  • Node 16.15.0

Additional context I'm using typescript and have no errors when I build my app - only when I run it. However, I'm not a professional programmer so it's possible I've installed the wrong version of something. My tsconfig.json file is below:

{
  "compilerOptions": {
    "lib": [
      "es2021",
      "dom"
    ],
    "module": "commonjs",
    "target": "es2021",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node"
  }
}

and my package.json

{
  "name": "logseq-deepl-integration",
  "version": "0.0.1",
  "description": "Translate selected blocks via deepl",
  "main": "dist/index.html",
  "targets": {
    "main": false
  },
  "scripts": {
    "build": "parcel build --no-source-maps index.html --public-url ./",
    "dev": "parcel build --no-optimize index.html --public-url ./"
  },
  "author": "dcoales",
  "license": "MIT",
  "dependencies": {
    "@logseq/libs": "^0.0.10",
    "deepl-node": "^1.7.3"
  },
  "logseq": {
    "id": "logseq-deepl",
    "title": "Logseq Deepl Integration",
    "author": "dcoales",
    "description": "Translate selected blocks via deepl"
  },
  "devDependencies": {
    "@types/jest": "^29.0.3",
    "@types/node": "^18.11.9",
    "buffer": "^5.5.0",
    "events": "^3.3.0",
    "parcel": "^2.8.0",
    "path-browserify": "^1.0.1",
    "punycode": "^1.4.1",
    "querystring-es3": "^0.2.1",
    "stream-http": "^3.2.0",
    "ts-node": "^10.9.1",
    "typescript": "^4.8.3",
    "url": "^0.11.0",
    "util": "^0.12.5"
  }
}

I'm running this as a plugin to a tool called LogSeq. The plugin code is below. Any help would be much appreciated. Thanks.

import '@logseq/libs';
import {BlockEntity} from "@logseq/libs/dist/LSPlugin";
import { SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin.user';
import * as deepl from 'deepl-node';
import {TextResult} from "deepl-node";

/**
 * entry
 */

function main() {

    logseq.useSettingsSchema(settings);

    logseq.Editor.registerSlashCommand('Get Jira Details for Selection', (_) => {
        return translate();
    })

    logseq.App.registerCommand('deeplTranslate', {
        key: 'deeplTranslate',
        label: 'Translate the selected lines',
        desc: 'Get translations for each of the currently selected lines',
        keybinding: {binding: 'mod+alt+t'}
    }, (e) => {
        return translate();
    })
}

async function translate() {
    let selection: BlockEntity[] | null = await logseq.Editor.getSelectedBlocks();
   //console.log(selection);
    if (!selection || selection.length === 0) {
        const block: BlockEntity | null = await logseq.Editor.getCurrentBlock();
        if (block){
            selection = [block];
        }
    }
    const lines:string[] = [];
    if (selection){
        for (let b of selection){
            lines.push(b.content);
        }
    }

    const authKey = logseq.settings?.APIToken;
    const translator = new deepl.Translator(authKey);
    const results:TextResult[] = await translator.translateText(lines,null,logseq.settings?.language);
    results.map((result: deepl.TextResult) => {
        console.log(result.text);
    });
}

const settings: SettingSchemaDesc[] = [
    {
        key: "language",
        description: "The language for the translated text",
        type: "string",
        default: "pl",
        title: "Translated text language",
    },
    {
        key: "APIToken",
        description: "Deepl API token",
        type: "string",
        default: "",
        title: "Deepl API token",
    }
];

logseq.ready(main).catch(console.error);

dcoales avatar Jan 07 '23 21:01 dcoales