-3

I am trying to make a electron program that could log the text of a TextArea in a HTML file but for some reason I get this error:

(node:8664) UnhandledPromiseRejectionWarning: TypeError: mainWindowHTML.getTextAreaText is not a function at Object.module.exports.Save (C:\Users\user\Documents\JavaScript\node.js\Electron\INTERFACE_EDITOR\menuScripts.js:12:33) at click (C:\Users\user\Documents\JavaScript\node.js\Electron\INTERFACE_EDITOR\index.js:36:37) at MenuItem.click (C:\Users\user\Documents\JavaScript\node.js\Electron\INTERFACE_EDITOR\node_modules\electron\dist\resources\electron.asar\browser\api\menu-item.js:55:9) at Function.executeCommand (C:\Users\user\Documents\JavaScript\node.js\Electron\INTERFACE_EDITOR\node_modules\electron\dist\resources\electron.asar\browser\api\menu.js:30:13) (node:8664) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:8664) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

In my main class I basically load the HTML file into a browser window which works and when a menuitem is clicked it calls this part of my menuScripts.js script:

const url = require('url');
const path = require('path');

module.exports.Save = async(dialog) => {

const mainWindowHTML = url.format({
    pathname: path.join(__dirname , "./htmls/MainWindow.html"),
    protocol: "file",
    slashes: true
    });

const text = mainWindowHTML.getTextAreaText();
    console.log(text);
}

module.exports.New = async(dialog) => {

}

module.exports.Exit = async(window) => {
    window.close();
}

And this is my MainWindow.html file:

<html lang="en">
<head>
    <title>document</title>
</head>
<body>
    <textarea id="code" cols="30" rows="10"></textarea>
</body>
<script>
    module.exports.getTextAreaText = async() => {
        return (document.getElementById("code").innerText)
    }
</script>
</html>
2
  • 2
    mainWindowHTML is the result of calling url.format (const mainWindowHTML = url.format...), which returns a string. Strings don't have a method called getTextAreaText. Commented Dec 17, 2018 at 17:55
  • Okay, so how do I reference the MainWindow.html itself? Commented Dec 17, 2018 at 18:17

2 Answers 2

0

Can't you just do:

<html lang="en">
<head>
    <title>document</title>
</head>
<body>
    <textarea id="code" cols="30" rows="10"></textarea>
    <script src="menuScripts.js"></script>
</body>
</html>

and in menuScripts.js:

const url = require('url');
const path = require('path');

module.exports.Save = async(dialog) => {

const mainWindowHTML = url.format({
    pathname: path.join(__dirname , "./htmls/MainWindow.html"),
    protocol: "file",
    slashes: true
    });

const text = document.getElementById("code").innerText;
    console.log(text);
}

module.exports.New = async(dialog) => {

}

module.exports.Exit = async(window) => {
    window.close();
}
Sign up to request clarification or add additional context in comments.

5 Comments

The problem is that I am getting a document is not defined error
Okay, well then the problem is as mentioned in the comments. url.format just creates a string, not importing the content.
Maybe use something like this stackoverflow.com/a/4819129/7296909
so how do I import the content?
I don't get why you would want to import html to javascript? You could just get the elements innertext by including the script in the html and use document.getElementById to get the element content
0

Okay, well I solved the problem using ipcRenderer instead of exporting functions which didn't really work. I really appreciate the people who tried helping!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.