Unable to use getElementsByTagName("body") - node.js

Here's the code that results in an error each time I run it. My goal is to scrap the content from the URL, remove all HTML, and return it:
console.log("Fetching: " + inputData.tweeturl);
fetch(inputData.tweeturl)
.then(function(res) {
return res.text();
}).then(function(body) {
var rawText = body.getElementsByTagName("body")[0].innerHTML;
var output = { id: 100, rawHTML: body, rawText: rawText };
callback(null, output);
})
.catch(callback);
The problem is with var rawText = body.getElementsByTagName("body")[0].innerHTML;
The error I receive is:
Bargle. We hit an error creating a run javascript. :-( Error:
TypeError: body.getElementsByTagName is not a function eval (eval at (/var/task/index.js:52:23), :16:24) process._tickDomainCallback (node.js:407:9)

Unfortunately - there is no JS DOM API in the Code by Zapier triggers or actions (that is because it isn't run in a browser and doesn't have the necessary libraries installed to fake it).
You might look at Python and instead, and https://docs.python.org/2/library/xml.etree.elementtree.html. Decent question and answer is available here Python Requests package: Handling xml response. Good luck!

Any function not supported by Zapier will result in a TypeError. I needed to use a regular expression to achieve this.

Related

DiscordJS + NodeJS: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)

Having an issue where I will randomly get this error after about an hour of my code running.
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
Here is my code:
function matchMaking() {
setInterval(function() {
fs.readFile('./rankedQueue.json', 'utf8', function(err, data) {
const file = JSON.parse(data); //The line the error occurs
if (err) {
console.log(err)
} else {
//lots of code here
}
})
}, 10 * 1000)
}
edit: This is the content of the JSON file.
{
"queue": [],
"waiting": [],
"lowLevel": [],
"placeHolder": [
{
"test": "test"
}
]
}
The arrays are being pushed to, and then spliced a couple times a minute.
After searching here and some forums, I've tried using fs.readFileSync, which makes the code not run at all. And now I'm finding some specific examples of this error that I can't quite seem to make the solutions apply to me. If anyone has any idea of what I should be changing, It would be appreciated.
Thanks in advance.
As the errors said data is not in json type
Can you make sure the file is valid and it's is what JSON.parse() can parse
How to use fs.readFileSync
Using fs.readFileSync returns a buffer. You can easily convert the buffer into something readable. You use buffer.toString('ascii'). Also, fs.readFile has a callback argument while readFileSync DOESN'T. What I think you were doing about readFileSync was:
fs.readFileSync('./rankedQueue.json', 'utf8', function(err, data) {
const file = JSON.parse(data); //The line the error occurs
if (err) {
console.log(err)
} else {
//lots of code here
}
})
But you should actually be doing this:
var data = fs.readFileSync('./rankedQueue.json');
var JSON = JSON.parse(data.toString('ascii'));
You could drop the 'ascii'.
Incorrect / Hacky Answer
Use require instead. Node.JS has it so it can parse a JSON file for you. Keep in mind that the way you require a file compared to fs is different.
If your code is located at /project/foobar/code.js
JSON is at /project/rankedQueue.json
fs automatically goes to the top of where your project folder is, e.g. /project & So ./rankedQueue.json
require does not do this, and stays in the folder where the file is. You have to add ../ for every folder you want to go above. So: ./../rankedQueue.json.
I'd suggest also running your JSON through a validator as well so it can tell you whats wrong.

Electron app: Reference mainWindow object in another module?

I am building an electron app, where the mainWindow object is created following the quick start: http://electron.atom.io/docs/tutorial/quick-start/.
As per this quick start, it is created asynchronously. The problem that I run into, is that for instance when I want to send messages from main to renderer process, I need to reference the mainWindow object. If this happens to be in a module that I require, then I need a means to make this module know of the mainWindow object.
I could of course prepend it with global., but I know that this is very much advised against. So I wish to do it more elegantly.
I came across this post: Asynchronous nodejs module exports; which appears to offer a solution. Taking the main.js file from the quick start (see above link, it's explicitly shown there), it appears I would add to the createWindow function
if( typeof callback === 'function' ){
callback(mainWindow);
}
and export the main.js module as
module.exports = function(cb){
if(typeof mainWindow !== 'undefined'){
cb(mainWindow);
} else {
callback = cb;
}
}
Then, in a higher-level script, I would require as follows:
let main = require('./main.js');
let lib = require('./lib.js'); // Library where I need a mainWindow reference
main(function(window) {
lib.doSomething(window);
});
where lib.js looks like
module.exports.doSomething = function(window) {
// Do something with window object, like sending ipc messages to it
window.webContents.send('hello-from-main', "hi!");
}
Although the simple case in the original post 'Asynchronous nodejs module exports' works fine, I cannot get it to work like described above; running the app it complains Uncaught Exception: TypeError: Cannot read property 'webContents' of null. This is also the case if I directly require lib.js within main()'s callback (which I know is also advised against).
I confess that I do not fully understand the simple case of the post, as I am rather new to node. This prevents me from fixing my own implementation of it, which I agree is blunt copy/pasting which reasonably should be expected to fail. Could somebody help me with how to correct above method, or advise me of a different approach to make it work? Thank you!
I have created the npm package electron-main-window for the same.
Install:
$ npm install electron-main-window
or
$ yarn add electron-main-window
Usage:
// Import ES6 way
import { getMainWindow } from 'electron-main-window';
const mainWindow = getMainWindow();
// Import ES5 way
const mainWindow = require('electron-main-window').getMainWindow();
// e.g:
if(mainWindow !== null ){
mainWindow.webContents.send('mainWindowCommunication', "This is a test message");
}
Whooops! The devil is in the details... I had defined on top of main.js
let mainWindow = null, callback;
which caused the error! Should be
let mainWindow, callback;
then it works perfectly!
P.s. Instead of deleting my post, I opted for keeping it and answering myself for future reference of other people who need asynchronous exporting.

Photoswipe and Require.js configuration

I have a module which is dependent upon both, PhotoSwipe and PhotoSwipeUI_Default. I am requiring both modules, like so and inside of my module I am initialising my gallery. EG:
define("modules/lightboxStart", ["vendor/PhotoSwipe", "vendor/PhotoSwipeUI_Default"], function (PhotoSwipe, PhotoSwipeUI_Default) {
var StartGallery = function(){
var lightBox = new PhotoSwipe($pswp, PhotoSwipeUI_Default, items, options);
lightBox.init();
}
StartGallery()
}
The gallery then works as expected but require is throwing a mismatched anonymous define module error in PhotoSwipe.js.
After looking through the docs I came to the conclusion that I need to run both scripts(PhotoSwipe and PhotoSwipeUI_Default) through the require.js API.
I then defined each, like so:
define('PhotoSwipe', function() {
//PhotoSwipe script here
return PhotoSwipe
})
And
define('PhotoSwipeUI_Default', function() {
//PhotoSwipeUI_Default script here
return PhotoSwipeUI_Default
})
This then eliminates the mismatched anonymous define module error I was getting earlier, but I am getting this error now instead:
PhotoSwipe is not a function
How could I fix this? I am essentially trying to access the PhotoSwipe function within PhotoSwipe.js and pass through my arguments but I am unable to do so without generating a load of js errors on the page and I can not figure out why.
Help much appreciated!
define( ["modules/lightboxStart", "vendor/PhotoSwipe", "vendor/PhotoSwipeUI_Default"], function (lightboxStart, PhotoSwipe, PhotoSwipeUI_Default) { //i think so}

Calling a module function inside a Nodejs callback

I have a module that writes to a log file. (coffeescript sorry, but you get the idea!)
require = patchRequire(global.require)
fs = require('fs')
exports.h =
log: ()->
for s in arguments
fs.appendFile "log.txt", "#{s}\n", (e)->
if (e) then throw e
It works file when I call it directly. But when I call it from a callback, for example casperjs start event:
h = require('./h').h
casper = require('casper').create()
casper.start "http://google.com", ()->
h.log("hi")
casper.run()
... I always get this or similar "undefined" TyepError:
TypeError: 'undefined' is not a function (evaluating 'fs.appendFile("log.txt", "" + s + "\n", function(e) {
if (e) {
throw e;
}
})')
Googling this doesn't give many clues!
CasperJS runs on PhantomJS (or SlimerJS) and uses its modules. It is distinct from nodejs. PhantomJS' fs module doesn't have an appendFile function.
Of course you can use fs.write(filepath, content, 'a'); to append to a file if used in casper. If you still want to use your module both in casper and node then you need to write some glue code like
function append(file, content, callback) {
if (fs.appendFile) {
fs.appendFile(file, content, callback);
} else {
fs.write(file, content, 'a');
callback();
}
}
I think the problem is with the coffeescript. Try using a splat parameter instead of relying on the arguments object.
log(statements...)
If that doesn't work, you might need to look at the javascript output or try the same thing in plain JavaScript and see if you get the same error.

Node-Webkit with external module containing native code

I'm using node-webkit with an external module called edge.
According to the node-webkit docs modules that contain native code must be recompiled using nw-gyp as oppose to node-gyp. I was able to recompile without error and node-webkit seems to import the module OK.
Heres my code. The code I'm trying to use:
var edge = require('edge.node');
var hello = edge.func(function () {/*
async (input) =>
{
return ".NET welcomes " + input.ToString();
}
*/});
hello('Node.js', function (error, result) {
if (error) throw error;
console.log(result);
});
Which throws the following error when run within node-webkit.
Uncaught TypeError: Object [object Object] has no method 'func'
If write the object out to console.log I can see:
Object {initializeClrFunc: function}
initializeClrFunc: function () { [native code] }
__proto__: Object
So the module seems to have loaded. If I run the same code outside of node-webkit, everything works perfectly and I can access the func function. This is driving me crazy - and any help would be really appreciated.
func method is provided by edge.js, the wrapper around edge.node native module. So you should replace require('edge.node') by require('edge').

Resources