Tags: ¥åßßå
Greasemonkey
A long time ago I installed Greasemonkey and with the latest upgrade it survived after transition to Firefox 3.0.
Greasemonkey allows you to run javaScripts that you created yourself on somebody else's sites. That's the short explanation. Anyway, the script does not need to be included in the source as you might expect from a normal javaScript. You may want to change the background colour or the font size of an arbitrary site you often visit. Those are common uses, together with removal of certain elements most notably adds. You can completely restyle a webpage.
The main site Greasespot holds a huge collection of scripts. Installing a script is easy. If Greasemonkey is installed you only need to click the link to the Greasemonkey script to trigger an installer. You will see at the end of this post how that goes. Unfortunately I didn't find the Killer App there. I don't believe there is. So the use of Greasemonkey is in practice limited to things you make cy yourself. Luckily the huge script library is of great help there. You are bound to find a script that does 'almost' what you want.
In the Greasespot site you find a link to the Open Source (downloadable) book Dive Into Greasemonkey which is an excellent guide to get started apart from the fact that most of the examples don't run. The book is simply outdated. But worth a glance anyway.
For the B2evolution forums I wanted some hacks. One of them is available as Greasemonkey script. That is phpBB No Stretch. If a post contains a code with a very long line as can be the case with an URL the post itself becomes very wide rather than wrapping rhe long line. The phpBB No Stretch plugin for Greasespot does just that. It wraps the long line.
A very impressive plugin is IMDB Pirated Version It adds a bar with links to torrents of the particular movie from the page you're at. Have a look at this picture:

The line just under the film title is added by the script. If you click it it Ajaxes to the sites it gets its information from. Actually, this is a kind of Killer App.
Of course I started making my own scripts. The first one is for the Forums for B2evolution. One of the posters calls himself ¥åßßå, so he will be called ¥åßßå. To write a script for Greasemonkey you start of with the header. That's self explanatory:
Code:
// ¥åßßå example user script | |
// version 0.1 BETA! | |
// 2005−04−22 | |
// Copyright (c) 2008, Foppe Hemminga | |
// Released under the GPL license | |
// http://www.hemminga.net/greasemonkey | |
// http://www.blog.hemminga.net/ | |
// | |
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− | |
// | |
// This is a Greasemonkey user script. | |
// | |
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/ | |
// Then restart Firefox and revisit this script. | |
// Under Tools, there will be a new menu item to "Install User Script". | |
// Accept the default configuration and install. | |
// | |
// To uninstall, go to Tools/Manage User Scripts, | |
// select "Yabba", and click Uninstall. | |
// | |
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− | |
// | |
// ==UserScript== | |
// @name Yabba | |
// @namespace http://www.hemminga.net/greasemonkey/ | |
// @description Changes Yabba/yabba into ¥åßßå | |
// @include http://forums.b2evolution.net/posting.php* | |
// @include http://forums.b2evolution.net/viewtopic.php* | |
// @exclude | |
// @exclude | |
// ==/UserScript== |
After that comes the user defined javaScript. In this case the user is the coder ;p
Code:
// Searches for textareas on the page | |
var allTextareas, thisTextarea; | |
allTextareas = document.getElementsByTagName('textarea'); | |
| |
for (var i = 0; i < allTextareas.length; i++) { | |
thisTextarea = allTextareas[i]; | |
// do something with thisTextarea | |
thisTextarea.addEventListener('change', changeName, false ); | |
} | |
| |
function changeName() { | |
var theText = thisTextarea.value; | |
var myRegExp = /yabba/gi; // regex must *not* be quoted | |
var modifiedText = theText.replace(myRegExp, "¥åßßå"); | |
thisTextarea.value = modifiedText; | |
} |
As you can see the code is identical to the code you would have used if it was your site and you would have embedded this javaScript.
You can download this code. If you have Greasemonkey installed it will install this script. If not it will show a page with the script.
yabba.user.js
Have fun
Manipulating the title with a plugin
tilqicom came with a problem. He wanted some specific changes to the title of a post automated. At first I got it wrong, thinking that he wanted some text replacement in the body of a post. That's fairly easy, use the RenderItemAsHtml function in a plugin. When I finished that I heard that he specifically wanted to automatically change some text in the title. We are talking about a change like the input '[date]' to 'Tuesday March 24 2008'.
That caused some serious problems because I hadn't seen it done before. ¥åßßå however had and pointed me in the right direction, I had to use the (Un)FilterItemContents( & $params ) functions. They filter (and you can make changes) at the point where you enter the write section and at leaving it (hit 'save'). Furthermore the $params are passed through the functions so both $params[ 'title' ] and $params[ 'content' ] are available.
Let's have a look at the results. This is the FilterItemContents function that is called after leaving the edit / write section of a post:
PHP:
function FilterItemContents( & $params ) | |
{ | |
$title = & $params[ 'title' ]; | |
$content = & $params[ 'content' ]; | |
| |
if( strpos( $title, '[cat wrote]' ) !== false ) | |
{ | |
global $edited_Item; | |
$original_title = $title; | |
| |
$foo = get_the_category_by_ID( $edited_Item->main_cat_ID ); | |
| |
$title = str_replace( '[cat wrote]', $foo[ 'cat_name' ] . ' wrote: ' . implode( ', ', $edited_Item->get_tags() ), $title ); | |
| |
$params[ 'title' ] = $title; | |
$original_title = '<!-- item title ' . $original_title . ' -->'; | |
$content = $content . $original_title; | |
$params[ 'content' ] = $content; | |
} | |
| |
return true; | |
} |
The magic quote here is '[cat wrote]' in the title of the post, so we first filter the title on the presence of that line. The long codeline does the hard job, it changes the magic block into something else and if you plan to use this block, this is where you should focus your attention upon.
What's next is an improvement I made later on. We store the original title in a comment in the post. That we will retrieve in the UnFilterItemComments function.
In that function we see a partial reverse, I am trying to restore the original situation:
PHP:
function UnfilterItemContents( & $params ) | |
{ | |
$title = & $params[ 'title' ]; | |
$content = & $params[ 'content' ]; | |
| |
// global $edited_Item; | |
if( strpos( $content, '<!-- item title' ) ) | |
{ | |
$original_title = $content; | |
preg_match( '~<\!\-\- item title (.+?) \-\->~', $original_title, $original_title_array ); | |
| |
$params[ 'title' ] = $original_title_array[1]; | |
| |
$content = preg_replace( '#<!-- item title .* -->#', '', $content ); | |
$params[ 'content' ] = $content; | |
| |
} | |
| |
return true; | |
} |
First of all we look for the comment in the body of the post. If it's there we extract the original title, place it back where it belongs and delete the comment.
So the way I did it is nearly failsafe. The only point is that the plugin needs to be set to 'always' or 'stealth' to avoid nasty complications.
Any one care to find out what *this* plugin actually does with the title?