Website Interference w/ Multiple Instances Running (JavaScript) - javascript

I am relatively new to JavaScript but I am creating a simple hangman web app in local host. The game runs perfectly, just as I want it. However, I realized that after loading the game at the same time in different tabs, the words change in all instances to the same word. For example, I can run the game in one tab, then another. Then when I go back to the first tab, the word I am trying to guess has changed to the one in the second tab. I don't know how else to describe my issue. Here's my JS and backend code:
JS
function runHangmanInit(){
$.get("/hangmanInit", {}, function(response){ //generates word
term.write("RUNNING HANGMAN.cpp\r\n");
guessedLetters = [];
let json = JSON.parse(response);
console.log(json);
term.write(json["state"] + "\r\n");
term.write("Category: " + json["category"] + "\t");
for(let i = 0; i < json["dashes"]; i++){
term.write(" _");
}
term.write("\r\nGuesses remiaining: "+ json["guesses"] + "\r\n");
term.write(json["message"]);
});
}
function checkLetterTerminal(letter){
$.get("/hangman/" + letter, {}, function(response){
let x = JSON.parse(response);
//console.log(x);
if(x["err"]){
term.write(x["err"]);
}
else{
term.write(x["state"] + "\r\n");
term.write("Category: " + x["category"] + "\t");
for(let i = 0; i < x.dashes.length; i++){
term.write(x.dashes[i] + " ");
}
term.write("\r\nGuesses remiaining: "+ x["guesses"] + "\r\n");
if(x["lettersGuessed"]){ //Incorrect guess
guessedLetters.push(x["lettersGuessed"]);
}
term.write("Letters Guessed: ");
for(let i = 0; i < guessedLetters.length; i++){
term.write(guessedLetters[i] + " ");
}
term.write("\r\n");
}
if(x["message"]){
term.write(x["message"] + "\r\n");
check1 = 0;
term.write("\r\n\x1B[1;3;31mdyaranon#DEKTOP-123:\x1B[0m/mnt/e$ ");
}
else{
term.write("\r\nEnter your guess: ");
}
});
}
Backend (Crow C++)
int main(int argc, char* argv[]){
crow::SimpleApp app;
Game game = Game(1);
CROW_ROUTE(app, "/hangmanInit")([](const request& req, response& res){
nlohmann::json x;
game.init();
x["word"] = game.getWord();
x["state"] = game.board.hangman;
x["category"] = game.categoryName;
x["dashes"] = game.board.dashes.size();
x["guesses"] = 6;
x["message"] = "Enter your guess: ";
res.sendJSON(x);
});
CROW_ROUTE(app, "/hangman/<string>")([](const request &req, response &res, string letter){
nlohmann::json x;
x = game.playGame(letter);
res.sendJSON(x);
});
I included all the relative parts of the code. Any help would be appreciated.

It looks like you're only creating one game instance? So yes, there's only once instance server-side and you're sharing it among all clients.
HTTP is stateless, so you first need a way to identify which client is which when they make requests. This is normally done by a session ID of some sort, which should be randomly generated.
It's common to store this session ID in cookies, but you'll actually want to just use a local variable in your JavaScript when the page loads, since you want to treat each tab differently.
From there, when you make your HTTP requests (which you're doing with $.get()), you need to include this session ID. I suggest putting it in the URL, something like:
/hangman/<some-game-id-here>/<some-route>
Finally, on the server side, you need to track multiple game instances... one for each game going on, and associate them with the session IDs.

Related

Unable to query postgres db with _ using javascript

I'm trouble shooting this application that I ave had no hand in creating, but I get to fix. Anywho, I'm finding that I am unable to query the postgres db for any variable that has an _. Now before you say use escape characters, I've already been there, done that, and got the t-shirt. From what I could tell, the application uses escape-string-regexp module to alter the string, for example => "some_random#email.com" => "some_random#email\.com" I added my own module to single out the _ and now the output is => "some\\_random#email\.com". Note the two escapes for the underscore as prescribed by the mass forums that I have scaled. I even built a tool to test out in python and that works. Slightly different as I use => "(E'some\\_random#email\\.com')". Tried that with javascript as well and still no dice. What say all ye? Oh, this shoots out via expressjs from what I can tell. I'm not a javascript guy.
Found the solution. I and another team mate went back and revisited the method I created and changed the " to '. Next thing you know, it started working.
exports.stringChanger = function(word){
//console.log(word)
var metas = ["_"];
var guestPool = word.split("");
//console.log(guestPool)
for(i in range(guestPool.length)){
for(j in range(metas.length)){
if(guestPool[i] === metas[j]){
guestPool[i] = "\\" + guestPool[i];
}
}
}
var guest = guestPool.join("");
return guest;
//console.log(guest)
};
to
exports.stringChanger = function(word){
//console.log(word)
var metas = ['_'];
var guestPool = word.split("");
//console.log(guestPool)
for(i in range(guestPool.length)){
for(j in range(metas.length)){
if(guestPool[i] === metas[j]){
guestPool[i] = '\\' + guestPool[i];
}
}
}
var guest = guestPool.join("");
return guest;
//console.log(guest)
};

How to count number of times an item is in an array

I've decided to just try again from the start since I'm a bit more awake now and go over building this step by step. i've looked at some of the answers and there seems to be many ways one could go about this. I'm trying to do this using what I've learned so far. I've learned about variables, basic functions, objects, arrays, 'this' and the push method. I know for, while, do while, for in loops, though the for loop is the one I understand the best.
Here is my new approach to building this, I know it's unnecessarily long but I want to be able to get a basic understanding of how to piece the different things I've learned together in a very simple way. This is more about learning how to go about building simple programs. Then I would proceed in fine-tuning the program to make it more concise and clever. If you could have a look and tell me how I would proceed with what I've got so far...
Here is the code, Ideally I want to run a function when there's a new 'visitor' that asks for their name and number. Then create a new 'customer' object with the given name and number and push it to a 'visitors' array. once I've successfully figured that out I would use loops to check the array if the visitor is new or not, and update their number of visits everytime they come.
//array that will contain 'Customer' objects
var visitors = [john];
//Customer object
function Customer(name, phonenumber){
this.name = name;
this.phonenumber = phonenumber;
//will eventually add a "visits" method logging number of visits
}
var john = new Customer("john smith", "333");
//visitor funtion that runs everytime there is a new visitor
var visitor = function(){
//visitor does not have a set name or number yet
var userNumber = "variable userNumber is currently equal to " + 0;
var userName = "variable userName is currently set to " + undefined;
console.log(userName, userNumber);
//ask for visitor name and number
var askNumber = prompt("type your number");
var askName = prompt("what is your name?");
//store user name and number in two variables
var userNumber = "variable 'userNumber' is now equal to " + askNumber;
var userName = "variable userName is now set to " + askName;
//print out the new variables
console.log(userNumber);
console.log(userName);
//print who the phone number belongs to, this lets me see that the above code worked correctly
var userNumber = askNumber;
var userName = askName;
console.log("Phone number " + userNumber + " belongs to " + userName);
//make new customer object with the given name and number
var userNumber = new Customer();
userNumber.name = askName;
userNumber.phonenumber = askNumber;
console.log("properties of " + userNumber);
};
the last bit returns "properties of [object, object]" why?
Not only did you confuse yourself, but you crashed the browser! :-)
(The for loop never terminates because you push a new value to the list on every iteration.)
I could tell you what's wrong with the code in more detail, but you'll learn a lot more if you chase it down yourself. So what you need now is to learn the art of debugging.
Add a debugger statement at the beginning of your test() function:
var list = ["111", "222", "333", "444", "555", "666"];
var test = function(input){
debugger;
var info = prompt("hi " + input + " what is your name?");
for(i=0; i< list.length; i++){
if( info === list.length[i]){
console.log(list[i]);
}
else{
list.push(info);
}
console.log(list);
}
}
Now run test() and it will stop in the debugger at that statement. Look at the different panels in the debugger - you can view your variables and other stuff. Find the place where it has controls to let you step through your code. Single-step through the code and look at the variables as you go. You will soon discover what the problems are. In most browsers there are also keyboard shortcuts to let you step through the code more easily.
If you use Chrome, here is an introduction to the Chrome DevTools. There are similar tutorials for the other browsers too.
heres an algorithm :
Use an object to store the user no. and the count. like :
{"222":"3","444":"1"}
this means 222 has visited 3 times and 444 visited once. now every time a user checks in:
see if the key with the user's number exists, if yes increment the count. if no, add a new entry with count = 1.
check if the number is 5, if yes get free coffee and reset count to zero. skip if no.
I would solve the problem by storing the visit count in a dictionary that maps each customer's ID to the number of times they have visited. The dictionary is initially empty. When you look up an ID in the dictionary for the first time, you'll get undefined, which tells you that you must initialize the value.
The following demonstration has a couple of buttons that you can use to make Alice or Bob visit the shop. Click on the blue button at the bottom to start the demo.
function message(s) { // Simple output for a code snippet.
document.getElementById('display').innerHTML += s + '<br />';
}
var coffeeCount = {}; // Stores the number of visits by each customer.
function customerVisit(id) { // Called when a customer visits.
if (coffeeCount[id] === undefined) { // Check for first visit.
coffeeCount[id] = 0; // Initialize the visit count.
}
var count = ++coffeeCount[id]; // Increment the visit count.
message(id+': '+count+' visit'+(count == 1 ? '' : 's'));
if (count % 5 == 0) { // Check for free coffee.
message('→ Free coffee for customer '+id+'!');
}
}
<button onclick="customerVisit('Alice')">Alice</button>
<button onclick="customerVisit('Bob')">Bob</button>
<div id="display"></div>
Try including utilization of input elements; set visits count as value of property id, info at object list; call test with value of input element : id ; or at prompt : info . At five visits , display coffee , reset value of id to 0.
var list = {
"111": 0,
"222": 0,
"333": 0,
"444": 0,
"555": 0,
"666": 0
};
var test = function test(id) {
coffee.innerHTML = "";
var info;
if (id.length > 0 && !/^(\s)/.test(id)) {
if (list.hasOwnProperty(id)) {
++list[id];
} else {
list[id] = 1;
}
info = confirm("hi " + id + " this is your " + list[id] + " visit");
} else {
info = prompt("hi, please enter an id");
if (!!info && !/^(\s)/.test(info) && list.hasOwnProperty(info)) {
list[info] = 1;
confirm("hi " + info + " this is your " + list[info] + " visit");
} else {
info = prompt("hi, please input a different id");
if (!!info && !/^(\s)/.test(info) && !list.hasOwnProperty(info)) {
list[info] = 1;
confirm("hi " + info + " this is your " + list[info] + " visit");
}
};
alert("please try a different id")
};
for (var id in list) {
if (list[id] === 5) {
alert("hi " + id + ", thanks for visting " + list[id] + " times");
coffee.innerText = "☕";
list[id] = 0;
break;
}
};
console.log(list);
};
var inputs = document.querySelectorAll("input");
var coffee = document.getElementById("coffee");
coffee.style.fontSize = "5em";
inputs[1].onclick = function(e) {
test(inputs[0].value)
};
<input type="text" placeholder="please enter id" value="" />
<input type="button" value="click" />
<div id="coffee"></div>
n is number of costumer
Initially array is [0,0,0,.....n times]
All values are 0 because no costumer has visited the shop.
For n=5 array would look like
var a=[0,0,0,0,0];
1)To solve this problem in array.
2)Suppose we have array of size n.
3)then index of the array will be 0...n-1
4)So we can map id of costumer to the index.
5)And value will keep count of the number of visit for the index.
6)a[5]=11; here 5 is index and 11 is value
7)For our problem if customer with id i we increment a[i]=a[i]+1 if he visits .
8) Then we can check
if(a[i]+1 === 5) {
console.log("Custumer "+ i + " get a free coffee");
a[i]=0;
}
else {
a[i]=a[i]+1;
}
I've decided to just try again from the start since I'm a bit more awake now and go over building this step by step. i've looked at some of the answers and there seems to be manyt ways one could go about this. I'm trying to do this using what I've learned so far. I've learned about variables, basic functions, objects, arrays, 'this' and the push method. I know for, while, do while, for in loops, though the for loop is the one I understand the best.
Here is my new approach to building this, I know it's unnecessarily long but I want to be able to get a basic understanding of how to piece the different things I've learned together in a very simple way. This is more about learning how to go about building simple programs. Then I would proceed in fine-tuning the program to make it more concise and clever. If you could have a look and tell me how I would proceed with what I've got so far...
Here is the code, Ideally I want to run a function when there's a new 'visitor' that asks for their name and number. Then create a new 'customer' object with the given name and number and push it to a 'visitors' array. once I've successfully figured that out I would use loops to check the array if the visitor is new or not, and update their number of visits everytime they come.
//array that will contain 'Customer' objects
var visitors = [john];
//Customer object
function Customer(name, phonenumber){
this.name = name;
this.phonenumber = phonenumber;
//will eventually add a "visits" method logging number of visits
}
var john = new Customer("john smith", "333");
//visitor funtion that runs everytime there is a new visitor
var visitor = function(){
//visitor does not have a set name or number yet
var userNumber = "variable userNumber is currently equal to " + 0;
var userName = "variable userName is currently set to " + undefined;
console.log(userName, userNumber);
//ask for visitor name and number
var askNumber = prompt("type your number");
var askName = prompt("what is your name?");
//store user name and number in two variables
var userNumber = "variable 'userNumber' is now equal to " + askNumber;
var userName = "variable userName is now set to " + askName;
//print out the new variables
console.log(userNumber);
console.log(userName);
//print who the phone number belongs to, this lets me see that the above code worked correctly
var userNumber = askNumber;
var userName = askName;
console.log("Phone number " + userNumber + " belongs to " + userName);
//make new customer object with the given name and number
var userNumber = new Customer();
userNumber.name = askName;
userNumber.phonenumber = askNumber;
console.log("properties of " + userNumber);
};
the last bit returns "properties of [object, object]" why?
I will give you a short function to check how many times a specific value is at the array:
function countHowManyTimesAValueIsOnArray(arr, valueToCheck){
return arr.filter(function(arrayValueToCompare){
return arrayValueToCompare === valueToCheck;
}).length
}
Attention for the use of '===' instead of '=='. This will compare the value considering its type. 222 will not be found as "222"
No need to complicate things here's an simple way, just add a count function that search how many times user has enter
var list = ["111", "222", "333", "444", "555", "666"];
var test = function(input){
var info = prompt("hi " + input + " what is your name?");
var check=0;
for(i=0; i< list.length; i++){
if( info === list[i]){
check=1;
break;
}
}
list.push(info);
if(check==1){
count(info);
}
}
function count(info){
var count=0;
for(i=0; i< list.length; i++){
if(list[i]===info)
count++;
}
console.log(count);
}
}

Add URL to specific word across entire website

I want to be able to link any word of my choice to a specific URL for example:
I want the word "goat" to link to "http://goat.com" across the entire website. So all "goat"/s will link to that URL right across the website.
I am using wordpress and I have not yet found a plugin to do this. If I can get a solution to this I would most likely create a plugin for this functionality.
I know how to target one word on a single page. But I would like it to be across all the pages and all the words in those pages( I used JavaScript for this).
Something like this may work for you.
function replaceWithUri(textToReplace, element){
element.innerHTML = element.innerHTML.replace(textToReplace, '<a href="http://www.' + textToReplace + '.com" >' + textToReplace + '</a>');
}
replaceWithUri('goat', document.getElementsByTagName('body')[0]);
Here's a crappy solution but it's better than nothing:
I found some code here which searches for a world across the whole page so I copy pasted that and modified it.
The replaceWord variable cannot contain the same string as word, otherwise it'll loop infinitely.
var word = " goat",
replaceWord = " <a href = 'http://goat.com'>goat</a>",
queue = [document.body],
curr
;
while (curr = queue.pop()) {
if (!curr.textContent.match(word)) continue;
for (var i = 0; i < curr.childNodes.length; ++i) {
switch (curr.childNodes[i].nodeType) {
case Node.TEXT_NODE : // 3
if (curr.childNodes[i].textContent.match(word)) {
curr.innerHTML = curr.innerHTML.replace(word,replaceWord);
}
break;
case Node.ELEMENT_NODE : // 1
queue.push(curr.childNodes[i]);
break;
}
}
}
Hello goat
<div>Look a goat</div>
This might be a bit resource intensive and replaceWord cannot contain the same string as word, otherwise it'll loop forever.
document.onload = function() {
var word = " goat",
replaceWord = " <a href = 'http://goat.com'>goat</a>";
while(document.body.innerHTML.indexOf(word) !== -1) {
document.body.innerHTML = document.body.innerHTML.replace(word,replaceWord);
}
}
Hello goat
<div>Look a goat</div>

Updating a ScriptProperty to avoid retrieving duplicate Twitter statuses

I was interested in writing a twitter bot to help out some friends at a local ski resort. I found this tutorial from Amit Agarwal which gave me enough to get started (it did take me more than 5 minutes since I did a lot of modifying). I host the script on google docs.
FIRST I think this is javascript (my understanding is that google apps script uses javascript...) and when I have had problems with the code so far, google searches for javascript-such-and-such have been helpful, but if this is not actually javascript, please let me know so I can update the tag accordingly!
I have no prior experience with javascript, so I am pretty happy that it's actually working. But I want to see if I'm doing this right.
The start function initiates the trigger, which kicks off the fetchTweets() function every interval (30 minutes). In order to avoid duplicates (the first errors I encountered) & potentially being flagged as spam, I needed a way to ensure that I was not posting the same tweets over and over again. Within the start() function, the initial since_id value is assigned:
ScriptProperties.setProperty("SINCE_TWITTER_ID", "404251049889759234");
Within the fetchTweet() function, I think I am updating this property with the statement:
ScriptProperties.setProperty("SINCE_TWITTER_ID", lastID + '\n');
Is this a good way to do this? Or is there a better/more reliable way? And if so, how can I be sure it's updating the property? (I can check the log file and it seems to be doing it, so I probably just need to create a permanent text file for the logger).
Any help is greatly appreciated!!
/** A S I M P L E T W I T T E R B O T **/
/** ======================================= **/
/** Written by Amit Agarwal #labnol on 03/08/2013 **/
/** Modified by David Zemens #agnarchy on 11/21/2013 **/
/** Tutorial link: http://www.labnol.org/?p=27902 **/
/** Live demo at http://twitter.com/DearAssistant **/
/** Last updated on 09/07/2013 - Twitter API Fix **/
function start() {
Logger.log("start!" + '\n')
// REPLACE THESE DUMMY VALUES
// https://script.google.com/macros/d/18DGYaa-jbaAK9rEv0HZ2cMcWjFGgkvVcvr6TfksMNbbu2Brk3gZeZ46R/edit
var TWITTER_CONSUMER_KEY = "___REDACTED___";
var TWITTER_CONSUMER_SECRET = "___REDACTED___";
var TWITTER_HANDLE = "___REDACTED___";
var SEARCH_QUERY = "___REDACTED___" + TWITTER_HANDLE;
// Store variables
ScriptProperties.setProperty("TWITTER_CONSUMER_KEY", TWITTER_CONSUMER_KEY);
ScriptProperties.setProperty("TWITTER_CONSUMER_SECRET", TWITTER_CONSUMER_SECRET);
ScriptProperties.setProperty("TWITTER_HANDLE", TWITTER_HANDLE);
ScriptProperties.setProperty("SEARCH_QUERY", SEARCH_QUERY);
ScriptProperties.setProperty("SINCE_TWITTER_ID", "404251049889759234");
// Delete exiting triggers, if any
var triggers = ScriptApp.getScriptTriggers();
for(var i=0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
// Setup trigger to read Tweets every 2 hours
ScriptApp.newTrigger("fetchTweets")
.timeBased()
.everyMinutes(30)
//.everyHours(2)
.create();
}
function oAuth() {
//Authentication
var oauthConfig = UrlFetchApp.addOAuthService("twitter");
oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");
oauthConfig.setConsumerKey(ScriptProperties.getProperty("TWITTER_CONSUMER_KEY"));
oauthConfig.setConsumerSecret(ScriptProperties.getProperty("TWITTER_CONSUMER_SECRET"));
}
function fetchTweets() {
oAuth();
// I put this line in to monitor whether the property is getting "stored" so as to avoid
// reading in duplicate tweets.
Logger.log("Getting tweets since " + ScriptProperties.getProperty("SINCE_TWITTER_ID"))
var twitter_handle = ScriptProperties.getProperty("TWITTER_HANDLE");
var search_query = ScriptProperties.getProperty("SEARCH_QUERY")
Logger.log("searching tweets to " + search_query + '\n');
// form the base URL
// restrict to a certain radius ---:
//var search = "https://api.twitter.com/1.1/search/tweets.json?count=5&geocode=42.827934,-83.564306,75mi&include_entities=false&result_type=recent&q=";
// unrestricted radius:
var search = "https://api.twitter.com/1.1/search/tweets.json?count=5&include_entities=false&result_type=recent&q=";
search = search + encodeString(search_query) + "&since_id=" + ScriptProperties.getProperty("SINCE_TWITTER_ID");
var options =
{
"method": "get",
"oAuthServiceName":"twitter",
"oAuthUseToken":"always"
};
try {
var result = UrlFetchApp.fetch(search, options);
var lastID = ScriptProperties.getProperty("SINCE_TWITTER_ID");
if (result.getResponseCode() === 200) {
var data = Utilities.jsonParse(result.getContentText());
if (data) {
var tweets = data.statuses;
//Logger.log(data.statuses);
for (var i=tweets.length-1; i>=0; i--) {
// Make sure this is a NEW tweet
if (tweets[i].id > ScriptProperties.getProperty("SINCE_TWITTER_ID")) {
lastID = (tweets[i].id_str);
var answer = tweets[i].text.replace(new RegExp("\#" + twitter_handle, "ig"), "").replace(twitter_handle, "");
// I find this TRY block may be necessary since a failure to send one of the tweets
// may abort the rest of the loop.
try {
Logger.log("found >> " + tweets[i].text)
Logger.log("converted >> " + answer + '\n');
sendTweet(tweets[i].user.screen_name, tweets[i].id_str, answer.substring(0,140));
// Update the script property to avoid duplicates.
ScriptProperties.setProperty("SINCE_TWITTER_ID", lastID);
Logger.log("sent to #" + tweets[i].user.screen_name + '\n');
} catch (e) {
Logger.log(e.toString() + '\n');
}
}
}
}
}
} catch (e) {
Logger.log(e.toString() + '\n');
}
Logger.log("Last used tweet.id: " + lastID + + "\n")
}
function sendTweet(user, reply_id, tweet) {
var options =
{
"method": "POST",
"oAuthServiceName":"twitter",
"oAuthUseToken":"always"
};
var status = "https://api.twitter.com/1.1/statuses/update.json";
status = status + "?status=" + encodeString("RT #" + user + " " + tweet + " - Thanks\!");
status = status + "&in_reply_to_status_id=" + reply_id;
try {
var result = UrlFetchApp.fetch(status, options);
Logger.log("JSON result = " + result.getContentText() + '\n');
}
catch (e) {
Logger.log(e.toString() + '\n');
}
}
// Thank you +Martin Hawksey - you are awesome
function encodeString (q) {
// Update: 09/06/2013
// Google Apps Script is having issues storing oAuth tokens with the Twitter API 1.1 due to some encoding issues.
// Henc this workaround to remove all the problematic characters from the status message.
var str = q.replace(/\(/g,'{').replace(/\)/g,'}').replace(/\[/g,'{').replace(/\]/g,'}').replace(/\!/g, '|').replace(/\*/g, 'x').replace(/\'/g, '');
return encodeURIComponent(str);
// var str = encodeURIComponent(q);
// str = str.replace(/!/g,'%21');
// str = str.replace(/\*/g,'%2A');
// str = str.replace(/\(/g,'%28');
// str = str.replace(/\)/g,'%29');
// str = str.replace(/'/g,'%27');
// return str;
}
When you use ScriptProperties.setProperty("KEY", "VALUE");, internally Script Properties will overwrite a duplicate key (i.e., if an old Property has the same key, your new one will replace it). So in your case, since you are using the same identifier for the key (SINCE_TWITTER_ID), it will replace any previous Script Property that is that key.
Furthermore, you can view Script Properties via File -> Project properties -> Project properties (tab). Imo Google didn't name that very well. User properties as specific to Google users. Script properties as specific to the Script Project you are working under.
Also, it probably isn't a good idea to include \n in your value when you set the property. That will lead to all sorts of bugs down the road, because you'll have to compare with something like the following:
var valToCompare = "My value\n";
instead of:
var valToCompare = "My value";
because the value in SINCE_TWITTER_ID will actually be "some value\n" after you call your fetchTweet() function.
Of course, one seems more logical I think, unless you really need the line breaks (in which case you should be using them somewhere else, for this application).
Its ok like that thou I dont know why you are adding \n at fhe end. Might confuse other code. You can see script properties in the script's file menu+ properties

Manage firefox tabs in emacs with org-mode

I recently find a way to manage firefox tab in emacs. This sounds a little crazy. I use tree style tabs(firefox addon), Moz Repl, emacs, org-mode to do it.
For 10-15 tabs, my plan works fine. But 20+ tabs, My firefox hangs randomly. Maybe javascript stack overflow or something else? I don't know what's wrong with my code. I post the most import code here. Somesone help me to find some bugs?
It's a basic firefox chrome code below, you can run it in firefox without emacs and MozPepl.
I use tree style tabs api to get tabs and set each tab a cetain level. The output will be used in emacs with org-mode.
tree style tabs api: http://piro.sakura.ne.jp/xul/_treestyletab.html.en#api
The Code can run in many ways. I recommend "workspace addon". Copy My code, choose chrome context to run it.
https://addons.mozilla.org/en-US/firefox/addon/workspace/
// two helper function to get title and url of tab
function getTitle(tab)
{
var brower = gBrowser.getBrowserForTab(tab)
var url = brower.currentURI.spec
var title = brower.contentTitle
return title
}
function getUrl(tab)
{
var brower = gBrowser.getBrowserForTab(tab)
var url = brower.currentURI.spec
var title = brower.contentTitle
return ":PROPERTIES:\n:URL:"+url+"\n:END:\n"
}
var L = gBrowser.tabContainer.childNodes.length //firefox tabs length
var str = "" //global string for output
//parse tabs. If tab has child, parse it. It tab has no child, just output.
for(i = 0; i < L; i++){
level = "*"
tab = gBrowser.tabContainer.childNodes[i]
if ('TreeStyleTabService' in window){
if(TreeStyleTabService.hasChildTabs(tab))
{
str = [str, level, " [+] ", getTitle(tab), "\n", getUrl(tab)].join("") //output title and url. level used in org-mode
treeparse(TreeStyleTabService.getChildTabs(tab), "**") //if a tab has child tabs. parse it and level up
}
str = [str, level, " ", getTitle(tab), "\n", getUrl(tab)].join("")
}
function treeparse(tablist,level) //parse a list of tabs. If tab has not a child, output. If it has childs, parse again
{
for(i=0 ; i < tablist.length;i++) {
tab = tablist[i]
if ('TreeStyleTabService' in window){
if(TreeStyleTabService.hasChildTabs(tab))
{
str = [str, level, " [+] ", getTitle(tab), "\n", getUrl(tab)].join("")
newlevel = level + "*"
treeparse(TreeStyleTabService.getChildTabs(tab),newlevel)
}
} }
str = [str, level, " ", getTitle(tab), "\n", getUrl(tab)].join("")
}
}
alert(str) //alert to view result. You can also write the result into a file.
I'm not sure what's specifically causing the problem, as I couldn't reproduce it, but I see loads of issues with this code. I can't remember how MozRepl works, but this improved code should give you a nice org-mode friendly tab output. I hope this helps you, or whoever stumbles across this thread.
var bullet = "*"; // Org-mode bullet
// two helper function to get title and url of tab
function getTitle(tab) {
var brower = gBrowser.getBrowserForTab(tab);
var url = brower.currentURI.spec;
var title = brower.contentTitle;
return title;
}
function getUrl(tab) {
var brower = gBrowser.getBrowserForTab(tab);
var url = brower.currentURI.spec;
var title = brower.contentTitle;
return ":PROPERTIES:\n:URL:"+url+"\n:END:\n";
}
// NOTE: we factor these string-generation functions out,
// to make things a bit more clear
function makeParentNodeOutput(tab, level) {
return (Array(level+1).join(bullet) +
" [+] " +
getTitle(tab) +
"\n" +
getUrl(tab));
}
function makeLeafNodeOutput(tab, level) {
return (Array(level+1).join(bullet) +
" " +
getTitle(tab) +
"\n" +
getUrl(tab));
}
// NOTE: we only need to handle parsing a collection of tabs
// in once place, and we have a function for it here.
function parseTabCollection(tabs, level) {
var currentTab;
var outputString = "";
for(var i = 0; i < tabs.length; i++){
currentTab = tabs[i];
// For a parent node, we output the node and its children
if(TreeStyleTabService.hasChildTabs(currentTab)){
outputString += makeParentNodeOutput(currentTab, level);
outputString += parseTabCollection(
TreeStyleTabService.getChildTabs(currentTab),
level + 1
);
} else {
outputString += makeLeafNodeOutput(currentTab, level);
}
}
return outputString;
}
if ('TreeStyleTabService' in window){
//NOTE: Start with the rootTabs only. The old version started with
// *all* tabs, which isn't what we want
var orgModeOutput = parseTabCollection(TreeStyleTabService.rootTabs, 1);
alert(orgModeOutput);
}
I hope this helps somehow.

Categories