d2jsp
Log InRegister
d2jsp Forums > d2jsp > General Help > One-click Add Any Bbcode To Any Text Field On Jsp > Greasemonkey Script
1235Next
Add Reply New Topic
Banned
Posts: 2,525
Joined: Jan 2 2012
Gold: 0.01
Warn: 10%
Jan 24 2012 05:38pm
Hello jsp

As a request I wrote a little script that lets one create button to insert any bbcode into any text field on site, be it profile edit, new topic, pm or anything. He suggested I should publish this for general public so more peple might have any use.

What it allows you to do?
- one-click insert any bbcode you want into any active text box, automatically center your caret in between.
- create fast-access links to anything you want. Be it your friends' profiles, your gmail, your home page, anything.

What will you get right after install?
Little semi-transparent ninja icon in top-left corner of the screen, that when clicked, will make your text purple. This can be easily changed to any bbcode you want and you can have as many bbcodes as you want for quick access.

Important: this script loads 2 other scripts: one is at http://forums.d2jsp.org/jscripts/q.js and one is at http://pastebin.com/raw.php?i=kzDtpR0d. Feel free to inspect what they do, a little word on them follows :)

=== technical details here ===
Here's the script, save it as custom.user.js and open in firefox/chrome. firefox needs greasemonkey/scriptish addon. Should also work in opera if you put this into userjs dir.
Explanation in a second:
Code


// ==UserScript==
// @name D2JSP bbcode button
// @namespace MEH
// @description adds custom bbcode buttons
// @author ikusus
// @license wtfpl
// @include http://forums.d2jsp.org/*.php*
// @version  1
// ==/UserScript==

function __insf() {
//this is where you define buttons. Can be also used for links to often used places etc...
var buttons = [
{
"name": "color",
"html": "<img src='http://forums.d2jsp.org/html/emoticons/ph34r.gif' />",
"script": function() {
 doI("[color=purple]","[/color]", "");
 return false;
}
}

];
// don't edit below this line

var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = "div.bbcodebox{ color: #fff; width: 30px; position: fixed; top: 0px; left: 0px; opacity: 0.1; z-index: 9999999; }\n";
style.innerHTML += "div.bbcodebox:hover{ opacity: 1; }\n";
style.innerHTML += "div.bbcodebox a{ color: #fff;cursor: pointer; display: block;}\n";
document.getElementsByTagName("head")[0].appendChild(style);

var js = document.createElement('script');
js.src = 'http://forums.d2jsp.org/jscripts/q.js';
js.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(js);

var js2 = document.createElement('script');
js2.src = 'http://pastebin.com/raw.php?i=kzDtpR0d';
js2.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(js2);

var box = document.createElement("div");
box.setAttribute("class", "bbcodebox" );

for( i in buttons ) {
var a = document.createElement("a");

a.addEventListener( "click", buttons[i]["script"], false );
a.innerHTML = buttons[i]["html"];
a.title = buttons[i]["name"];
if (buttons[i]["href"])
a.href = buttons[i]["href"];
box.appendChild( a );
}

document.body.appendChild( box );

};

var __insb = document.createElement("script");
__insb.textContent = __insf + "\n\n __insf();";
__insb.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(__insb);



First, it has definition of what buttons you want. In this example it creates one button, with ninja emoticon, to insert purple color bbcode. Then there is little <style> definition that makes it display properly.

Lines:
Code

var js = document.createElement('script');
js.src = 'http://forums.d2jsp.org/jscripts/q.js';
js.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(js);

var js2 = document.createElement('script');
js2.src = 'http://pastebin.com/raw.php?i=kzDtpR0d';
js2.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(js2);


load required scripts - first one is from this site, but it's not always loaded (and we'll always need it), second overrides one of functions so that it'll work properly. Open both scripts and read if you please.

There is also some magic to override chrome/firefox differences so everything is framed into yet another function and injected into pages.

The last part inserts the button into the page. It'll be visible in top-left corner of the site.

=== technical details over ===

What you really have to care about is the buttons section.

If you need help customizing it to insert bbcode of your choice, pm me, i'll be glad to help. Hope it'll be of any use to anyone :)
Member
Posts: 14,754
Joined: Dec 14 2006
Gold: 0.00
Jan 24 2012 06:51pm
vouch. Great script. I requested this since njag refused to add a custom text addon since I always color and bold my text.
it works great. :)

Came from my original idea here: http://forums.d2jsp.org/topic.php?t=57800926&f=258


This post was edited by ThePwnEffect on Jan 24 2012 07:18pm
Member
Posts: 14,754
Joined: Dec 14 2006
Gold: 0.00
Jan 28 2012 12:11am
Due to recently helping another user install this, I feel that this thread needs updated with "simpler instructions" - as I couldn't even figure out how I did it before, or else I'm just a moron.
I couldn't get a file to save as a .js - had to just send him my copy of the color.user.js - greasemonkey is confusing to use compared to scriptish, in scriptish you just drag + drop the color.user.js into the window and it detects/prompts to install the script, and can be easily edited on the fly.
I may just be simple minded though, but it may make it easier if you upload a youtube video for installing with firefox / chrome * scriptish / greasemonkey, may increase the userbase.

Just a thought! :p

Thanks again for the script.
Banned
Posts: 2,525
Joined: Jan 2 2012
Gold: 0.01
Warn: 10%
Jan 30 2012 06:48pm
Hmm maybe later I'll try to make some illustrated guide for that, but being technical I am it seems just too easy to bother ... :P

in greasemonkey it should work just like that (i tested it on both GM/scriptish and chrome), and to save as .js you probably have to use real editor (like notepad++/notepad2) or make sure your system shows file extensions so you can remove trailing .txt, if any.

In short it should Just Work if you follow 3 easy steps:

- install scriptish/greasemonkey for firefox (not required for chrome)
- save everything from big code block into file named color.user.js (or download from http://pastebin.com/download.php?i=0harL941 - it will save as color.user.js.txt - remove .txt from the end)
- drag-and-drop the file into your browser window, or otherwise navigate to it.

I might update guide for opera if i find the demand and some time.

In case of trouble, pm me.
Member
Posts: 17,060
Joined: Jan 21 2005
Gold: 0.00
Jan 30 2012 10:01pm
installs fine..adds nothing. also have to save name as custom.user not custom.user.js
Banned
Posts: 2,525
Joined: Jan 2 2012
Gold: 0.01
Warn: 10%
Jan 31 2012 04:59am
Quote (Decadence @ 31 Jan 2012 05:01)
installs fine..adds nothing. also have to save name as custom.user not custom.user.js


No, it's really custom.user.js - possibly you have showing file ext disabled so only see color.user

And what browser are you using? Make sure it really adds nothing - there should be almost-transparent ninja in top-left corner of the screen, visible only on d2jsp. It's only default example action too (does this), which you need to customize for your needs. Another thing -> error console should tell you if something is wrong. You might try restarting browser too.

Also, just tested in chrome and firefox, using my previous post - works as expected.


This post was edited by ikusus on Jan 31 2012 05:04am
Member
Posts: 13,268
Joined: Feb 24 2010
Gold: 4.13
Jan 31 2012 10:41am
i like this script work's fine, nice idea.

Thank you :wub:


This post was edited by Famed on Jan 31 2012 10:42am
Member
Posts: 17,060
Joined: Jan 21 2005
Gold: 0.00
Jan 31 2012 10:56am
Quote (ikusus @ Jan 31 2012 03:59am)
No, it's really custom.user.js - possibly you have showing file ext disabled so only see color.user

And what browser are you using? Make sure it really adds nothing - there should be almost-transparent ninja in top-left corner of the screen, visible only on d2jsp. It's only default example action too (does this), which you need to customize for your needs. Another thing -> error console should tell you if something is wrong. You might try restarting browser too.

Also, just tested in chrome and firefox, using my previous post - works as expected.


when i leave the file name as "custom.user.js" and drag it to the screen it opens the file on my screen so i see the coding. When i leave the file name as "custom.user" it will load the script to the page but nothing...useing chrome.
Banned
Posts: 2,525
Joined: Jan 2 2012
Gold: 0.01
Warn: 10%
Feb 1 2012 05:21am
Quote (Decadence @ 31 Jan 2012 17:56)
when i leave the file name as "custom.user.js" and drag it to the screen it opens the file on my screen so i see the coding. When i leave the file name as "custom.user" it will load the script to the page but nothing...useing chrome.


well, make sure you really see file extensions, it should just work :F

Anyway, small update. Now it doesn't load external scripts and is really simplified, while providing a way to bind to other events (e.g. mouse over icon instead of clicking).

INSTALL:
1. uninstall old version
2. create empty file called color.user.js - make sure it doesn't end in .txt - windows often hides file extensions, disable that!
3. paste following code into it, edit buttons section (to have bbcode of your choice) save and exit
4a. (firefox only) install scriptish or greasemonkey if you didn't already
4b. drag and drop the file into chrome/firefox window.

Code

// ==UserScript==
// @name D2JSP custom bbcode button
// @namespace MEH
// @description adds custom bbcode buttons
// @author ikusus
// @license wtfpl
// @include http://forums.d2jsp.org/*.php*
// @version  1
// ==/UserScript==

function __insf() {
var buttons = [
 {
  "name": "color",
  "html": "<img src='http://forums.d2jsp.org/html/emoticons/ph34r.gif' />",
  "code": {
   "click": function() {
    bbcode_in("[B][COLOR=Purple]", "[/COLOR][/B]");
   }
  }
 }
]

var box = document.createElement("div");
box.setAttribute("class", "bbcodebox" );

for( i in buttons ) {
 var a = document.createElement("a");
 qc = buttons[i]["code"];
 for( j in qc )
  a.addEventListener( j, qc[j], false );
 a.innerHTML = buttons[i]["html"];
 a.title = buttons[i]["name"];
 box.appendChild( a );
}

document.body.appendChild( box );

function bbcode_in(_o,_c) {
 _t = "";
 var ppx = document.getElementsByName('Post');
 for(iq in ppx) {
  p = ppx[iq];
  if( p )
   p.focus();

  ms=_o.length;
  if(document.selection && document.selection.createRange) {
   var s = document.selection.createRange();
   _x=s.text!=""?s.text:_t;
   s.text = txt = _o+_x+_c;
   s.moveStart('character',-txt.length+ms);
   s.moveEnd('character',-txt.length+ms+_x.length);
   s.select();
  } else if(p.selectionStart!='undefined') {
   _s=p.selectionStart;
   _e=p.selectionEnd;
   _x=p.value.substring(_s,_e);
   if(_x=="")_x=_t;
   txt = _o+_x+_c;
   p.value=p.value.substr(0,_s)+txt+p.value.substr(_e);
   p.selectionEnd=p.selectionStart=_s+ms;
   p.selectionEnd+=_x.length;
  } else{
   p.value+=_o+_c;
  }
 }
}

function link_in(url) {
 document.location = url;
}


}

var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = "div.bbcodebox{ color: #000; width: 30px; position: fixed; top: 0px; left: 0px; opacity: 0.1; z-index: 9999999; }\n";
style.innerHTML += "div.bbcodebox:hover{ opacity: 1; }\n";
style.innerHTML += "div.bbcodebox a{ color: #fff;cursor: pointer; display: block;}\n";
document.getElementsByTagName("head")[0].appendChild(style);


var __insb = document.createElement("script");
__insb.textContent = "\n" + __insf + "\n\n__insf();";
__insb.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(__insb);




After refreshing forums.d2jsp.org you should get this little transparent ninja:


If you hover it with your mouse it'll be more visible:


When you click it, it'll add purple bold text bbcode (by default; edit buttons section to suit your needs; pm me in case of trouble)


AH. One more thing. If you use noscript or simillar you might need to disable it for forums.d2jsp.org for this to work!
Member
Posts: 31,186
Joined: Aug 31 2008
Gold: 28,306.50
Trader: Trusted
Feb 3 2012 10:56pm
Quote (ikusus @ Feb 1 2012 05:21am)
well, make sure you really see file extensions, it should just work :F

Anyway, small update. Now it doesn't load external scripts and is really simplified, while providing a way to bind to other events (e.g. mouse over icon instead of clicking).

INSTALL:
1. uninstall old version
2. create empty file called color.user.js - make sure it doesn't end in .txt - windows often hides file extensions, disable that!
3. paste following code into it, edit buttons section (to have bbcode of your choice) save and exit
4a. (firefox only) install scriptish or greasemonkey if you didn't already
4b. drag and drop the file into chrome/firefox window.

Code// ==UserScript==
// @name D2JSP custom bbcode button
// @namespace MEH
// @description adds custom bbcode buttons
// @author ikusus
// @license wtfpl
// @include http://forums.d2jsp.org/*.php*
// @version  1
// ==/UserScript==

function __insf() {
var buttons = [
  {
  "name": "color",
  "html": "<img src='http://forums.d2jsp.org/html/emoticons/ph34r.gif' />",
  "code": {
    "click": function() {
    bbcode_in("", "");
    }
  }
  }
]

var box = document.createElement("div");
box.setAttribute("class", "bbcodebox" );

for( i in buttons ) {
  var a = document.createElement("a");
  qc = buttons[i]["code"];
  for( j in qc )
  a.addEventListener( j, qc[j], false );
  a.innerHTML = buttons[i]["html"];
  a.title = buttons[i]["name"];
  box.appendChild( a );
}

document.body.appendChild( box );

function bbcode_in(_o,_c) {
  _t = "";
  var ppx = document.getElementsByName('Post');
  for(iq in ppx) {
  p = ppx[iq];
  if( p )
    p.focus();

  ms=_o.length;
  if(document.selection && document.selection.createRange) {
    var s = document.selection.createRange();
    _x=s.text!=""?s.text:_t;
    s.text = txt = _o+_x+_c;
    s.moveStart('character',-txt.length+ms);
    s.moveEnd('character',-txt.length+ms+_x.length);
    s.select();
  } else if(p.selectionStart!='undefined') {
    _s=p.selectionStart;
    _e=p.selectionEnd;
    _x=p.value.substring(_s,_e);
    if(_x=="")_x=_t;
    txt = _o+_x+_c;
    p.value=p.value.substr(0,_s)+txt+p.value.substr(_e);
    p.selectionEnd=p.selectionStart=_s+ms;
    p.selectionEnd+=_x.length;
  } else{
    p.value+=_o+_c;
  }
  }
}

function link_in(url) {
  document.location = url;
}


}

var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = "div.bbcodebox{ color: #000; width: 30px; position: fixed; top: 0px; left: 0px; opacity: 0.1; z-index: 9999999; }\n";
style.innerHTML += "div.bbcodebox:hover{ opacity: 1; }\n";
style.innerHTML += "div.bbcodebox a{ color: #fff;cursor: pointer; display: block;}\n";
document.getElementsByTagName("head")[0].appendChild(style);


var __insb = document.createElement("script");
__insb.textContent = "\n" + __insf + "\n\n__insf();";
__insb.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(__insb);

After refreshing forums.d2jsp.org you should get this little transparent ninja:
http://i.imgur.com/jnCbm.png

If you hover it with your mouse it'll be more visible:
http://i.imgur.com/Cu46S.png

When you click it, it'll add purple bold text bbcode (by default; edit buttons section to suit your needs; pm me in case of trouble)


AH. One more thing. If you use noscript or simillar you might need to disable it for forums.d2jsp.org for this to work!


put color.user.js

in quotes like this

"color.user.js"

when they save it.


This post was edited by Vegito on Feb 3 2012 11:06pm
Go Back To General Help Topic List
1235Next
Add Reply New Topic