Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Javascript - Node.js require('ttapi') not working from bookmarklet

Javascript - Node.js require('ttapi') not working from bookmarklet

Problem

I'm trying to write a bot for turntable.fm using alaingilbert's API (https://github.com/alaingilbert/Turntable-API), which uses node.js. I've never written a line of javascript before, so I'm not sure what I'm getting wrong here.

Here's my code:

var container = document.body;

if(document.getElementById("ttapi")) {
    container.removeChild(document.getElementById("ttapi"));
}
var obj = document.createElement("div");
obj.id = "ttapi";
obj.style.position = "absolute";
obj.style.top = "20px";
obj.style.left = "20px";
obj.style.width = "500px";
obj.style.height = "100px";
obj.style.padding = "10px";
obj.style.backgroundColor = "#fff";
obj.style.fontSize = "13px";
var auth = document.createElement("div");
    auth.innerHTML = "Auth: "+turntable.user.auth;
var userid = document.createElement("div");
    userid.innerHTML = "UserId: "+turntable.user.id;
for (var i in turntable) { if (turntable[i].roomId) { var rid = turntable[i].roomId; break; } }
var roomid = document.createElement("div");
    roomid.innerHTML = "RoomId: "+rid;
var testLine = document.createElement("div");
    testLine.innerHTML = " IM STILL WORKING";

var Bot= require('ttapi');
var AUTH = turntable.user.auth;
var USERID = turntable.user.id;
var ROOMID = rid;   

obj.appendChild(auth);
obj.appendChild(userid);
obj.appendChild(roomid);
obj.appendChild(testLine);
container.appendChild(obj);

I'm running the script from a bookmarklet while on turntable. I'm not really sure how to get good debug messages from javascript, so I'm debugging by moving container.appendChild(obj) down with each line of code. It stops displaying when the script stops working

Problem courtesy of: ZECTBynmo

Solution

It sounds like you're trying to run node.js code in the browser. If this is correct, then it won't work because require is a node-specific function. Node.js does not run in a browser; it is an alternative runtime for javascript that things like turntable.fm run on instead of on a browser.

If you want to write something using a bookmarklet, you will only be able to use what is available through the browser--that is, what you can access when you go to turntable.fm, open the javascript console and start typing things in. This generally consists of window and document, and any libraries that turntable.fm is using. If they're using the jquery, for example, you can access that with $.

Edit: Also, if you are using firefox look for the 'firebug' extension. If you're lucky (or use chrome), your browser (or an extension like firebug) will give you console.log which is useful for debugging.

Oh, and here's the manual: https://developer.mozilla.org/en-US/docs You'll want to read it.

Solution courtesy of: Joshua Holbrook

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Javascript - Node.js require('ttapi') not working from bookmarklet

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×