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... Commented Mar 15, 2012 at 12:56
  • @Josh Apparently (see definition of inject), chat.SE does not use $ but jQuery? Commented Mar 15, 2012 at 14:18
  • 2
    I'll check into this and get back to you @Raphael! 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) 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 Comments

Hey sorry, somehow I missed this answer until just now! I'll take a look at this and get back to you shortly.
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!
window.addEventListener('storage',...) is not firing in FireFox, so that's why ThirdPlace.observe is having no effect. Looking for a solution.
Okay Raphael, I just released v0.6 of ThirdPlace. Please update to the new code and try your test again. Thanks!
I'm working on these questions now @Raphael
|

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.