Skip to content

Instantly share code, notes, and snippets.

@mooz
Created February 19, 2010 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mooz/308874 to your computer and use it in GitHub Desktop.
Save mooz/308874 to your computer and use it in GitHub Desktop.
(function () {
let encodings = [];
const Cc = Components.classes;
const Ci = Components.interfaces;
let RDF = Cc['@mozilla.org/rdf/rdf-service;1'].getService(Ci.nsIRDFService);
let RDFCU = Cc['@mozilla.org/rdf/container-utils;1'].getService(Ci.nsIRDFContainerUtils);
let cm = RDF.GetDataSource('rdf:charset-menu');
let sbService = Cc['@mozilla.org/intl/stringbundle;1'].getService(Ci.nsIStringBundleService);
let sbCharTitle = sbService.createBundle('chrome://global/locale/charsetTitles.properties');
let allEnum = cm.GetAllResources();
while (allEnum.hasMoreElements())
{
let res = allEnum.getNext().QueryInterface(Ci.nsIRDFResource);
let value = res.Value;
if (RDFCU.IsContainer(cm, res) || value.indexOf('charset.') === 0 || value.indexOf('----') === 0)
continue;
let label = sbCharTitle.GetStringFromName(value.toLowerCase() + '.title');
encodings.push([value, label]);
}
encodings = encodings.sort(function ([, a], [, b]) (a === b) ? 0 : (a > b) ? 1 : -1);
ext.add("set-encoding", function (ev, arg) {
let index = 0;
let collection;
if (!arg)
{
let (prefix = 'charset.')
collection = Array.slice(document.getElementById("charsetMenu").getElementsByTagName("menuitem"))
.filter(function (n) !n.id.indexOf(prefix))
.map(function (n) [n.id.slice(prefix.length), n.label]);
}
if (!collection || !collection.length)
collection = encodings;
let (current = getBrowser().docShell.QueryInterface(Ci.nsIDocCharset).charset.toLowerCase())
collection.some(function ([v, l], i) (v.toLowerCase() === current) ? (index = i, true) : false);
prompt.selector(
{
message : M({ja: "文字エンコーディングの選択:", en: "Select charset:"}),
collection : collection,
initialIndex : index,
flags : [0, 0],
callback : function (i) {
if (i < 0)
return;
let charset = collection[i][0];
["SetForcedCharset", "SetDefaultCharacterSet", "BrowserSetForcedCharacterSet"].forEach(
function (n) { if (n in window) window[n](charset); }
);
}
}
);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment