Tuesday, September 06, 2011

» FF6/FF7: Re-enable javascript: from urlbar +

Jump a bit for instructions on how to re-enable execution of javascript: urls in Firefox 6.0.1 and 7.0.

Background

So, much to my surprise, javascript: urls stopped working after I upgraded to FF6. I assumed it was a bug, and would be fixed soon enough. After all, "ReferenceError: alert is not defined" couldn't be intentional, right? I mean, javascript:alert(foo.prop); has worked since before javascript was even invented, amirite?

Not so. In bug 527530 it was considered necessary to remove execution of even typed-in javascript: urls in order to circumvent naive users being tricked into running malicious code. Chrome chose to address the social problem with a technical solution also--by disabling execution of drag-dropped and pasted javascript, but not removing the execution of typed-in javascript. Chrome also restores the urlbar after running the script. I have no idea what IE does, but the bug mentioned that it addresses the issue without removing typed javascript url execution.

I tend to think that it's a staple tool used by thousands daily, competing browsers support it, and I can't see a good reason why it shouldn't be available. Counter arguments that it takes fewer keystrokes to launch Web Console or Scratchpad miss the fact that not everyone has incorporated those tools into their workflows. Requiring developers to adopt completely new workflows to use Firefox is a Really Bad Idea(TM), especially if they've been productive with their current workflows for a long time. Further, that's a user interface design concern and shouldn't be rolled into the security concern. And as far as ui design goes, like it or not, besides javascript execution, the urlbar is also used to launch the download manager (from a user perspective, "paste this in your browser to download X" is completely different from "navigate to google"), to view internal information and state (about:*), and to launch the source viewer (view-source:). There are probably more I'm forgetting.

The only compelling argument for removing javscript execution from the urlbar completely is that users may still be tricked into pasting bad scripts, they'll just have to type a letter or two before hitting enter--"Check out the super secret awesome page! Paste this in your browser then press the Home key and type ja and press enter: vascript:badstuff()". Since the hypothetical naive user thinks the urlbar is only for urls, this could be a successful attack vector, but I'm not so sure it would be.

In any case, I'm perfectly fine with having a default-false boolean pref. This allows for developers to enable it, and it mitigates all the security concerns as it requires explicit opt-in. Bug 680302 is tracking adding such a pref.

Fixing Firefox 6.0.1 and 7.0

If you're like me and can't wait to turn urlbar execution of javascript back on, I'm including a backpatch to Firefox 6.0.1 and instructions to apply it.

Note: This is only tested on Linux, but should be identical on Windows and Mac, you'll just have to find where omni.jar is stored on those platforms and find tools un/zip it (it's a PKZIP format file), and to apply universal difference files.

Edit: This process works for FF7 release, you just need to adjust the paths and use something like 10 fuzziness when applying the patch (patch -p0 -F10 < ...).

All platforms

Go to about:config and right click, add a new boolean preference named browser.urlbar.allowInheritPrincipal and set it to true. Shutdown the browser.

Linux

mkdir ff6
cd ff6
wget -O ff_urlbar_javascript_pref.diff \
'http://sites.google.com/site/monkeesage/files/ff_urlbar_javascript_pref.diff?d=1'
unzip /usr/lib/firefox-6.0.1/omni.jar
patch -p0 < ff_urlbar_javascript_pref.diff
sudo rm /usr/lib/firefox-6.0.1/omni.jar
sudo zip -r /usr/lib/firefox-6.0.1/omni.jar .
cd ..
rm -rf ff6

Others

So first grab the patch: ff_urlbar_javascript_pref.diff

Make a directory and extract omni.jar into it.

Apply the patch (from inside the directory).

Create a new zip archive named omni.jar with the contents of the directory you extracted to, and move this over the original omni.zip. Look inside both before moving yours over the original, to make sure they have the same directory structure.

Restart Fixefox and you're back to being a normal, competent adult. :)

Labels: , , ,

Wednesday, April 06, 2011

» Thunderbird won't open links +

So after a recent system update (Arch Linux, cause it's the best, obviously. Duh, Winning), Thunderbird refused to open links in email bodies.

I would be like, "Please Mr. email client, open my links" and it would say "No, eff you, rawr-rawr-rawr" and then poop this on the error console:

Error: uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIExternalProtocolService.loadUrl]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: chrome://communicator/content/contentAreaClick.js :: openLinkExternally :: line 188" data: no]

Apparently, my default browser somehow got set to an explicit path for Firefox 3.6. When I upgraded to FF4, that obviously broke.

Followed the article linked below and everything works again. Yay!

http://kb.mozillazine.org/Default_browser

Labels: ,

Thursday, January 01, 2009

» Automatic YouTube download with IE7 +

So my mom likes to collect silly little clips from YouTube, and she uses IE7 on Windows XP (I've tried to get her to convert to Linux, but she's not very computer saavy and has learned computing with Windows—even Ubuntu is a bit too much for her to take in). She normally just copies the YouTube url, pastes it to KeepVid, followed by a right-click + save-as, pasting in the video title as the file name in the save dialog instead of the default "video.mp4" (or "video.flv"), and finally clicking "OK." That's a lot of work for what should be a simple operation.

I had some free time over the holidays, so I hacked up a little user script and Windows Scripting Host script to allow automatically downloading files from YouTube with IE7. Now you might be thinking something like: "That's lovely, but you could have just downloaded Firefox, installed GreaseMonkey and FlashGot, writen a user script to re-write YouTube urls to add &fmt=18, and used the FlashGot icon in the statusbar to download the video." That's all very true, but as I said, my mother is used to Windows and IE—it was just easier to KISS than to teach her a new technology (not to mention that she watches Netflix movies online, which requires IE—using two browsers for different things would thoroughly confuse her).

Firstly, you need to install the Trixie plugin for IE (get it here), which in turn requires .NET.

Next you need to place the following user script in C:\Program Files\Bhelpuri\Trixie\Scripts:

YouTube.user.js
// ==UserScript==
// @name Download YouTube videos
// @description Creates a link to download YouTube videos directly
// @namespace MonkeeSage@gmail.com
// @include *
// ==/UserScript==

(function()
{
// Trixie's @include directive seems broken using "*youtube.com/watch?*"
// so we manually filter the url here
if (location.href.indexOf("youtube.com/watch?") == -1)
return;

var match;
var node;
var videoId;
var tParam;
var videoTitle;

var videoUrl = "download://www.youtube.com/get_video?";
var videoTitleRe = new RegExp("YouTube - (.+)", "m");
var videoIdRe = new RegExp("video_id=([^&,\"]+)", "m");
var tParamRe = new RegExp("\"t\": \"([^&,\"]+)\"", "m");

try
{
match = document.title.match(videoTitleRe);
if (match) videoTitle = match[1];

match = document.body.innerHTML.match(videoIdRe);
if (match) videoId = match[1];

match = document.getElementsByTagName("head")[0].innerHTML.match(tParamRe);
if (match) tParam = match[1];
}
catch (ex)
{
dump(ex.message);
}

if (videoId && tParam)
{
videoUrl += "video_id=" + videoId + "&t=" + tParam + "&fmt=18#" +
"C:\\Documents and Settings\\mom\\Desktop\\My Music&" +
videoTitle + ".mp4";
a = document.createElement("a");
a.appendChild(document.createTextNode("Download video"));
a.href = videoUrl;
node = document.getElementById("watch-this-vid");
node.appendChild(a);
}

})();

Ps. You obviously need to change the C:\\Documents and Settings\\mom\\Desktop\\My Music to the default location you wish to save to.

Pss.You'll need to enable the script under Tools->Trixie Options (or Trixie Settings, or something similar—I'm going from memory here).

Next, you need to save the following file to C:\Program Files\Common Files\System:

wget.js
(function()
{
function BrowseForFolder(path, file)
{
var objDialog = new ActiveXObject("SAFRCFileDlg.FileSave");
var fso = new ActiveXObject("Scripting.FileSystemObject");

objDialog.FileName = fso.BuildPath(path, file);
objDialog.FileType = "MPEG4 Video";

var rv = objDialog.OpenFileSaveDlg();
if (rv)
{
return objDialog.FileName
}
else
{
return false;
}
}

function DownloadFile(url, file)
{
var data;
var ado;

try
{
var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");

// ResolveTimeout, ConnectTimeout, SendTimeout, ReceiveTimeout
WinHttpReq.SetTimeouts(30000, 30000, 30000, 5000);

void(WinHttpReq.Open("GET", url, false));
WinHttpReq.Send();
if (WinHttpReq.Status == 404)
{
return false;
}
data = WinHttpReq.ResponseBody;
}
catch (ex)
{
WScript.Echo("Error downloading file: " + ex.message);
return false;
}

ado = new ActiveXObject("ADODB.Stream");
ado.Type = 1; // binary mode
ado.Open();
ado.Write(data);
ado.SaveToFile(file, 2); // 2 = overwrite existing file
ado.Close();

return true;
}

function SetFileSuffix(filename, suffix)
{
var idx = filename.lastIndexOf(".");
if (idx > -1)
{
filename = filename.slice(0, idx);
}
return filename + suffix;
}

var url;
var file;
var path = "C:\\Documents and Settings\\mom\\Desktop\\My Music";
var filename = "foo";

var objArgs = WScript.Arguments;
if (objArgs.length > 0)
{
url = objArgs(0).split("#");
if (url.length == 2)
{
var tmp = url[1].split("&");
if (tmp.length > 0)
{
path = tmp[0];
}
if (tmp.length > 1)
{
filename = tmp[1];
}
}
url = url[0];
}
else
{
WScript.Echo("A URL argument is required");
return;
}

filename = SetFileSuffix(filename, ".mp4");

// replace the download:// pseudo-protocol with http://
url = "http" + url.slice(8);

file = BrowseForFolder(path, filename);
if (file)
{
var rv = DownloadFile(url, file);
if (!rv)
{
file = SetFileSuffix(file, ".flv");
url = url.replace("&fmt=18", "");
rv = DownloadFile(url, file);
}
if (rv)
{
var objWSH = new ActiveXObject("WScript.Shell");
objWSH.Popup("Finished downloading " + filename, 10, "Download Complete", 64);
}
}

})();

Finally, you need to add a new protocol handler to the registery to handle the download:// pseudo-protocol. Save and run the following regedit file:

YouTubeDL.reg
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\download]
@="URL:Download Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\download\shell]

[HKEY_CLASSES_ROOT\download\shell\open]

[HKEY_CLASSES_ROOT\download\shell\open\command]
@="C:\\WINDOWS\\system32\\wscript.exe \"C:\\Program Files\\Common Files\\System\\wget.js\" \"%1\""


Now restart IE, and you should be able to download YouTube videos from a link directly under the video (on the left side).

Caveat: There is no progress dialog and no way to cancel downloads. I could probably figure out a COM dialog to display the progress, and run the HTTP GET asychonously (and cancel the Send operation when requested), but my mom didn't need those features, and I'm lazy, so I didn't. ;P

Labels: , , ,