Greasemonkey, forms & selected text copy.

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Greasemonkey, forms & selected text copy.

Post by JAM »

I'm calling for aid because... well I'm lazy and tired of this now. :banghead:

I'd like, to select text in an <textarea> and by using a greasemonkey script add the functionality that phpbb's tag-buttons.I've tried various of functions as window.getSelection().toString(); and similiar without result. Would post code, but I don't have it here.

Code: Select all

Some [color=#FF0000]text selected[/color] here
...transforms after passing through greasemonkey...

Code: Select all

Some

Code: Select all

[color=#FF0000]text selected[/color]
here
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Greasemonkey, forms & selected text copy.

Post by Weirdan »

That should be easy:
[js] unsafeWindow.bbfontstyle('[bb]','[/bb]'); [/js]
This will work on phpbb forums (this one for example) because they provide such a function.

Here's what I quickly hacked together:

Code: Select all

var buttons = document.getElementsByClassName('button2');
var button = undefined;
for (var i = 0, l = buttons.length; i < l; i++) {
    if (buttons.item(i).parentNode.id == 'format-buttons') {
        button = buttons.item(i);   
        break;
    }
}
if (button) {
    ['php','js','sql'].forEach(function(lang) {
        var newButton = button.cloneNode(false);
        newButton.value = lang;
        newButton.title = 'Wrap in ' + lang + ' tags';
        newButton.setAttribute('onclick', "");
        newButton.addEventListener('click', function() { 
            unsafeWindow.bbfontstyle('[code=' + lang + ']', '[/' + 'code]'); 
        }, false);
        button.parentNode.insertBefore(newButton, null);
    });
}
 
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Greasemonkey, forms & selected text copy.

Post by JAM »

Ahhhha! Thanks for that. Will disect that later and hopefully learn something.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Greasemonkey, forms & selected text copy.

Post by JAM »

getElementsByClassName Is a Firefox 3 feature tho. But nevermind, I'll mess around with it.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Greasemonkey, forms & selected text copy.

Post by Weirdan »

JAM wrote:getElementsByClassName Is a Firefox 3 feature tho. But nevermind, I'll mess around with it.
Yeah, I must have forgotten about that because I use ff3 for over a month already (and recommend it to everyone for performance reasons. Eats less memory too).
Post Reply