Now I have a bookmarklet that gets triggered when I’m on a page I want to bookmark. It window.open()’s to the wiki page named after the page title and passes two parameters to the template: the title and the URI of the page. The template gets preloaded1More here: https://www.mediawiki.org/wiki/Manual:Creating_pages_with_preloaded_text .

javascript:window.open('http://pchr8.net/f/index.php?title='+encodeURIComponent(document.title)+'&action=edit&preload=Template:NewLink&preloadparams[]='+encodeURIComponent(location.href)+'&preloadparams[]='+encodeURIComponent(document.title))

The template itself looks pretty trivial. (Note to self: Templates are case sensitive?!)

Then we get a page with an URI and a title. I fill the additional values like this:

http://ronburk.blogspot.de/2009/09/commas-depend-on-linebreaks.html
Ron Burk: Commas Depend on Linebreaks
6


language, linguistics, internet, style, etiquette, mildly interesting

Language
Style

Then I run the bookmarklet, which transform it to the following:

{{B|#124;#124;
http://ronburk.blogspot.de/2009/09/commas-depend-on-linebreaks.html

|#124;#124;Ron Burk: Commas Depend on Linebreaks
|#124;#124;6
|#124;#124;5
}}
{{#set:
k=language, linguistics, internet, style, etiquette, mildly interesting
|#124;#124;+sep=, }}

[[Category: Language]]
[[Category: Style]]

Then I could add something, and then save it.

The new bookmarklet now understands an unlimited number of keywords and categories.

The updated bookmarklet:

var lines = '';
var lines = $('#wpTextbox1').val().split('\n'); 
for (i=0; i<7; i++) {
if (typeof lines[i] == 'undefined') {lines[i]='';}
}
 
if (!ValidURL(lines[0])) {alert(lines[0]+" doesn't look like a valid URL.")};
if (lines[1]=='') {lines[1]=lines[0]};
if (lines[2]=='') {lines[2]='5'};
if (lines[3]=='') {lines[3]='5'};
 
if (parseInt(lines[2]) > 10 |#124;#124;|#124;#124; parseInt(lines[2])<0 |#124;#124;|#124;#124; isNaN(lines[2])) {
alert(lines[2]+'is not a valid value, setting to default 5');
lines[2]='5';
}
 
if (parseInt(lines[3]) > 10 |#124;#124;|#124;#124; parseInt(lines[3])<0 |#124;#124;|#124;#124; isNaN(lines[3])) {
alert(lines[3]+'is not a valid value, setting to default 5');
lines[3]='5';
}

var keywords="\n{{#set:\n";
keywords+="k=";
keywords+=lines[5]; 
keywords+="\n|#124;#124;+sep=, }}\n";

var categories="\n";
for (i=0; i<lines.length-7; i++){
 categories+="[[Category: ";
 categories+=lines[i+7];
 categories+="]]\n";
}

var text="{{B|#124;#124;\n"+lines[0]+"\n|#124;#124;"+lines[1]+"\n|#124;#124;"+lines[2]+"\n|#124;#124;"+lines[3];
if (lines[4]!='') text+="\n|#124;#124;"+lines[4];
text+="\n}}";
text+=keywords;
text+=categories;
 
var field = document.getElementById('wpTextbox1');
var textArray = field.value.split("\n");
textArray.splice(0, lines.length);
textArray[0] = text;
field.value = textArray.join("\n");


function ValidURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|#124;#124;'+
'((\\d{1,3}\\.){3}\\d{1,3}))'+
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+
'(\\?[;&a-z\\d%_.~+=-]*)?'+
'(\\#[-a-z\\d_]*)?$','i');
return pattern.test(str);
}

Todo:

Make it check for bad characters (think “ #124;#124;”, which mediawiki doesn’t like in names and parameters, and everything else here: https://www.mediawiki.org/wiki/Manual:Page_title; make it do something for empty titles, so I don’t get redirected to the main page; put together all of this and upload it to github?

The final bookmarklet looks like this, in one line to make it easy to copypaste:

{% highlight javascript %} javascript:var lines = ‘’; var lines = $(‘#wpTextbox1’).val().split(‘\n’); for (i=0; i<7; i++) { if (typeof lines[i] == ‘undefined’) {lines[i]=’’;} } if (!ValidURL(lines[0])) {alert(lines[0]+” doesn’t look like a valid URL.”)}; if (lines[1]==’’) {lines[1]=lines[0]}; if (lines[2]==’’) {lines[2]=’5’}; if (lines[3]==’’) {lines[3]=’5’}; if (parseInt(lines[2]) > 10 |#124;#124;|#124;#124; parseInt(lines[2])<0 |#124;#124;|#124;#124; isNaN(lines[2])) { alert(lines[2]+’is not a valid value, setting to default 5’); lines[2]=’5’; } if (parseInt(lines[3]) > 10 |#124;#124;|#124;#124; parseInt(lines[3])<0 |#124;#124;|#124;#124; isNaN(lines[3])) { alert(lines[3]+’is not a valid value, setting to default 5’); lines[3]=’5’; } var keywords=”\n{{#set:\n”; keywords+=”k=”; keywords+=lines[5]; keywords+=”\n|#124;#124;+sep=, }}\n”; var categories=”\n”; for (i=0; i<lines.length-7; i++){ categories+=”[[Category: “; categories+=lines[i+7]; categories+=”]]\n”; } var text=”{{B|#124;#124;\n”+lines[0]+”\n|#124;#124;”+lines[1]+”\n|#124;#124;”+lines[2]+”\n|#124;#124;”+lines[3]; if (lines[4]!=’’) text+=”\n|#124;#124;”+lines[4]; text+=”\n}}”; text+=keywords; text+=categories; var field = document.getElementById(‘wpTextbox1’); var textArray = field.value.split(“\n”); textArray.splice(0, lines.length); textArray[0] = text; field.value = textArray.join(“\n”); function ValidURL(str) { var pattern = new RegExp(‘^(https?:\/\/)?’+ ‘(((a-z\d)\.?)+[a-z]{2,}|#124;#124;’+ ‘((\d{1,3}\.){3}\d{1,3}))’+ ‘(\:\d+)?(\/[-a-z\d%_.~+])’+ ‘(\?[;&a-z\d%_.~+=-])?’+ ‘(\#[-a-z\d_]*)?$’,’i’); return pattern.test(str); } {% endhighlight %}

Unrelated to the above:

Apparently, after N years of studying programming and tweaking/optimizing whatever incarnation of my workspace, I’m getting it. Like, you can really solve your own problems by writing your own code?! Wow!

What happens next? I’m gonna go around writing extensions and stuff? How was the quote: “Be a man, write your own drivers!”

Regardless, this small and dead-simple bookmarklet that solves my own problems has done much more for my motivation than anything else I’ve ever tried in the last couple of years.

Wohoo!

(Y)

Footnotes   [ + ]