Auto linking IDs to the brickset database

taconight2taconight2 Member Posts: 3
edited August 2012 in Forum Operation
Hi Guys,

I'm new here. I love the site but there is one thing I think could be improved: I don't know all the set IDs by heart. I'm sure many people are in the same boat. So I was thinking it would be nice if the forum auto linked numeric IDs back to the brickset database. Often people don't want to go through the effort of linking themselves.

So when someone writes "I really love 8129" it will be replaced with "I really love __8129 AT-AT Walker (2010)__" which is a link and provides a name for the set even without clicking the link.

I messed around a bit and I was able to do this in my Firebug console window so I think it's something the admins could implement here even though the forum software is hosted by a 3rd party.

First the forum loads a static javascript file with a dictionary containing the 500 most popular sets or so. I've written some working code, you really just need to populate some sets in the _bs[] dictionary.

e.g.
_bs = {};
_bs.autoLink = function(html) {
// replace any "(space)###(space)" with a link to the brickset db
// number must be 4 to 6 chars to reduce chance of linking non lego ID numbers. e.g. dates
return html.replace(/\s(\d{4,6})\s/g, function(text, id) { return '' + id + ' ' + (_bs[id] || '') + ''; }); } _bs[8129] = 'AT-AT Walker (2010)'; _bs[10198] = 'Tantive IV (2009)';
That example only has 2. But you get the idea. This is a static file that you generate once in a while and then add to this forums head via a simple include.

Once you have that script loaded you can invoke it via this code:

$('.Message').each(function() { $(this).html(_bs.autoLink($(this).html())); });


What do you guys think?

Issues:
-I've only tested this under Firefox.
-I don't know if all brickset IDs actually end in "-1"

Comments

  • atkinsaratkinsar Member Posts: 4,272
    edited August 2012
    This is already possible by simply placing # in front of the set number e.g #8099

    Only limitation is it doesn't sem to work on the mobile version of the forum.

    It also doesn't add the set name in like yours which would be useful.
  • taconight2taconight2 Member Posts: 3
    edited August 2012
    So I just rewrote something that's already there? Funny.

    It looks like I'm not that only one that is unaware since so many sets aren't linked. I still like the idea of putting the name in however. It saves a click.
  • atkinsaratkinsar Member Posts: 4,272
    Yes you're right, a lot of people don't do it, maybe I'll announce something later. I'll let @Huw comment on the viability of adding the set name to the link, I agree it could be useful.
  • HuwHuw Brickset Towers, Hampshire, UKAdministrator Posts: 6,440
    Not a bad idea. I think to be truly useful, though, it would need to use JSON or something to retrieve the set names rather than a static list.

    I could certainly create a 'live' version from the database of the _bs array as a .JS file that could be referenced from here, that might be the easiest way.

    I'll give it some thought when I've finished what I'm currently working on.
  • CCCCCC UKMember Posts: 17,790
    I wouldn't want it to automatically convert numbers to sets.

    I'm doing a 1066 MOC, halfway there with the Normans, still loads of Saxons to go.

    I wouldn't want people thinking I am doing a 1066-1: Little People with Accessories MOC. Although I guess in reality, with that set name I am doing that.
    brickmatic
  • atkinsaratkinsar Member Posts: 4,272
    edited August 2012
    It should still require a positive action on the person's behalf, i.e. prefixing with the # to let the forum know you are specifying a set number. I agree, trying to convert all numbers to set links and descriptions wouldn't be good.
  • paul_mertonpaul_merton UKMember Posts: 2,951
    edited August 2012
    I made myself a bookmarklet (a small script contained in a bookmark) so that I can select a portion of text on any website and it'll automatically find the first Lego set number in the selected text and open the relevant Brickset page in a new window.

    To use this, you just have to create a new bookmark which points to this address:

    javascript:(function(){var%20setNumber=window.getSelection();if(setNumber==""){setNumber=prompt("No%20LEGO%20set%20number%20highlighted;%20please%20enter%20one%20manually.");}var%20regexp=/\b(\d{3,7})\b/;var%20result=regexp.exec(setNumber);if(!result){alert("Please%20highlight%20a%20LEGO%20set%20number%20or%20manually%20enter%20a%20valid%20number%20before%20clicking%20on%20the%20bookmarklet.");}else{setNumber=result[1];window.open("http://www.brickset.com/detail/?Set="+setNumber+"-1");}})()

    If you name the bookmark something sensible like "Lookup on brickset.com" then you just have to select the set number and click on the bookmark. It doesn't matter if you accidentally select too much text, as it'll search through the whole selection for a valid-looking set number.

    You could use something like GreaseMonkey to do this automatically for every page you visit, but I didn't consider that to be desirable! (It assumes that any 3-7 digit number corresponds to a Lego set, which is of course not true, and I can't be bothered to maintain a complete list of all set numbers in a bookmarklet :)

    I find this bookmarklet quite useful, especially when hunting around eBay. Let me know if you like it, or if you think it could be improved.
    brickmatic
  • graphitegraphite USMember Posts: 3,241
    ^ That is really handy Paul. Thanks for sharing.
Sign In or Register to comment.
Recent discussions Categories Privacy Policy