Archive for February, 2014

Stop Hijacking Annoyances

Here’s another rescue from My Opera. The script was compiled by the user pehage on February 3, 2012, after a quick pointer by me. It is Opera UserJS, so it won’t work in any other browser. Don’t forget to enable User JavaScript on HTTPS if you so desire. In Opera 12 it no longer pops up a warning all the time.

// ==UserScript==
// @name Stop Hijacking Annoyances
// @include *
// ==/UserScript==

var annoyances = ["focus", "focusin", "focusout", /*"click",*/ "dblclick", "mousedown", "mouseup", "mousemove", "mouseover", "mouseout", "mouseenter", "mouseleave", "select", "submit", "keydown", "keypress", "keyup"];

for (var i=0; i<annoyances.length; i++) {
	//opera.postError(annoyances[i]);
	window.opera.addEventListener("BeforeEventListener." + annoyances[i], function (e) {
		e.preventDefault();
		//opera.postError(e);
	}, false);
}

And here is a simple testcase I created.

<!DOCTYPE html>
<html>
<head>
<title>keypress hijacker</title>
<script>
document.addEventListener('keypress', function(e){alert('document.addEventlistener on keypress ' + String.fromCharCode(e.which))}, false);
window.onkeydown = function(e){alert('window.onkeydown ' + String.fromCharCode(e.which))}
</script>
</head>
<body>
This page hijacks keypress events in order to display an alert message.
</body>
</html>

Comments

Soft Cheese

A play for two actors.

Act 1

A rustic village in Normandy. Candide, a recent émigré from the big city, has become quite fond of the rural lifestyle.

Candide enters the local cheese shop. Gaute, the store owner, looks up at the sound of the bell.

Gaute
Good morning, Candide! It’s so nice to welcome you again to my humble realm.
Candide
I inspected my garden this morning and some wonderful champignons matured nicely. Last week I ate mushrooms with Vieuxchatêl as per your suggestion, but I’m looking for a different type of local soft cheese today. Do you have any recommendations?
Gaute
Certainly, a new cheesemaker opened up shop in this area recently! They call it La Vache Qui Pleure. Would you like to sample some?
Candide
No thanks Gaute, that’s alright. Your judgment in these matters has been absolutely impeccable in these past few months, and whatever you recommend is always a culinary delight.
Gaute
Very well! That’ll be €3 please.
Candide
Here you are. Thank you so much!

Candide exits the store.

Act 2

The next morning, an angry Candide rushes into the store. The bell jingles violently.

Candide
Why didn’t you tell me this was processed cheese?! Why did you lie to me?
Gaute
Lied? But monsieur Candide, this is locally produced soft cheese, just like you asked for.
Candide
I don’t care if it’s technically soft and produced locally. You knew very well I wanted a real local cheese and you gave me this mass-produced junk.
Gaute
But the product I sold you already had artificial penicillium camemberti flavor added to it!
Candide
Artificial fungus flavor? Are you insane? You lied to me.
Gaute
You will quit making these libelous remarks! And besides, the cheese was never truly without fungus. You could have sprinkled some fungus on it yourself and it might’ve taken to the cheese.
Candide
You’re insane. This is no proper cheese.
Gaute
Sir, I will take you to court for libel and slander.
Candide
Do your worst. The truth is plain for all to see.

Act 3

A courtroom. Candide is down on his knees in front of a judge in court dress with his eyes toward the ground.

Judge
Are you Candide, the man who libeled against Gaute the cheese store owner?
Candide
I am Candide, but I did not libel against Gaute. He promised me a local soft cheese and he gave me mass-produced processed cheese.
Judge
But it was a local product. Your libelous lies make a mockery of this court.

Candide finally looks up at the judge. His face changes into a shocked expression of understanding.

Candide
You!
Gaute
In a small rural village such as this, I have to take on the role of judge, jury, and sometimes executioner.
Candide
This is a travesty of justice!
Gaute
The accused shall not yell in court.
Candide
You did not sell me proper cheese!
Gaute
The defendant is found guilty of libel. Gaute did sell cheese.
Candide
But proper cheese does not contain emulsifiers! It has the right fungus growing on the outside! It does not stay good for weeks while unrefrigerated!
Gaute
The defendant is also found guilty of trolling.
Candide
You cannot be serious. Until this very week you never sold me cheese with such artificial properties and additives. You sold me real cheese.
Gaute
The court finds itself obliged to forgive you for your ignorance of what is technically allowed to qualify as cheese. But by insisting on your own definition of this so-called proper cheese, you have proved yourself guilty of conceitedness. Bailiff, this man shall hang for the crime of being full of himself. Prepare the gallows.

Fade to black.

Comments (1)

Torn Apart

Here’s a silly “children’s story” I wrote around mid December. It’s a bit rough around the edges, probably a bit odd, and too graphical. So it goes.


It was a perfectly nice day when Esprit stumbled and broke his front right leg. When the little boy did not find the horse at his trough a few hours later he thought nothing of it; sometimes Esprit would run off and play and forget about dinner. To make up for lost time, the boy promised himself he’d visit the horse the next morning as early as he could.

The next day he noticed with mild consternation that the horse did not appear to be in his stable. The boy went out to search the property, trying to remember what were Esprit’s favorite spots. He remembered the open hill, where Esprit would often lie in the sun.

Time stood still when the little boy discovered what had happened. Whether running back to his house, alerting his parents, and calling the veterinarian helicopter emergency service took minutes or hours was not a question he’d be able to answer, but the sky was already turning red as a thunderous cavalcade of chopped air signaled the arrival of the helicopter. Esprit was harnessed in with great care so the broken leg wouldn’t shift, was given a sedative, and was swiftly lifted up. The little boy waved at the horse, certain the veterinarian would be able to mend and properly set Esprit’s leg.

Unbeknown to the helicopter pilot, distracted by the glint of the setting sun in her eye, the rescue pulley system malfunctioned and started lowering the horse just as the helicopter was starting to pick up speed. The little boy stared in wide-eyed horror as Esprit started bouncing left and right from tree to tree. Gapes and gashes appeared and started to bleed, and despite the sedative the poor horse woke up when its broken leg got stuck between the top branches of a particularly sturdy evergreen tree and was torn off like a cocktail pricker. The horse started to howl the most agonizing scream the little boy would ever hear, but that was not the end of Esprit’s misery. A particularly sharp branch a few trees over impaled the horse, which consequently produced a guttural, almost stuttering, and above all angry sound. The pilot finally noticed that something was awry when the helicopter refused to go forward anymore, but it was already too late. The surprisingly elastic tree finally gave up under the barrage of the helicopter’s brute force and snapped like a twig, propelling the helicopter on its now downward trajectory into the trees. Like in the Hollywood movies the little boy loved so much, the rotored machine made a squealing noise before exploding in a fiery ball of death.

The little boy was finally able to break out of his trance, and he collapsed into a sobbing mass. For months after he was bedridden, and each night he claimed Esprit came limping to his window while floating through the air, on three legs and a bloody stump, with mad, bloodshot eyes. The boy’s parents were worried sick, and their little boy’s German-accented psychiatrist was having the time of his life writing article after article about the little boy’s disturbed unconscious. When they found the broken window and the little boy’s dead body with the missing right arm, the autopsy report bluntly stated he had inflicted all these injuries on himself—that he had torn off his own arm. The hoofmarks all over his body were left out of the official report. The estate was up for sale the next day already, and no one’s lived there in sixty years. The locals are still weary of the place where the little boy died, but a real-estate developer drove by the other day and is planning to turn it into a hotel. The organization he represented put up a sign already: “The Hillcrest Hotel. Opening in May 2016, just in time for the holidays!”

Comments (1)Tags:

Data URI Bookmarklet

This is a repost of something I wrote on My Opera forums on 8 January 2013. The My Opera forums will be shutting down on 1 March 2014.


The easiest method [to base64 encode an image] might be to use the newly supported (since v12) FileReader object, which has a readAsDataURL method. I wrote a quick proof of concept using XHR because I’m not really sure how else to get data to it (aside from a file input which is no good here). Alternatively you could load the image in a canvas and call toDataURL, but then you’ll lose stuff like metadata and I’m not even sure if the compression and such will remain the same.

var xhr = new XMLHttpRequest(),
	reader = new FileReader();
xhr.open('GET', location.href, true);
xhr.responseType = 'blob';
xhr.addEventListener('load', function () {
	if (xhr.status === 200) {
		reader.addEventListener('load', function (e) {
			location.href = e.target.result;
		});
		var responseWithMimeType = new Blob(new Array(xhr.response), {
			'type': xhr.getResponseHeader('Content-Type')
		});
		reader.readAsDataURL(responseWithMimeType);
	}
});
xhr.send();

It does seem a bit roundabout, so I’d love to hear it if anyone has more efficient suggestions.

I just realized you may not be aware of how to make a bookmarklet. To save myself the effort of removing comments and such I used http://chris.zarate.org/bookmarkleter to obtain the following result:

javascript:(function(){var%20xhr=new%20XMLHttpRequest(),reader=new%20FileReader();xhr.open('GET',location.href,true);xhr.responseType='blob';xhr.addEventListener('load',function(){if(xhr.status===200){reader.addEventListener('load',function(e){location.href=e.target.result;});var%20responseWithMimeType=new%20Blob(new%20Array(xhr.response),{'type':xhr.getResponseHeader('Content-Type')});reader.readAsDataURL(responseWithMimeType);}});xhr.send();})();

Create a new bookmark, paste that into the URL and give it a nickname of your choice.

Easier still, just drag this or right-click, bookmark link: To Data URI. There might be some security restrictions on MyOpera against javascript links, so I’m not sure if it’ll work.

But for local files a simple page would do just fine. All you need is input type=”file” and a tiny bit of scripting similar to the above. Something like this should do:

<!DOCTYPE html>
<html>
<head>
<title>File to Data URL</title>
</head>
<body>

<form>
<input type="file">
</form>

<script>
var file = document.querySelector('input[type=file]'), reader = new FileReader();

file.addEventListener('change', function(e) {
	reader.readAsDataURL(e.target.files[0]);
});

reader.addEventListener('load', function(e) {
	location.href = e.target.result;
});
</script>

</body>
</html>

Comments