Simple console menu - node.js

I'm a beginner in node and I'm trying to build a simple console menu. I kept searching for this but coulnd't find a proper answer.
When I run the script I want to display a menu and ask the user to enter an option. After he chooses, I perform an action then display the menu again.
I tried using a while loop but it's blocking my program.
Here's an example of what I'm trying to achieve:
int userRes = -1;
while(userRes != 0){
console.log("Option 1")
console.log("Option 2")
console.log("Option 3")
userRes = readLineSync.question("Pick an option");
if(userRes == 1){
doSomething();
}else if (userRes == 2){
doSomethingElse();
}
}
EDIT: Actual code bellow. As you can see I use stomp. The while loop displays my menu and the action inside the if statement gets executed.
The problem is, when stomp sends back the response my code in the subscribe function is not being executed.
I tried without a while (just the action) and it works perfectly.
var Stomp = require("stomp-client");
const readlineSync = require("readline-sync");
var client = new Stomp(host, 61613);
function conn(res,req){
client.connect(function(sessionId) {
client.subscribe("/queue/" + res, function(body, headers) {
console.log(body);
});
var res = -1;
while (res != 0) {
displayMenu();
var res = readlineSync.question("Introduceti o optiune: ");
if (res == 1) {
client.publish("/queue/" + req, "test");
} else if (res == 0) {
process.exit();
}
}
});
}
function displayMenu() {
console.log(
"Option one\n Option two\n 0 for exit";
);
}

You can use this code
const readLineSync = require('readline-sync')
let userRes;
while (userRes !== '0') {
console.log("Option 1")
console.log("Option 2")
console.log("Option 3")
userRes = readLineSync.question("Pick an option");
if (userRes === '1') {
doSomething()
} else if (userRes === '2') {
doSomethingElse()
}
}

Related

Chrome.tabs.sendMessage is not sending a message to content script

I have looked at a lot of different posts about using execute script and send message to execute the content script on a specified tab, but it doesn't execute the content script until I do a hard refresh and then the response from the content script is successful. Attached below is the call back function for button clicked this is in a popup.js file.
function buttonClicked() {
// Get an object for the active tab
console.log("print dymo has been clicked");
chrome.tabs.query({active: true, currentWindow: true}, function(tab_array){
// send a messege to the contentscript that will then scrape the web
// it will then call the popup.js receiveURL() method with the url it makes
console.log("Messege sent to conntent script");
alert("print has been clicked")
alert(tab_array[0].id)
chrome.tabs.sendMessage(tab_array[0].id, {getHTML: true}, receiveURL);
});
Content Script:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
// When there is a request (that will come in as true)
if (request) {
/*********************************************************************
IF YOU NEED TO CHANGE THE WEB SCRAPER START HERE
**********************************************************************/
// Take out everything in the html before the tag "<label>Job:</label>"
// Selects only the inner elements between the style row
var matches = document.querySelectorAll('ul.list-unstyled')
//Gives us the inner text of the elements of information
//Gives us an array of all the information split up by new lines
information = matches[1].innerText.split(/\r?\n/)
//Iterate ansd store everything in a dictionary split by a :
var dict_info = {}
for (index = 0; index < information.length; index++) {
parts = information[index].split(": ")
ans = ""
for(i = 1; i < parts.length; i++) {
ans += parts[i]
}
dict_info[parts[0]] = ans
}
var name = dict_info['Requestor']
var job = dict_info['Job']
if (job != undefined) {
job = job.match(JOB_REGEX)[1]
}
var request = dict_info['Request']
if (request != undefined) {
request = request.match(REQ_REGEX)[1]
}
var file = dict_info["File"]
if (file.length > 10) {
file = file.substring(0,file.length-9)
}
if (file.length > 20) {
file = file.substring(0, 20)
}
var email = dict_info['Requestor Email']
var cost = dict_info['Estimated Cost']
if(cost == undefined) {
cost = dict_info['Cost']
}
if (cost != undefined) {
cost = cost.match(COST_REGEX)[1]
}
name = name.split(" ")
name = name[0] + "/" + name[name.length-1]
var url = "https:// test"
sendResponse(url);
return true;
}
}
);

Run an event only once in NodeJS but use the data multiple times

So I have written this code :
manager.getInventoryContents(730, 2, true, (err,inventory,currencies) => {
if (err){
console.log(err)
} else {
console.log("Create order for : " + orderitemname.length + " items.")
var otn = orderitemname.length -1;
while (otn !== -1) {
var li= inventory.length - 1;
while (li !== -1){
if (inventory[li].market_name === orderitemname[otn]){
console.log("Add item to trade " + orderitemname[otn]);
li = li -1;
otn = otn -1;
} else {
console.log("ERR !!! ITEM NOT IN INVENTORY !!! " + orderitemname[otn]);
//Change Order Status To Failed !!
n = n-1;
otn = -1;
li = -1;
}
}
}
}
})
So what is happening is that the process is calling this event multiple times (since it is in a while loop), and the node module is ignoreing it saying :
Error: The request is a duplicate and the action has already occurred in the past, ignored this time
So is there a way I can just call the event once, save it to a variable or something and then use the data multiple time ?
You can wrap everything in a new context and initiate whatever variable you need there. Then, the variable will be accessible from within your callback function.
Something like this:
(() => {
let ctx_data = 0;
manager.getInventoryContents(730, 2, true, (err,inventory,currencies) => {
console.log(++ctx_data);
});
})();

How can I get female voice by Web Speech API in Google Chrome

In a webpage, I want a female voice to speak my texts. I tried to do this by following code. But still now male voice is talking. How can I arrange a female voice to talk my texts? Can anybody share me a correct code that works in Google Chrome.
var voices = speechSynthesis.getVoices();
var msg = new SpeechSynthesisUtterance("Hello World!");
msg.default=false;
msg.localservice=true;
msg.lang = "en-GB";
msg.voice = voices[3].name;
speechSynthesis.speak(msg);
The below code worked for me. I hope it works for you.
var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.voice = voices[3];
msg.text = "Hello World";
speechSynthesis.speak(msg);
On the 1st try it might give out male's voice. But on the 2nd attempt (without refreshing) it will give out the female's voice and try deploying it on a dummy server, there it will work like a charm in the first go.
This happens to me that the voice doesn't change at the first time the page is loaded.
I found a solution that works for me.
getVoices() should be triggered when the document is ready.
So I add at the top of my js like this.
$(document).ready(function() {
var voices = window.speechSynthesis.getVoices();
})
On Chrome, I do something like this:
<html>
<head>
<script>
function speak(language, country, preferredNames, text) {
var resolvePromise;
var promise = new Promise(r => resolvePromise = r);
var voices = window.speechSynthesis.getVoices();
if (voices.length == 0) {
new Promise(r => {
window.speechSynthesis.onvoiceschanged = () => {
window.speechSynthesis.onvoiceschanged = null;
r();
};
})
.then(() => speak(language, country, preferredNames, text))
.then(resolvePromise);
} else {
var msg = new SpeechSynthesisUtterance(text);
voices.sort((a, b) => {
if (language != null) {
var matchA = a.lang.indexOf(language + "-") == 0;
var matchB = b.lang.indexOf(language + "-") == 0;
if (! matchA && ! matchB) return 0;
if (! matchA && matchB) return 1;
if (! matchB && matchA) return -1;
}
if (country != null) {
var matchA = a.lang.indexOf("-" + country) == a.lang.length - ("-" + country).length;
var matchB = b.lang.indexOf("-" + country) == b.lang.length - ("-" + country).length;
if (! matchA && ! matchB) return 0;
if (! matchA && matchB) return 1;
if (! matchB && matchA) return -1;
}
if (preferredNames != null) {
var indexA = voices.length;
var indexB = voices.length;
preferredNames.forEach((e, i) => {
if (indexA == voices.length && a.name.match(e) != null) indexA = i;
if (indexB == voices.length && b.name.match(e) != null) indexB = i;
});
return indexA - indexB;
}
return 0;
});
if (voices.length > 0) msg.voice = voices[0];
if (language != null) {
msg.lang = language;
if (country != null) msg.lang += "-" + country;
}
msg.onend = resolvePromise;
window.speechSynthesis.speak(msg);
// msg.onend not triggered without call to console.log(msg)?
console.log(msg);
}
return promise;
}
speak("en", null, [ /Google US English/, /Samantha/, /Fiona/, /Victoria/, /female/i ], "Hello, world.")
.then(() => speak("en", null, [ /female/i ], "Hello, world."))
.then(() => speak("en", "US", [ /female/i ], "Hello, world."))
.then(() => speak("en", "US", null, "Hello, world."))
.then(() => speak("en", "GB", [ /\Wmale/i ], "Hello, world."));
</script>
</head>
<body>
</body>
</html>
This code worked for me.
var speakObj = new SpeechSynthesisUtterance();
speakObj.text = text;
speakObj.voice = speechSynthesis.getVoices().filter(function(voice) {
return voice.name == "Google UK English Female"
})[0];
window.speechSynthesis.speak(speakObj);
After a long time of troubleshooting, I could figure out the solution.
4 persons already suggested me some solutions. But these did not work for me. But helped me to figure out the solution. Thanks to all of them.
To solve the problem, I did two things.
I had to load voices during onvoiceschanged event as follows:
var voices;
window.speechSynthesis.onvoiceschanged = function() {
voices=window.speechSynthesis.getVoices();
};
I found this tip from this link.
'Google UK English Female' does not work for me. So I used 'Microsoft Zira Desktop - English (United States)' as follows:
speech.voice = voices.filter(function(voice) { return voice.name == 'Microsoft Zira Desktop - English (United States)'; })[0];

CSJS addCallback always runs SSJS

Can someone please shed some light on why this CSJS code is always running my SSJS code regardless of the CSJS seeming to return true or false?
CSJS code
message = "Starting Settle ATM Process..."
$(".infoMessage").text(message);
atmID = $("input.atmID").val(); //pull from hidden input
var deferred = atmRPC.closeATMFirstSettle();
deferred.addCallback(function(result){
if (result == "false") {
alert("Close command failed during 1st Settle ATM process. System will retry...");
var insideDeferred = atmMRPC.closeATMSecondSettle();
insideDeferred.addCallback(function(result) {
if (result == "false") {
message = "Close command failed during Settle ATM process. Please try again later."
$(".infoMessage").text(message);
atmRPC.updateInfoMsg(message);
return false;
} else if(result == "true"){
var deferred = atmRPC.settleATMFirst();
deferred.addCallback(function(result){
if (result == "false") {
alert("Settlement failed during 1st attempt. System will retry...");
var insideDeferred = atmMRPC.settleATMSecond();
insideDeferred.addCallback(function(result) {
if (result == "false") {
message = "Settlement failed. Please try again later."
$(".infoMessage").text(message);
atmRPC.updateInfoMsg(message);
return false;
} else if(result == "true"){
message = atmID + " has been successfully Settled."
$(".infoMessage").text(message);
atmRPC.updateInfoMsg(message);
// return true;
}
})
} else if(result == "true"){
message = atmID + " has been successfully Settled."
$(".infoMessage").text(message);
atmRPC.updateInfoMsg(message);
// return true;
}
})
}
})
} else if(result == "true"){
var deferred = atmRPC.settleATMFirst();
deferred.addCallback(function(result){
if (result == "false") {
alert("Settlement failed during 1st attempt. System will retry...");
var insideDeferred = atmMRPC.settleATMSecond();
insideDeferred.addCallback(function(result) {
if (result == "false") {
alert("Settlement failed during 1st attempt. System will retry...");
message = "Settlement failed. Please try again later."
$(".infoMessage").text(message);
atmRPC.updateInfoMsg(message);
return false;
} else if(result == "true"){
message = atmID + " has been successfully Settled."
$(".infoMessage").text(message);
atmRPC.updateInfoMsg(message);
// return true;
}
})
} else if(result == "true"){
message = atmID + " has been successfully Settled."
$(".infoMessage").text(message);
atmRPC.updateInfoMsg(message);
// return true;
}
})
}
})
closeATMFirsSettle and closeATMSecondSettle are identical with exception of the boolean variable
if(atmBean.atmStatus == "OPEN" || atmBean.atmStatus == "WOUNDED") {
var firstTry:boolean = atmBean.closeATM(atmBean.atmID, userBean.userID);
return firstTry.toString();
} else if(atmBean.atmStatus == "CLOSED") {
return "true";
} else {
return "false";
}
settleATMFirst and settleATMSecond are identical with exception of the boolean variable
sessionScope.infoMsg = null;
try {
if(atmBean.atmAmountReceived == null) {
var settleAmt:Integer = Integer.parseInt(atmBean.atmTodaySettlementAmt);
} else {
var settleAmt:Integer = Integer.parseInt(atmBean.atmAmountReceived);
}
} catch(e) {
message:String = "1st Settle ATM Amount Error: " + e;
logInfo(message);
return sessionScope.infoMsg = message;
}
var firstTry:boolean = atmBean.settleATM(atmBean.atmID, userBean.userID, settleAmt);
return firstTry.toString();
atmBean.settleATM
public boolean settleATM(String atmId, String employeeId, Integer settlementAmount, String attempt){
//This method called by RPC control from client JS
AtmOperationSettlementResult result = null;
boolean returnValue = false;
try {
result = AtmTerminalUtil.settleAtm(atmID, employeeId, settlementAmount);
returnValue = result.getSuccess();
if(attempt.equals("Second")) {
System.out.println("ATM Bean - Second Attempt");
if(!returnValue) {
handleATMError(result);
}
}
} catch(Exception e) {
System.out.println(e);
}
//saveATMHistory();
return returnValue; //return status of operation to UI
}
I know the SSJS in the Server tab is always being ran because the log entries show the value of a viewScope that is set if the atmBean.settleATM returns a false.
Sorry, didn't realize until the last comment that everyone may be thinking the SSJS code I'm talking about is in the RPC, but I'm referring to the Server tab.

Opening tabs in Chrome news reader extension

I'm trying to create a simple chrome extension using the following google RSS reader sample,
http://code.google.com/chrome/extensions/samples.html#597015d3bcce3da693b02314afd607bec4f55291
I can add links in the pop-up window that open tabs, but not from the feeds themselves.
Looping through the items in the feed, grabbing title tags and link tags, I want the title to link the the appropriate sites
var entries = doc.getElementsByTagName('item');
var count = Math.min(entries.length, maxFeedItems);
for (var i = 0; i < count; i++) {
item = entries.item(i);
// Grab the title for the feed item.
var itemTitle = item.getElementsByTagName('title')[0];
if (itemTitle) {
itemTitle = itemTitle.textContent;
} else {
itemTitle = "Unknown title";
}
// Grab the link for this feed item
var itemLink = item.getElementsByTagName('link')[0];
if (itemLink) {
itemLink = itemLink.textContent;
} else {
itemLink = "Unknown link";
}
var title = document.createElement("a");
title.className = "item_title";
title.innerText = itemTitle; //display title in iframe
title.addEventListener("click", titleLink); // should open link when clicking on title, but does not.
}
// -------------------------------------------------------------------
// Show |url| in a new tab.
function showUrl(url) {
// Only allow http and https URLs.
if (url.indexOf("http:") != 0 && url.indexOf("https:") != 0) {
return;
}
chrome.tabs.create({url: url});
}
function moreStories(event) {
showUrl(moreStoriesUrl);
}
function titleLink(event) {
showUrl(itemLink);
}
Any thoughts on why this is not working.
If I replace title.addEventListener("click", titleLink); with title.addEventListener("click", moreStories); each title will link to moreStories, I cannot get each title to link to itemLink.
Thanks
Its a bit hard to answer your question without the whole code, but Ill give it a shot ;)
First up, titleLink() isnt going to work because itemLink isnt known. When you create title (the link) you should of attached it to that...say title.href=itemLink then in tiltleLinks you could access that href with showUrl(event.currentTarget.href)
Also did you fix the error in that example?...if not then change frameLoaded to....
function frameLoaded() {
var links = document.getElementsByTagName("A");
for (i = 0; i < links.length; i++) {
var clssName = links[i].className;
if (clssName != "item_title" && clssName != "open_box") {
links[i].addEventListener("click", showStory);
}
}
window.addEventListener("message", messageHandler);
}
If you still have probs could you attach the whole code so I can see what your doing and Ill give you a hand.
Thank you very much for your help.
code title.href=itemLink and code showUrl(event.currentTarget.href) was exactly what I needed.
For completeness, here is the full code,
<script id="iframe_script">
function reportHeight() {
var msg = JSON.stringify({type:"size", size:document.body.offsetHeight});
parent.postMessage(msg, "*");
}
function frameLoaded() {
var links = document.getElementsByTagName("A");
for (i = 0; i < links.length; i++) {
var class = links[i].className;
if (class != "item_title" && class != "open_box") {
links[i].addEventListener("click", showStory);
}
}
window.addEventListener("message", messageHandler);
}
function showStory(event) {
var href = event.currentTarget.href;
parent.postMessage(JSON.stringify({type:"show", url:href}), "*");
event.preventDefault();
}
function messageHandler(event) {
reportHeight();
}
</script>
<script>
// Feed URL.
var feedUrl = 'http://localhost/newsfeed.xml';
// The XMLHttpRequest object that tries to load and parse the feed.
var req;
function main() {
req = new XMLHttpRequest();
req.onload = handleResponse;
req.onerror = handleError;
req.open("GET", feedUrl, true);
req.send(null);
}
// Handles feed parsing errors.
function handleFeedParsingFailed(error) {
var feed = document.getElementById("feed");
feed.className = "error";
feed.innerText = "Error: " + error;
}
// Handles errors during the XMLHttpRequest.
function handleError() {
handleFeedParsingFailed('Failed to fetch RSS feed.');
}
// Handles parsing the feed data we got back from XMLHttpRequest.
function handleResponse() {
var doc = req.responseXML;
if (!doc) {
handleFeedParsingFailed("Not a valid feed.");
return;
}
buildPreview(doc);
}
// The maximum number of feed items to show in the preview.
var maxFeedItems = 10;
// Where the more stories link should navigate to.
var moreStoriesUrl;
function buildPreview(doc) {
// Get the link to the feed source.
var link = doc.getElementsByTagName("link");
var parentTag = link[0].parentNode.tagName;
if (parentTag != "item" && parentTag != "entry") {
moreStoriesUrl = link[0].textContent;
}
// Setup the title image.
var images = doc.getElementsByTagName("image");
var titleImg;
if (images.length != 0) {
var urls = images[0].getElementsByTagName("url");
if (urls.length != 0) {
titleImg = urls[0].textContent;
}
}
var img = document.getElementById("title");
// Listen for mouse and key events
if (titleImg) {
img.src = titleImg;
if (moreStoriesUrl) {
document.getElementById("title_a").addEventListener("click",moreStories);
document.getElementById("title_a").addEventListener("keydown",
function(event) {
if (event.keyCode == 13) {
moreStories(event);
}});
}
} else {
img.style.display = "none";
}
// Construct the iframe's HTML.
var iframe_src = "<!doctype html><html><head><script>" +
document.getElementById("iframe_script").textContent + "<" +
"/script></head><body onload='frameLoaded();' " +
"style='padding:0px;margin:0px;'>";
var feed = document.getElementById("feed");
// Set ARIA role indicating the feed element has a tree structure
feed.setAttribute("role", "tree");
var entries = doc.getElementsByTagName('item');
var count = Math.min(entries.length, maxFeedItems);
for (var i = 0; i < count; i++) {
item = entries.item(i);
// Grab the title for the feed item.
var itemTitle = item.getElementsByTagName('title')[0];
if (itemTitle) {
itemTitle = itemTitle.textContent;
} else {
itemTitle = "Unknown title";
}
// Grab the link for the feed item.
var itemLink = item.getElementsByTagName('link')[0];
if (itemLink) {
itemLink = itemLink.textContent;
} else {
itemLink = "Unknown link";
}
var item = document.createElement("div");
var title = document.createElement("a");
title.innerText = itemTitle; //display title in iframe
title.href=itemLink;
title.addEventListener("click", titleLink);
item.appendChild(title);
feed.appendChild(item);
}
if (moreStoriesUrl) {
var more = document.createElement("a");
more.className = "more";
more.innerText = "***Site Main Page*** \u00BB";
more.tabIndex = 0;
more.addEventListener("click", moreStories);
more.addEventListener("keydown", function(event) {
if (event.keyCode == 13) {
moreStories(event);
}});
feed.appendChild(more);
}
}
// -------------------------------------------------------------------
// Show |url| in a new tab.
function showUrl(url) {
// Only allow http and https URLs.
if (url.indexOf("http:") != 0 && url.indexOf("https:") != 0) {
return;
}
chrome.tabs.create({url: url});
}
// -------------------------------------------------------------------
function moreStories(event) {
showUrl(moreStoriesUrl);
}
function titleLink(event) {
showUrl(event.currentTarget.href);
}
function keyHandlerShowDesc(event) {
// Display content under heading when spacebar or right-arrow pressed
// Hide content when spacebar pressed again or left-arrow pressed
// Move to next heading when down-arrow pressed
// Move to previous heading when up-arrow pressed
if (event.keyCode == 32) {
showDesc(event);
} else if ((this.parentNode.className == "item opened") &&
(event.keyCode == 37)) {
showDesc(event);
} else if ((this.parentNode.className == "item") && (event.keyCode == 39)) {
showDesc(event);
} else if (event.keyCode == 40) {
if (this.parentNode.nextSibling) {
this.parentNode.nextSibling.children[1].focus();
}
} else if (event.keyCode == 38) {
if (this.parentNode.previousSibling) {
this.parentNode.previousSibling.children[1].focus();
}
}
}
function showDesc(event) {
var item = event.currentTarget.parentNode;
var items = document.getElementsByClassName("item");
for (var i = 0; i < items.length; i++) {
var iframe = items[i].getElementsByClassName("item_desc")[0];
if (items[i] == item && items[i].className == "item") {
items[i].className = "item opened";
iframe.contentWindow.postMessage("reportHeight", "*");
// Set the ARIA state indicating the tree item is currently expanded.
items[i].getElementsByClassName("item_title")[0].
setAttribute("aria-expanded", "true");
iframe.tabIndex = 0;
} else {
items[i].className = "item";
iframe.style.height = "0px";
// Set the ARIA state indicating the tree item is currently collapsed.
items[i].getElementsByClassName("item_title")[0].
setAttribute("aria-expanded", "false");
iframe.tabIndex = -1;
}
}
}
function iframeMessageHandler(e) {
// Only listen to messages from one of our own iframes.
var iframes = document.getElementsByTagName("IFRAME");
for (var i = 0; i < iframes.length; i++) {
if (iframes[i].contentWindow == e.source) {
var msg = JSON.parse(e.data);
if (msg) {
if (msg.type == "size") {
iframes[i].style.height = msg.size + "px";
}
else if (msg.type == "show") {
var url = msg.url;
if (url.indexOf("http://localhost/index.html") == 0) {
// If the URL is a redirect URL, strip of the destination and go to
// that directly. This is necessary because the Google news
// redirector blocks use of the redirects in this case.
var index = url.indexOf("&url=");
if (index >= 0) {
url = url.substring(index + 5);
index = url.indexOf("&");
if (index >= 0)
url = url.substring(0, index);
}
}
showUrl(url);
}
}
return;
}
}
}
window.addEventListener("message", iframeMessageHandler);
</script>
Thanks again for the help.
-Mike

Resources