I am using electron-dl to download a file but I cant seem to make it work.
I did almost exactly same as defined in docs but i dont know what is wrong.
Error:
I get the following error on main process
TypeError: Cannot read property 'then' of undefined
at EventEmitter.ipcMain.on ( \electron-dl-test\main.js:24:7)
How to reproduce (setup):
use repo: https://github.com/mafar/electron-dl-test
npm install and then npm start
main.js:
ipcMain.on('download', (ev, args) => {
download(BrowserWindow.getFocusedWindow(), args.url, args.properties)
.then(dl => console.log(dl.getSavePath()))
.catch(console.error);
})
index.html as renderer:
document.getElementById("download-file").onclick = function () {
//
var ipcRenderer = require('electron').ipcRenderer;
ipcRenderer.send('download', {
url: 'https://textfiles.com/100/ad.txt',
properties: {
saveAs: true,
directory: 'C:\\'
}
});
//
};
Preview:
The download function is part of an object exported by electron-dl. You need to destructure (ES6) or reference it directly (ES5) when requiring the module:
const { download } = require("electron-dl") // ES6
or
var download = require("electron-dl").download // ES5
Related
In a .ts file I create a test to try and access a custom created command from command.js, createInbox function is underlined with red with the following message : Property 'createInbox' does not exist on type 'cy & EventEmitter
it.only('dsdsds', () => {
cy.createInbox().then((inbox) => {
console.log(inbox);
// { id: '...', emailAddress: '...' }
});
})
My command.js file look like this
const { MailSlurp } = require("mailslurp-client");
const mailslurp = new MailSlurp(Cypress.env("mailSlurpApiKey"));
Cypress.Commands.add("createInbox", () => {
return mailslurp.createInbox();
});
Cypress.Commands.add("waitForLatestEmail", (inboxId) => {
return mailslurp.waitForLatestEmail(inboxId);
});
I understand that I have to rename command.js to ts, however when I do that all custom commands is underlined with red with the following error :
Argument of type '"waitForLatestEmail"' is not assignable to parameter of type 'keyof Chainable
How could I fix this?
Solved by adding custom chainable interface to support folder
declare namespace Cypress {
interface Chainable {
createInbox(): Chainable<any>;
waitForLatestEmail(inboxId: number): Chainable<any>;
}
}
I had this problem aswell!
My solution:
I had my *.d.ts files located inside the cypress/support folder, right next to the *.ts files with all the custom commands. Moving those outside(!) the "support"-folder and into another one called "definitionFiles" (for example) worked beautifully!
Problem
I am trying to inject / replace environment variables with #rollup/plugin-replace. Unfortunately, I get this error:
TypeError: can't convert undefined to object
Code
// rollup.config.js
import replace from "#rollup/plugin-replace";
import { config } from "dotenv";
config();
export default {
// ...
plugins: [
replace({
values: { YOUTUBE_API: JSON.stringify(process.env.YOUTUBE_API) },
preventAssignment: true,
}),
// ...
}
And I call it like this:
onMount(() => {
(async function getPopular() {
videos = await axios.get("https://www.googleapis.com/youtube/v3/videos", {
part: "id, snippet, suggestions",
chart: "mostPopular",
key: YOUTUBE_API,
});
})();
});
What I tried
I logged out the variable and so can confirm that it exists. Also, if I remove the stringify function, I get another error:
ReferenceError: blablabblub is not defined
I have done this successfully in other projects. What the heck is wrong here?
So after some digging around with the same issue, I found it was related to object assignment. For example:
export default {
// ...
plugins: [
replace({
values: {
env: {
API_URL: process.env.API_URL,
API_VERSION: process.env.API_VERSION,
}
},
preventAssignment: true,
}),
// ...
}
// in some JS or Svelte file
const config = {
host: env.API_URL,
version: env.API_VERSION
}
// The above will result in a reference error of 'env' not being defined.
// in the same JS or Svelte file..
const envVars = env;
const config = {
host: envVars.API_URL,
version: envVars.API_VERSION
}
// this works just fine!
I haven't had anymore time to investigate, but my gut feeling is that rollup won't replace variable names when they are nested inside an object assignment. It might be nice for an optional flag to allow this, but it might also get very messy hence why they didn't do it.
I hope this helps if it's still an issue for you.
I'm using puppeteer library to access some sites and leach data from their HTML files. for that manner, I've got a Python script that should help me solve each captcha if there is.
now, to run the Python script, I'm using python-shell from npm, and committing this:
let options = {
mode: 'text',
args: [captchas[0].sitekey],
scriptPath: path.resolve('bypass'),
pythonPath: '/usr/bin/python',
}
console.log(`options.scriptPath`, options.scriptPath)
PythonShell.run(
`bypass/bypass.py`,
options,
(err, [, captchaKey]) => {
if (err) throw err
let solutions = [
{
_vendor: captchas[0]._vendor,
id: captchas[0].id,
text: captchaKey,
hasSolution: true,
},
]
resolve({ solutions })
}
)
I've got this error -
Solving captcha...
options.scriptPath MY_PATH/backend/bypass
file:///MY_PATH/backend/bypass/captchaBypasser.js:18
(err, [, captchaKey]) => {
^
TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator))
at file:///MY_PATH/backend/bypass/captchaBypasser.js:18
at PythonShell._endCallback (/MY_PATH/backend/node_modules/python-shell/index.js:254:20)
at terminateIfNeeded (/MY_PATH/backend/node_modules/python-shell/index.js:209:39)
at ChildProcess.<anonymous> (/MY_PATH/backend/node_modules/python-shell/index.js:182:13)
I've tried also to use spawn of child_process, like this:
const location = path.resolve('bypass/bypass.py')
console.log(`python ${location} ${captchas[0].sitekey}`)
const run = spawn('python', [location, captchas[0].sitekey])
run.stdout.on('data', ([, captchaKey]) => {
let solutions = [
{
_vendor: captchas[0]._vendor,
id: captchas[0].id,
text: captchaKey,
hasSolution: true,
},
]
resolve({ solutions })
})
and it appears to just crush without even an outcome.
So I've tried to try the actual command line:
python /MY_PATH/backend/bypass/bypass.py ae7317...TOKEN...d0dab8b75
BY THE WAY - I've TRYED ALSO python3 WITH VERSION 3.10.0
And now I've got this error:
from hcapbypass import bypass
File "/MY_PATH/backend/bypass/hcapbypass.py", line 35
def N_Data(req) -> str:
^
So I tried to use the latest version of Python, 3.10.0, that came out like a few weeks ago, and that's the outcome:
File "/MY_PATH/backend/bypass/bypass.py", line 4, in <module>
captcha_solved = bypass(sys.argv[1], 'google.com', True)
TypeError: bypass() missing 1 required positional argument: 's'
For your full understanding, here is the bypass and the solver codes:
bypass.py
from hcapbypass import bypass
import sys
captcha_solved = bypass(sys.argv[1], 'google.com', True)
print(captcha_solved)
hcapbypass
Link to github
Does anyone know what to do?
Thanks a lot!!!
OK, ive got the solution:
inside the hcapbypass.py file, the httpx module isn't presented, so I've created an environment with bin & lib folders to install it locally.
In addition, few functions got an argument that doesn't exist.
And of course, destructuring an array that is null / undefined - throws an error!
I am trying to implement this library with my React applicaiton however, I get an error when I import the package.
Error - TypeError: Object prototype may only be an Object or null: undefined
Package - https://www.npmjs.com/package/dukascopy-node
I tried importing it both ways, using require and using import from however it gives the same output
Function
import { getHistoricRates } from "dukascopy-node";
export const getHistoricalData = async (
instrument = "btcusd",
from = new Date("2018-01-01"),
to = new Date("2019-01-01"),
timeframe = "d1"
) => {
try {
const data = await getHistoricRates({
instrument: instrument,
dates: {
from: from,
to: to,
},
timeframe: timeframe,
format: "json",
});
console.log(data);
} catch (error) {
console.log("error", error);
}
};
As the import statement looks OK, there must be something wrong with your packages. I would try to delete the node_modules folder and reinstall all packages.
This is my go-to solution in such strange, unexplainable cases with imports.
Upgrading from Electron v2.0.3 to the latest relase v5.0.1
When I try running electron, I get the following error:
TypeError: app.makeSingleInstance is not a function
I believe this is because the api has changed. I cannot find what the equivalent for this would be. Any help would be appreciated!
main.js (was working fine in v2.0):
let appInstance= null,
mainWindow = null,
appInstance = app.makeSingleInstance(() => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
})
Yes, the API has changed since Electron 4.0: Planned Breaking API Changes (4.0):
app.makeSingleInstance
// Deprecated
app.makeSingleInstance((argv, cwd) => {
/* ... */
})
// Replace with
app.requestSingleInstanceLock()
app.on('second-instance', (event, argv, cwd) => {
/* ... */
})
More details are available in the documentation for the requestSingleInstanceLock() method and the 'second-instance' event.