Load webpage in NodeJs - javascript

I'm just wondering how I can load a webpage within nodejs, I've been searching for 2 days and I can't find anything. Either I'm using the wrong search terms or I'm just not looking for what I actually need.
I need to open a link to authenticate an account and I've retrieved the URL that I need to open but I'm not sure how. I've tried http.request but it mustn't be loading the page as when I check if the account has been verified it hasn't.
How would I go about this?
Thanks (Sorry for nooby question and bad formatting)
Buck
P.S Oops I wrote it in such a hurry I forgot to add the snippet of my code
var http = require('http');
var options = {
host: Host, //Both variables defined earlier in the code this is just a snippet of the http.request part
path: Path
};
callback = function(response) {
var string = '';
response.on('data', function (blob) {
string += blob;
});
response.on('end', function () {
console.log(string);
});
}
http.request(options, callback).end();
It just returns
['R޾�\s۝�V������T��:�I����$��v�* �*�;�* P���q�ܠ���5�E!9��I���v��r��� �CmO����q��<�>���&�趩�C��i�&��a��q�(��1a4I^XvLe�T˔�|��M�3�EA!نY\0�h�R��#r�b�a��Yr��z��1аB
Even when I try
console.log(string.toString('utf8'));

I managed to find what the problem was and fix it,
Further above in my code I had not declared an important variable properly.
http.request works perfectly

Related

How do you store links outside Javascript?

I am trying to create a website that takes you to a random website. However, I couldn't really find any answers to store the links of websites outside javascript, forcing me to store them in the script itself. But this would become an issue in the future when I would need to navigate around the script and it's going to be difficult to work with. What should I do?
example script:
function clicked(){
window.open(links[Math.floor(Math.random()*max)]);
}
var links = [
"linkexample.com",
"linkexample2.com",
"linkexample3.com"
];
Like the last comment said the best way is to store them in your server script to access them with ajax here's an example of Nodejs server script and js browser Script
Nodejs
const express = require("express");
var app = express();
app.post("/links", (req, res) => {
var links = [
"linkexample.com",
"linkexample2.com",
"linkexample3.com"
];
links = JSON.stringify(links);
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.send(links)
});
app.listen(3000, "localhost");
JS Browser Script
var xhr = new XMLHttpRequest();
xhr.onloadend = function(e) {
//You can see your links array in the console
//You can also use JSON.parse() to parse the array in you script and use it
console.log(e.currentTarget.responseText)
}
xhr.open("POST", "http://localhost:3000/links", true)
xhr.send()
thanks, everyone for answering. I managed to figure out my own solution which was to create an additional javascript file just for the links then get the value from there
external.js
var links = [
"linkexample.com",
"linkexample2.com",
"linkexample3.com"
];
main.js
function clicked(){
window.open(links[Math.floor(Math.random()* links.length)]);
}
I changed the max variable since it wasn't working for some reasons but at least it works perfectly now :D

Is there a better way than eval() in this scenario?

It is a web app, using Google Apps Script, running as the user accessing the app.
We have custom data and code for some users.
That custom information is in a text file within the developer's Google Drive, with only View access from the specific user.
The content of that text file could be like below dummy code:
var oConfig = {
some : "OK",
getinfo : function (s) {
return this.some + s;
}
}
In order to get that custom data / code into the app, we can use eval() as shown below:
var rawjs = DriveApp.getFileById(jsid).getBlob().getDataAsString();
eval(rawjs);
Logger.log(oConfig.getinfo("?")); // OK?
My questions are:
Is there a better way to achieve this goal than eval()?
Is eval() secure enough in this case, considering that the text file is only editable by the developer?
Thanks, Fausto
Well, it looks secure enough. But using eval has other problems, like making it difficult to debug your code, and possibly some other problems.
If you're generating such custom data within your code, I imagine the variety of such customizations is enumerable. If so, I'd leave the code within your script and save in Drive just data and use indicators (like function variants names) of how to rebuild the config object in your script. For example:
function buildConfig(data) {
var config = JSON.parse(data); //only data, no code
config.getInfo = this[config.getInfo]; //hook code safely
return config;
}
function customInfo1(s) { return this.some + s; }
function customInfo2(s) { return s + this.some; }
function testSetup() {
//var userData = DriveApp.getFileById(jsid).getBlob().getDataAsString();
var userData = '{"some":"OK", "getInfo":"customInfo1"}'; //just for easier testing
var config = buildConfig(userdata); //no eval
//let's test it
Logger.log(config.getInfo('test'));
}
It seems secure. But, it will make your execution process slower if you have large data in your text file.
I would still suggest to use JSON.parse() instead of eval() to parse your custom data/code.
{
some : "OK",
getinfo : "function(s){return this.some +\" \"+ s;}"
}
var rawjs = DriveApp.getFileById(jsid).getBlob().getDataAsString();
var oConfig = JSON.parse(rawjs, function(k,v){//put your code here to parse function}); // avoid eval()
Logger.log(oConfig.getinfo("?"));

The subject content is undefined

I have a small problem using mailparser. When I'm trying to see the subject content it says that is undefined.
My code looks something like this :
var fs = require("fs");
var socket = fs.createWriteStream('./outputmail.eml');
socket.on("end",function()
{
connection.loginfo("end------->");
});
connection.transaction.message_stream.pipe(socket, {}); // haraka related
var mailparser = new MailParser();
mailparser.on("end", function(mail_object){
connection.loginfo("----------SUBJECT-------------------->:", mail_object.subject);
});
fs.createReadStream("./outputmail.eml").pipe(mailparser);
I'm using mailparser in Haraka (http://haraka.github.io) . The whole point doing this, is that I want to parse every mail I receive and add a banner/image/whatever into the mail's body. If you have any idea why this message appears , please let me know.
If that is the code it is likely that you start reading the file before it is written. As the libraries both support streams you should be able to pipe the message_stream directly to mailparser, ie.
var mailparser = new MailParser();
mailparser.on("end", function(mail_object) {
console.log(mail_object);
});
connection.transaction.message_stream.pipe(mailparser, {});
Another reason could naturally be that there just isn't a subject in the email.

jquery.get(url) synchronization

OS X 10.6.8, Chrome 15.0.874.121
I'm experiencing an issue with javascript/jquery: I want to download a header file from base url, then add some more text to it and than spit it out to the client. I'm using the following code:
var bb = new window.WebKitBlobBuilder;
$.get('js/header.txt', function(data) {
bb.append(data);
console.log("finished reading file");
});
console.log("just before getting the blog");
var blob = bb.getBlob('text/plain');
// append some more
saveAs(blob,"name.dxf");
But that fails because getting the file is only finished way after the saveAs(blob) is executed. I know I can fix it with:
var bb = new window.WebKitBlobBuilder;
$.get('js/header.txt', function(data) {
bb.append(data);
//append some more
var blob = bb.getBlob('text/plain');
saveAs(blob,"name.dxf");
});
But that does not really look attractive: I only want to use the get statement only to append the header to the blob, and if I want to read a footer from the file system, I have to do a get inside a get, and spit out the blob in the inner get
Are there alternative ways to withhold the code after the get statement from executing until the whole file has been successfully loaded?
No.*
But, if you want it to look more attractive, try to describe semantically what you are trying to achieve and then write functions accordingly. Maybe:
function loadBlob (loadHeader, loadBody) {
loadHeader(loadBody);
}
loadBlob(function (oncomplete) {
$.get("js/header.txt", function(data) {
bb.append(data);
oncomplete();
});
}, function () {
var blob = bb.getBlob('text/plain');
// append some more
saveAs(blob,"name.dxf");
});
I don't know, is that more attractive? Personally, I find the original just fine, so maybe mine isn't any better, but the point is to use sematics.
* You could use setTimeout to poll and see if the response has been received. That's technically an alternative, but certainly not more attractive, is it?

debug help with rare unexpected output in js

I have the following javascript on my page that is supposed to generate and go to a url instead of posting a form:
var tokenList = ["auto", "usate"];
var dirList = [];
function makeUrl(prov, manuf, model, price){
if (_addToken(prov)){
_joinTokens();
}
if (_addToken(manuf)){
_addToken(model);
_joinTokens();
}
if (price){
return _joinDirs() + "?prezzo=" + price;
}
return _joinDirs();
}
function _addToken(tok){
if (tok){
tokenList.push(tok.replace(/ /g,"_"));
return true;
}
return false;
}
function _joinTokens(){
dirList.push(tokenList.join('-'));
tokenList = [];
}
function _joinDirs(){
if (tokenList){
_joinTokens();
}
var url = '/' + dirList.join('/');
if (url.charAt(url.length-1) == '/'){
url = url.slice(0, -1);
}
return url;
}
It's triggered by this code:
$(document).ready(function(){
$('#navForm').submit(function() {
var prov = $("[name=select-provincia]").val();
var manuf = $("[name=select-marca]").val();
var model = $("[name=select-modello]").val();
var price = $("[name=select-prezzo]").val();
var url = makeUrl(prov, manuf, model, price);
window.location = url;
return false;
});
});
It's been a long while since I translated this code from its original python. I've been getting rare errors in my server logs occasionally that show users trying to visit strange urls that look almost like two urls concatenated. I haven't been able to ever duplicate such an error, but my best guess is that there is something going on with my javascript. The last two times I got this error I noticed that the user was using firefox 3.6 and iphone. Could this be some kind of browser incompatibility? Is there anything wrong with my javascript at all? Is the error just in userland?
For reference here is an example wrong url:
/auto-usate-pesaro_e_urbino/fiat-500//rimini/fiat-500?prezzo=13000
and two possible correct ones:
/auto-usate-pesaro_e_urbino/fiat-500?prezzo=13000
/auto-usate-rimini/fiat-500?prezzo=13000
Any unrelated suggestions for optimizing the code are welcome since I am bad at this.
Not sure it that's the case, but I think those strange URLs might be a result of appending the generated URL to the URL of the page being viewed. You are generating just the pathname part of the URL, not including the protocol and host name (http://foo.com) -- it's possible that some browsers are interpreting this path as relative to the current one. Try prepending the URL with the protocol and hostname.
You might also want to see this answer: Setting JavaScript window.location and follow the advice to write the URL to window.location.href.

Categories

Resources