12

Screenshot / Code Snippet

Screen Capture

var room = ThirdPlace.Room.GetCurrentRoom();
var message = new ThirdPlace.Message;
message.room_id = room.id;
message.content = "ThirdPlace.js is a library which abstracts some of the concepts of the SE Chat system (the \"Third Place\") into easy to use JavaScript objects.";
message.post();
message.content += " You can find it over at StackApps.";
message.post();

About

ThirdPlace.js is a library which abstracts some of the concepts of the SE Chat system (the "Third Place") into easy to use JavaScript objects. It's still very much a work in progress, but it is functional. When complete, almost all parts of chat should have ThirdPlace objects associated with them, allowing others to create event-driven UserScripts without having to reverse-engineer chat. For example, running code when a user enters / leaves a room, when messages are posted / edited, when notifications happen, etc, will all be as simple as observing events on ThirdPlace. In addition, posting/editing messages, joining rooms, and more will all be able to be done through a simple set of objects. (See code sample)

License

This is free software released under the MIT license.

Download

Current version v0.6a available from my website. Also available on GitHub

Posting of messages and observing new message events both confirmed to work. Other events coming soon!

Release Notes

  • v0.6a:
    • Added a 1 second poll for new events; this makes ThirdPlace work with the new WebSockets based chat system. Event firing should work again!
    • Made ThirdPlace.debug default to false, this should eliminate "console.log.apply is not a function" errors. Set ThirdPlace.debug = true; to enable debugging.

Platform

Currently tested on Chrome. Should work on Safari and Firefox as well.

Contact

This library was created and is maintained by Joshua Gitlin a.k.a The Unhandled Exception. You can contact me via my website or via email at thirdplace -at- josh -dot- gitlin -dot- name

Code

This library was built with snippits of code from Tim Stone and includes chunks of Prototype.js including the Class implementation. If others with to develop it, contact me and I will set up a repo.

4
  • @Raphael are you using this on a StackExchange site? $ should be defined if jQuery is available...
    – Josh
    Commented Mar 15, 2012 at 12:56
  • @Josh Apparently (see definition of inject), chat.SE does not use $ but jQuery?
    – Raphael
    Commented Mar 15, 2012 at 14:18
  • 2
    I'll check into this and get back to you @Raphael!
    – Josh
    Commented Mar 15, 2012 at 14:58
  • @Raphael I just tested a userscript I wrote which uses ThirdPlace under Firefox 10.0.2 and it seems to work okay... can you answer this question with more detail about the errors you're seeing and maybe some sample code and I'll try to reproduce? Thanks! (I'll also confirm that the two versions match)
    – Josh
    Commented Mar 16, 2012 at 16:26

1 Answer 1

2

This "answer" relates to version 5a which has been superseded since.

This is a minimal failing example:

// ==UserScript==
// @name           Test
// @namespace      .
// @description    Test userscript.
// @require        http://code.jquery.com/jquery-1.7.1.min.js
// @require        http://josh.gitlin.name/userscripts/ThirdPlace.js
// @include        http://chat.stackexchange.com/rooms/*
// ==/UserScript==

/*
 * Injects functions into the page so they can freely interact with existing code
 * Taken from http://stackapps.com/q/2171/10127
 */
function inject() {
  for (var i = 0; i < arguments.length; ++i) {
    if (typeof(arguments[i]) == 'function') {
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.textContent = '(' + arguments[i].toString() + ')(jQuery)';
      document.body.appendChild(script);
    }
  }
}

inject(function($) {
  alert('inject');
  ThirdPlace.observe('newMessage',function(event) {
    alert(event);
  });

  ThirdPlace.observe('editMessage',function(event) {
    alert(event);
  });
});

The alert with inject is executed (when entering chat), the others are nor (when message events happen).

This is the error I get (repeatedly) when reloading the chat and when new messages come in:

Error: console.log.apply is not a function
Source File: resource://greasemonkey/runScript.js
Line: 410

Without

// @require        http://code.jquery.com/jquery-1.7.1.min.js

I get

Error: $ is not defined
Source File: file://.../ThirdPlace.js
Line: 587

I run Firefox 10.0.1

Follow-up questions:

  • Is there an event that is triggered after entering a room when all messages have been loaded? Or is a newMessage event triggered for all shown messages?
  • Do message events contain the corresponding DOM element, i.e. the div.message or div.content nodes?
20
  • 2
    Hey sorry, somehow I missed this answer until just now! I'll take a look at this and get back to you shortly.
    – Josh
    Commented Mar 23, 2012 at 12:07
  • Found the problem. ThirdPlace.observe seems to be broken. ThirdPlace is working, and the console.log.apply messages are just a tangential issue. For some reason ThirdPlace is unable to detect events. Working on that, will post back when it's resolved!
    – Josh
    Commented Mar 23, 2012 at 14:41
  • window.addEventListener('storage',...) is not firing in FireFox, so that's why ThirdPlace.observe is having no effect. Looking for a solution.
    – Josh
    Commented Mar 23, 2012 at 14:54
  • Okay Raphael, I just released v0.6 of ThirdPlace. Please update to the new code and try your test again. Thanks!
    – Josh
    Commented Mar 23, 2012 at 16:11
  • @Josh No luck. It does not throw any errors but no dialogs pop up. Does the example work for you? Maybe ThirdPlace is not the problem; in this case I would appreciate hints as towards what I am doing wrong.
    – Raphael
    Commented Mar 24, 2012 at 17:36
  • I didn't try your specific example, I tried part of it in the Firebug console. Let me play with this a bit longer and see if I can reproduce. I think the issue may be the @require http://josh.gitlin.name/userscripts/ThirdPlace.js. Try wrapping the entire ThirdPlace.js script in an inject(function($) { call?
    – Josh
    Commented Mar 24, 2012 at 18:37
  • To answer your other questions: right now Message objects do not know their DOM object, but I can add such a feature. At the moment, newMessage events only fire as new messages are loaded, they won't fire for older messages in the room when you join. Again, I can add that.
    – Josh
    Commented Mar 24, 2012 at 18:39
  • @Josh when I inject your code it works. O.o I am a userscript novice, but does such behaviour not defeat the purpose of @require? Regarding DOM and old messages: that would be cool! I want to tell MathJax to work on the message text so I need a handle, and of course you would want math in old messages to be parsed, too (in particular also when even older messages are explicitly loaded). Thanks for your help!
    – Raphael
    Commented Mar 24, 2012 at 19:20
  • An alternative to passing the DOM object of messages would be their ID; with that, one can retrieve the required elements oneself.
    – Raphael
    Commented Mar 24, 2012 at 19:29
  • I tried injecting a script tag with src pointing to your file but that causes the $-issue again (I guess; at least nothing happens.)
    – Raphael
    Commented Mar 24, 2012 at 19:39
  • Two more things: newMessge is fired for the latest message when entering a room. Is this intended? And all newMessage events seem to come through twice (not so editMessage).
    – Raphael
    Commented Mar 24, 2012 at 19:45
  • I'll check in to these items and get back to you. Also, a GitHub site may be in order :-)
    – Josh
    Commented Mar 25, 2012 at 17:02
  • 3
    I'm working on these questions now @Raphael
    – Josh
    Commented Mar 29, 2012 at 18:37
  • there's definitely bugs with double messages. I think it's related to multiple tabs, but I am unsure. Still working on it!
    – Josh
    Commented Mar 30, 2012 at 2:50
  • @Josh: How are things going? Just letting you know someone still cares. ;)
    – Raphael
    Commented Apr 11, 2012 at 10:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .