teimaj
teimaj

Reputation: 334

Get browser tab titles in Node js

I am trying to get the title of a tab in a browser, say Google Chrome for example, in my Node application. Something similar to GetWindowText() from the Win32 API. It needs to be applicable to multiple browsers if possible

At the moment in Node I can retrieve a list of current processes, but I am unable to find anything that will give me the details of that process, equivalent to when you open Task Manager and click the expand arrow on the Google Chrome process and it provides you with the names of the tabs.

I tried some node modules from npm, however they do not seem to return me the information I need. I have also looked on here for similar questions, but I didn't find anything that was similar to what I am asking here.

So if anyone could point me in the right direction I would appreciate it,

Thanks

Upvotes: 1

Views: 2937

Answers (2)

teimaj
teimaj

Reputation: 334

I solved my problem by using the ffi and ref node modules in my application that allowed me to use the user32.dll or any other .dll that i want to create. I then found this bit of code from node-ffi - callback extraction of EnumWindows that i changed to JavaScript that did the trick for me.

var voidPtr = ref.refType(ref.types.void);
var stringPtr = ref.refType(ref.types.CString);

var bindings = {
    EnumWindows: ['bool', [voidPtr, 'int32']],
    GetWindowTextA: ['long', ['long', stringPtr, 'long']]
};

var user32 = ffi.Library('user32', bindings);
var windowProc = ffi.Callback('bool', ['long', 'int32'], function (hwnd, lParam) {
    var buf = new Buffer(255);
    var ret = user32.GetWindowTextA(hwnd, buf, 255);
    var name = ref.readCString(buf, 0);     
    console.log(name);   
    return true;      
});

var checkTitles = user32.EnumWindows(windowProc, 0);

This is however only applicable on Windows, and i am yet to find (properly look) for a solution on OSX. Hopefully this can help somebody else

Upvotes: 1

Jarrod
Jarrod

Reputation: 183

You should look into the Chrome remote debugging protocol here:

https://developer.chrome.com/devtools/docs/debugger-protocol

Specifically this part:

In this scenario, you can substitute Developer Tools front-end with your own implementation. Instead of navigating to the HTML page at http://localhost:9222, your application can discover available pages by requesting:

http://localhost:9222/json and getting a JSON object with information about inspectable pages along with the WebSocket addresses that you could use in order to start instrumenting them.

There are a few node.js libraries that work with the protocol.

https://github.com/cyrus-and/chrome-remote-interface

https://github.com/node-inspector/node-inspector

These might help you on your way.

Upvotes: 1

Related Questions