window.location search path doesn't work - search

The following search function navigates to :
http://webbsite.com/?search=query
instead of
http://webbsite.com/search?q=query
this._search = function (evt) {
var searchValue = this.select('searchTextSelector').val();
if (searchValue !== '' && evt.which === 13 /*enter*/) {
var query = this._getValFromEventObject(evt);
window.location.href = '/search?q=' + query;
}
};
How do I change the window.location path ?
Ok sorry, I solved it by adding e.preventDefault();

fixed by adding e.preventDefault();

Related

node js find problem with special characters

i build a simple search form with onchange event but when i put in search input something like / or ' or alert( it returning my error and this is my backend code:
const searchAds = async (req, res, next) => {
const search = req.params.search;
let ads;
try {
ads = await Ad.find({ "title": { $regex: '.*' + search,$options:'i' + '.*' }});
} catch (err) {
const error = new HttpError(
'Searching ads failed, please try again later.',
500
);
return next(error);
}
const page = parseInt(req.query.page) || 1;
const pageSize = 20;
const pager = paginate(ads.length, page, pageSize);
const pageOfItems = ads.map(ad => ad.toObject({ getters: true })).slice(pager.startIndex, pager.endIndex + 1);
return res.json({ pager, pageOfItems });
};
whats the best way to avoid this? Also is this correct way for find method?
Have you tried wrapping your search variable in encodeURI() or encodeURIComponent()?
https://www.w3schools.com/jsref/jsref_escape.asp
You can use a library such as https://www.npmjs.com/package/regex-escape to escape your search parameter.
var escape = require("regex-escape");
...
ads = await Ad.find({ "title": { $regex: '.*' + escape(search), $options:'i' + '.*' }});

How to get all fonts used on a page using node.js?

I need to crawl all the pages on a site (the crwling part works fine.) and so i need to run THIS script on my server using node.js. I tried implementing the following logic:
var request = require('request');
var cheerio = require('cheerio');
var URL = require('url-parse');
var jsdom = require("jsdom");
var { JSDOM } = jsdom;
var START_URL = "http://balneol.com/";
var SEARCH_FONT = "helvetica";
var MAX_PAGES_TO_VISIT = 100000;
var pagesVisited = {};
var numPagesVisited = 0;
var pagesToVisit = [];
var url = new URL(START_URL);
var baseUrl = url.protocol + "//" + url.hostname;
pagesToVisit.push(START_URL);
crawl();
function crawl() {
if(numPagesVisited >= MAX_PAGES_TO_VISIT) {
console.log("Reached max limit of number of pages to visit.");
return;
}
var nextPage = pagesToVisit.pop();
if (nextPage in pagesVisited) {
// We've already visited this page, so repeat the crawl
crawl();
} else {
// New page we haven't visited
visitPage(nextPage, crawl);
}
}
function visitPage(url, callback) {
// Add page to our set
pagesVisited[url] = true;
numPagesVisited++;
// Make the request
console.log("Visiting page " + url);
request(url, function(error, response, body) {
// Check status code (200 is HTTP OK)
console.log("Status code: " + response.statusCode);
if(response.statusCode !== 200) {
callback();
return;
}
// Parse the window.document body
// var window = jsdom.jsdom(body).defaultView();
var { window } = new JSDOM(body);
//var $ = cheerio.load(body);
var helveticaFound = searchForHelvetica(window, 'font-family');
if(helveticaFound) {
console.log('Word ' + SEARCH_FONT + ' found at page ' + url);
} else {
collectInternalLinks($);
// In this short program, our callback is just calling crawl()
// callback();
}
});
}
function searchForHelvetica( window , css) {
if(typeof getComputedStyle == "undefined")
getComputedStyle= function(elem){
return elem.currentStyle;
}
var who, hoo, values= [], val,
nodes= window.document.body.getElementsByTagName('*'),
L= nodes.length;
for(var i= 0; i<L; i++){
who= nodes[i];
console.log(nodes[i]);
if(who.style){
hoo= '#'+(who.id || who.nodeName+'('+i+')');
console.log(who.style._values);
// return false;
val= who.style.fontFamily || getComputedStyle(who, '')[css];
if(val){
if(verbose) values.push([hoo, val]);
else if(values.indexOf(val)== -1) values.push(val);
// before IE9 you need to shim Array.indexOf (shown below)
}
}
}
// console.log(values);
// return values;
}
function collectInternalLinks($) {
var relativeLinks = $("a[href^='/']");
console.log("Found " + relativeLinks.length + " relative links on page");
relativeLinks.each(function() {
pagesToVisit.push(baseUrl + $(this).attr('href'));
});
}
If you see my visit page function you will see the below two lines of code:
var { window } = new JSDOM(body);
var helveticaFound = searchForHelvetica(window, 'font-family');
as you can see on the 2nd line i am passing the window object to the searchForHelvetic function.
In my searchForHelvetic function , if i console.log(nodes[i]); , i don't get the html element and hence the rest of the script does't quite run as expected. does the jsdom window differ from the window object in the browser ? how do i get the script working ? I.E. basically use the window object to run through all the pages on the page and spit out all the fonts used on the page ?
EDIT::-
To break the problem down to a micro level, if i console.log(who); inside searchForHelvetica function , i get the following result:
HTMLElement {}
HTMLDivElement {}
HTMLDivElement {}
HTMLDivElement {}
HTMLAnchorElement {}
HTMLImageElement {}
HTMLDivElement {}
HTMLFormElement {}
HTMLDivElement {}
HTMLLabelElement {}
HTMLInputElement {}
HTMLButtonElement {}
HTMLButtonElement {}
HTMLSpanElement {}
etc..
But if i were to do the same in a web browser the result world be different Eg.
nodes = window.document.body.getElementsByTagName('*');
console.log(node[1]) // <div id="mobile-menu-box" class="hide">...</div>
How do i get a similar result in node.js ?

Scraping website using node js using auth

var getMatches = function(){
var sait = 'https://www.website.com'
request({ url: sait, jar: true}, function(error, response, html){
if(!error){
var $ = cheerio.load(html)
var count = ($('.TheMatch').length)
for(var i = 2; i< count + 2 ; i++){
var live = ($('.TheMatch:nth-child('+i+') .LiveStatus').text())
var nameMatch = ($('.TheMatch:nth-child('+i+') .MDxEventName').text())
var time = ($('.TheMatch:nth-child('+i+') .DateTime').text().substring(8))
var websiteCount = ($('.TheMatch:nth-child('+i+') .TotalBookies').text())
if((websiteCount >= 25) && (live.length === 0) ){
console.log('match ' + nameMatch)
console.log('count Websites ' + websiteCount)
}
}}})}
i want to make auth on this website and save the cookie how can i do it ? and save the cookie so everytime i parse dont gonna need to log in ?
Well, I did similar task. But problem is that it is always site specific. Anyway, the way to do it is to use request library to make a post request to website's auth endpoint. Then response object will contain appropriate cookies like sessionID or something. Then you save this cookie and do a new request to page you wanted from the beginning. Here is documentation about cookies in request module: https://github.com/request/request#requestcookie. That worked for me fine.
How to get:
request.post({ url: "website/signin",
form: {
login: 'login',
password: "password",
remember: 1
}
}, function(err, httpResponse, body) {
var cooka = false;
var cookies = httpResponse.headers['set-cookie'];
if (typeof cookies != "undefined") cooka = cookies[0];
if (typeof cooka != "undefined" && cooka != false) {
self.sessionId = cooka.substring(10, cooka.indexOf(";"));
} else {
return self.emit("error", "Can't get session id");
}
});
How to set:
var options = { uri: "desired url" };
var j = request.jar();
if (worker.source == "coquette") {
var cookie = request.cookie('PHPSESSID=' + worker.sessionId);
j.setCookie(cookie, url);
options.jar = j;
}
request.get(options, function(error, response, body) {
if (error || response.statusCode != 200) {
} else {
// work with body body
}
});

Search for var in external page

I want to make a script that search for a variable in a external page.
For example, i want the script to visit this page: Here,
Check whether a server is available and notify me somehow.
Can someone help me with this?
My solution is to recuperate the text of the source of a page. Then search for any strings that we want in that source. Just add another search call to extend the search. Works in Firefox.
<script>
function print(text) {
alert(text);
}
function search(where, what) {
var position = where.indexOf(what)
if ( position !== -1) {
print("Found \"" + what + "\" at " + position);
return;
} //else
print("\"" + what + "\" not found");
}
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://billing.dacentec.com/hostbill/index.php?/cart/dedicated-servers/", true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var externalText = xhr.responseText;
search(externalText, "technology");
search(externalText, "secure");
search(externalText, "we");
} else {
print(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
print(xhr.statusText);
};
xhr.send(null);
</script>

Node.js download multiple files

I need to download multiple files from urls. I have got list of them in the file. How should I do that? I already made it, but it's not working. I need to wain until last download is done before starting next wan. How can I do that?
You want to call the download function from the callback of the file before that. I threw together something, do not consider it pretty nor production ready, please ;-)
var http = require('http-get');
var files = { 'url' : 'local-location', 'repeat-this' : 'as often as you want' };
var MultiLoader = function (files, finalcb) {
var load_next_file = function (files) {
if (Object.keys(files) == 0) {
finalcb(null);
return;
}
var nexturl = Object.keys(files)[0];
var nextfnname = files[nexturl];
console.log('will load ' + nexturl);
http.get(nexturl, nextfnname, function (err, result) {
console.log('loaded ' + nexturl);
delete files[nexturl];
load_next_file(files);
});
};
load_next_file(JSON.parse(JSON.stringify(files)));
};
MultiLoader(files, function () { console.log('finalcb'); });
http-get is not a standard node module, you can install it via npm install http-get.
I think this is what you're looking for.
const fs = require('fs')
const https = require('https')
const downloadFolderPath = 'downloads'
const urls = [
'url 1',
'url 2'
]
const downloadFile = url => {
return new Promise((resolve, reject) => {
const splitUrl = url.split('/')
const filename = splitUrl[splitUrl.length - 1]
const outputPath = `${downloadFolderPath}/${filename}`
const file = fs.createWriteStream(outputPath)
https.get(url, res => {
if (res.statusCode === 200) {
res.pipe(file).on('close', resolve)
} else {
reject(res.statusCode)
}
})
})
}
if (!fs.existsSync(downloadFolderPath)) {
fs.mkdirSync(downloadFolderPath)
}
let downloadedFiles = 0
urls.forEach(async url => {
await downloadFile(url)
downloadedFiles++
console.log(`${downloadedFiles}/${urls.length} downloaded`)
})
You can read files using fs (var fs = require('fs');)in node js
fs.readFile('<filepath>', "utf8", function (err, data) {
if (err) throw err;
console.log(data);
});

Resources