mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-21 22:37:58 +01:00
Automagically grab form charset in ;S.
This commit is contained in:
@@ -58,7 +58,7 @@ var Bookmarks = Module("bookmarks", {
|
|||||||
add: function add(unfiled, title, url, keyword, tags, force) {
|
add: function add(unfiled, title, url, keyword, tags, force) {
|
||||||
// FIXME
|
// FIXME
|
||||||
if (isObject(unfiled))
|
if (isObject(unfiled))
|
||||||
var { unfiled, title, url, keyword, tags, post, force } = unfiled;
|
var { unfiled, title, url, keyword, tags, post, charset, force } = unfiled;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let uri = util.createURI(url);
|
let uri = util.createURI(url);
|
||||||
@@ -82,13 +82,15 @@ var Bookmarks = Module("bookmarks", {
|
|||||||
if (!bmark)
|
if (!bmark)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if (charset !== undefined)
|
||||||
|
bmark.charset = charset;
|
||||||
if (post !== undefined)
|
if (post !== undefined)
|
||||||
bmark.post = post;
|
bmark.post = post;
|
||||||
if (keyword)
|
if (keyword)
|
||||||
bmark.keyword = keyword;
|
bmark.keyword = keyword;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
dactyl.log(e, 0);
|
util.reportError(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,10 +104,12 @@ var Bookmarks = Module("bookmarks", {
|
|||||||
* @param {Element} elem A form element for which to add a keyword.
|
* @param {Element} elem A form element for which to add a keyword.
|
||||||
*/
|
*/
|
||||||
addSearchKeyword: function (elem) {
|
addSearchKeyword: function (elem) {
|
||||||
let [url, post] = util.parseForm(elem);
|
let [url, post, charset] = util.parseForm(elem);
|
||||||
let options = { "-title": "Search " + elem.ownerDocument.title };
|
let options = { "-title": "Search " + elem.ownerDocument.title };
|
||||||
if (post != null)
|
if (post != null)
|
||||||
options["-post"] = post;
|
options["-post"] = post;
|
||||||
|
if (charset != null && charset !== "UTF-8")
|
||||||
|
options["-charset"] = charset;
|
||||||
|
|
||||||
commandline.open(":",
|
commandline.open(":",
|
||||||
commands.commandToString({ command: "bmark", options: options, arguments: [url] }) + " -keyword ",
|
commands.commandToString({ command: "bmark", options: options, arguments: [url] }) + " -keyword ",
|
||||||
@@ -134,7 +138,7 @@ var Bookmarks = Module("bookmarks", {
|
|||||||
let extra = "";
|
let extra = "";
|
||||||
if (title != url)
|
if (title != url)
|
||||||
extra = " (" + title + ")";
|
extra = " (" + title + ")";
|
||||||
this.add(true, title, url);
|
this.add({ unfiled: true, title: title, url: url });
|
||||||
dactyl.echomsg({ domains: [util.getHost(url)], message: "Added bookmark: " + url + extra });
|
dactyl.echomsg({ domains: [util.getHost(url)], message: "Added bookmark: " + url + extra });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -179,8 +183,10 @@ var Bookmarks = Module("bookmarks", {
|
|||||||
}
|
}
|
||||||
ids.forEach(function (id) {
|
ids.forEach(function (id) {
|
||||||
let bmark = bookmarkcache.bookmarks[id];
|
let bmark = bookmarkcache.bookmarks[id];
|
||||||
if (bmark)
|
if (bmark) {
|
||||||
PlacesUtils.tagging.untagURI(util.newURI(bmark.url), null);
|
PlacesUtils.tagging.untagURI(bmark.uri, null);
|
||||||
|
bmark.charset = null;
|
||||||
|
}
|
||||||
services.bookmarks.removeItem(id);
|
services.bookmarks.removeItem(id);
|
||||||
});
|
});
|
||||||
return ids.length;
|
return ids.length;
|
||||||
@@ -302,6 +308,7 @@ var Bookmarks = Module("bookmarks", {
|
|||||||
let [shortcutURL, postData] = PlacesUtils.getURLAndPostDataForKeyword(keyword);
|
let [shortcutURL, postData] = PlacesUtils.getURLAndPostDataForKeyword(keyword);
|
||||||
if (!shortcutURL)
|
if (!shortcutURL)
|
||||||
return [url, null];
|
return [url, null];
|
||||||
|
let bmark = bookmarkcache.keywords[keyword];
|
||||||
|
|
||||||
let data = window.unescape(postData || "");
|
let data = window.unescape(postData || "");
|
||||||
if (/%s/i.test(shortcutURL) || /%s/i.test(data)) {
|
if (/%s/i.test(shortcutURL) || /%s/i.test(data)) {
|
||||||
@@ -309,17 +316,16 @@ var Bookmarks = Module("bookmarks", {
|
|||||||
var matches = shortcutURL.match(/^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/);
|
var matches = shortcutURL.match(/^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/);
|
||||||
if (matches)
|
if (matches)
|
||||||
[, shortcutURL, charset] = matches;
|
[, shortcutURL, charset] = matches;
|
||||||
else {
|
else
|
||||||
try {
|
try {
|
||||||
charset = services.history.getCharsetForURI(util.newURI(shortcutURL));
|
charset = services.history.getCharsetForURI(util.newURI(shortcutURL));
|
||||||
}
|
}
|
||||||
catch (e) {}
|
catch (e) {}
|
||||||
}
|
|
||||||
var encodedParam;
|
|
||||||
if (charset)
|
if (charset)
|
||||||
encodedParam = escape(window.convertFromUnicode(charset, param));
|
var encodedParam = escape(window.convertFromUnicode(charset, param));
|
||||||
else
|
else
|
||||||
encodedParam = encodeURIComponent(param);
|
encodedParam = bmark.encodeURIComponent(param);
|
||||||
|
|
||||||
shortcutURL = shortcutURL.replace(/%s/g, encodedParam).replace(/%S/g, param);
|
shortcutURL = shortcutURL.replace(/%s/g, encodedParam).replace(/%S/g, param);
|
||||||
if (/%s/i.test(data))
|
if (/%s/i.test(data))
|
||||||
postData = window.getPostDataStream(data, param, encodedParam, "application/x-www-form-urlencoded");
|
postData = window.getPostDataStream(data, param, encodedParam, "application/x-www-form-urlencoded");
|
||||||
@@ -443,6 +449,7 @@ var Bookmarks = Module("bookmarks", {
|
|||||||
force: args.bang,
|
force: args.bang,
|
||||||
unfiled: false,
|
unfiled: false,
|
||||||
keyword: args["-keyword"] || null,
|
keyword: args["-keyword"] || null,
|
||||||
|
charset: args["-charset"],
|
||||||
post: args["-post"],
|
post: args["-post"],
|
||||||
tags: args["-tags"] || [],
|
tags: args["-tags"] || [],
|
||||||
title: args["-title"] || (args.length === 0 ? buffer.title : null),
|
title: args["-title"] || (args.length === 0 ? buffer.title : null),
|
||||||
@@ -470,7 +477,15 @@ var Bookmarks = Module("bookmarks", {
|
|||||||
}
|
}
|
||||||
completion.bookmark(context, args["-tags"], { keyword: args["-keyword"], title: args["-title"] });
|
completion.bookmark(context, args["-tags"], { keyword: args["-keyword"], title: args["-title"] });
|
||||||
},
|
},
|
||||||
options: [keyword, title, tags, post]
|
options: [keyword, title, tags, post,
|
||||||
|
{
|
||||||
|
names: ["-charset", "-c"],
|
||||||
|
description: "The character encoding of the bookmark",
|
||||||
|
type: CommandOption.STRING,
|
||||||
|
completer: function (context) completion.charset(context),
|
||||||
|
validator: Option.validateCompleter
|
||||||
|
}
|
||||||
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
commands.add(["bmarks"],
|
commands.add(["bmarks"],
|
||||||
@@ -547,14 +562,20 @@ var Bookmarks = Module("bookmarks", {
|
|||||||
let bmark = bmarks[0];
|
let bmark = bmarks[0];
|
||||||
|
|
||||||
options["-title"] = bmark.title;
|
options["-title"] = bmark.title;
|
||||||
|
if (bmark.charset)
|
||||||
|
options["-charset"] = bmark.charset;
|
||||||
if (bmark.keyword)
|
if (bmark.keyword)
|
||||||
options["-keyword"] = bmark.keyword;
|
options["-keyword"] = bmark.keyword;
|
||||||
|
if (bmark.post)
|
||||||
|
options["-post"] = bmark.post;
|
||||||
if (bmark.tags.length > 0)
|
if (bmark.tags.length > 0)
|
||||||
options["-tags"] = bmark.tags.join(", ");
|
options["-tags"] = bmark.tags.join(", ");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (buffer.title != buffer.URL.spec)
|
if (buffer.title != buffer.URL.spec)
|
||||||
options["-title"] = buffer.title;
|
options["-title"] = buffer.title;
|
||||||
|
if (content.document.characterSet !== "UTF-8")
|
||||||
|
options["-charset"] = content.document.characterSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
commandline.open(":",
|
commandline.open(":",
|
||||||
|
|||||||
@@ -903,7 +903,7 @@ var Commands = Module("commands", {
|
|||||||
|
|
||||||
// we have a validator function
|
// we have a validator function
|
||||||
if (typeof opt.validator == "function") {
|
if (typeof opt.validator == "function") {
|
||||||
if (opt.validator.call(this, arg, quoted) == false) {
|
if (opt.validator(arg, quoted) == false) {
|
||||||
fail("Invalid argument for option: " + optname);
|
fail("Invalid argument for option: " + optname);
|
||||||
if (complete) // Always true.
|
if (complete) // Always true.
|
||||||
complete.highlight(args.completeStart, count - 1, "SPELLCHECK");
|
complete.highlight(args.completeStart, count - 1, "SPELLCHECK");
|
||||||
|
|||||||
@@ -232,9 +232,8 @@ var StatusLine = Module("statusline", {
|
|||||||
else if (progress < 1) {
|
else if (progress < 1) {
|
||||||
progress = Math.floor(progress * 20);
|
progress = Math.floor(progress * 20);
|
||||||
progressStr = "["
|
progressStr = "["
|
||||||
+ "====================".substr(0, progress)
|
+ "===================> "
|
||||||
+ ">"
|
.substr(20 - progress, 20)
|
||||||
+ " ".substr(0, 19 - progress)
|
|
||||||
+ "]";
|
+ "]";
|
||||||
}
|
}
|
||||||
this.widgets.progress.value = progressStr;
|
this.widgets.progress.value = progressStr;
|
||||||
|
|||||||
@@ -10,14 +10,25 @@ defineModule("bookmarkcache", {
|
|||||||
require: ["services", "storage", "util"]
|
require: ["services", "storage", "util"]
|
||||||
});
|
});
|
||||||
|
|
||||||
var Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "id");
|
var Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "charset", "id");
|
||||||
var Keyword = Struct("keyword", "title", "icon", "url");
|
var Keyword = Struct("keyword", "title", "icon", "url");
|
||||||
Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
|
Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
|
||||||
|
update(Bookmark.prototype, {
|
||||||
|
get extra() [
|
||||||
|
["keyword", this.keyword, "Keyword"],
|
||||||
|
["tags", this.tags.join(", "), "Tag"]
|
||||||
|
].filter(function (item) item[1]),
|
||||||
|
|
||||||
|
get uri() util.newURI(this.url),
|
||||||
|
|
||||||
|
encodeURIComponent: function _encodeURIComponent(str) {
|
||||||
|
if (!this.charset || this.charset === "UTF-8")
|
||||||
|
return encodeURIComponent(str);
|
||||||
|
let conv = services.CharsetConv(this.charset);
|
||||||
|
return escape(conv.ConvertFromUnicode(str) + conv.Finish());
|
||||||
|
}
|
||||||
|
})
|
||||||
Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func);
|
Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func);
|
||||||
Bookmark.prototype.__defineGetter__("extra", function () [
|
|
||||||
["keyword", this.keyword, "Keyword"],
|
|
||||||
["tags", this.tags.join(", "), "Tag"]
|
|
||||||
].filter(function (item) item[1]));
|
|
||||||
Bookmark.setter("url", function (val) {
|
Bookmark.setter("url", function (val) {
|
||||||
let tags = this.tags;
|
let tags = this.tags;
|
||||||
this.tags = null;
|
this.tags = null;
|
||||||
@@ -26,6 +37,7 @@ Bookmark.setter("url", function (val) {
|
|||||||
});
|
});
|
||||||
Bookmark.setter("title", function (val) { services.bookmarks.setItemTitle(this.id, val); });
|
Bookmark.setter("title", function (val) { services.bookmarks.setItemTitle(this.id, val); });
|
||||||
Bookmark.setter("post", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.POST, val); });
|
Bookmark.setter("post", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.POST, val); });
|
||||||
|
Bookmark.setter("charset", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.CHARSET, val); });
|
||||||
Bookmark.setter("keyword", function (val) { services.bookmarks.setKeywordForBookmark(this.id, val); });
|
Bookmark.setter("keyword", function (val) { services.bookmarks.setKeywordForBookmark(this.id, val); });
|
||||||
Bookmark.setter("tags", function (val) {
|
Bookmark.setter("tags", function (val) {
|
||||||
services.tagging.untagURI(this.uri, null);
|
services.tagging.untagURI(this.uri, null);
|
||||||
@@ -37,6 +49,7 @@ var name = "bookmark-cache";
|
|||||||
|
|
||||||
var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
|
var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
|
||||||
POST: "bookmarkProperties/POSTData",
|
POST: "bookmarkProperties/POSTData",
|
||||||
|
CHARSET: "dactyl/charset",
|
||||||
|
|
||||||
init: function init() {
|
init: function init() {
|
||||||
services.bookmarks.addObserver(this, false);
|
services.bookmarks.addObserver(this, false);
|
||||||
@@ -68,12 +81,14 @@ var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
|
|||||||
let keyword = services.bookmarks.getKeywordForBookmark(node.itemId);
|
let keyword = services.bookmarks.getKeywordForBookmark(node.itemId);
|
||||||
let tags = services.tagging.getTagsForURI(uri, {}) || [];
|
let tags = services.tagging.getTagsForURI(uri, {}) || [];
|
||||||
let post = BookmarkCache.getAnnotation(node.itemId, this.POST);
|
let post = BookmarkCache.getAnnotation(node.itemId, this.POST);
|
||||||
return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, node.itemId);
|
let charset = BookmarkCache.getAnnotation(node.itemId, this.CHARSET);
|
||||||
|
return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, charset, node.itemId);
|
||||||
},
|
},
|
||||||
|
|
||||||
annotate: function (id, key, val) {
|
annotate: function (id, key, val, timespan) {
|
||||||
if (val)
|
if (val)
|
||||||
services.annotation.setItemAnnotation(id, key, val, 0, services.annotation.EXPIRE_NEVER);
|
services.annotation.setItemAnnotation(id, key, val, 0,
|
||||||
|
timespan || services.annotation.EXPIRE_NEVER);
|
||||||
else if (services.annotation.itemHasAnnotation(id, key))
|
else if (services.annotation.itemHasAnnotation(id, key))
|
||||||
services.annotation.removeItemAnnotation(id, key);
|
services.annotation.removeItemAnnotation(id, key);
|
||||||
},
|
},
|
||||||
@@ -159,7 +174,9 @@ var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
|
|||||||
onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
|
onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
|
||||||
if (isAnnotation)
|
if (isAnnotation)
|
||||||
if (property === this.POST)
|
if (property === this.POST)
|
||||||
[property, value] = ["post", BookmarkCache.getAnnotation(itemId, this.POST)];
|
[property, value] = ["post", BookmarkCache.getAnnotation(itemId, property)];
|
||||||
|
else if (property === this.CHARSET)
|
||||||
|
[property, value] = ["charset", BookmarkCache.getAnnotation(itemId, property)];
|
||||||
else
|
else
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
@@ -624,8 +624,8 @@ var JavaScript = Module("javascript", {
|
|||||||
"Uint16Array", "Uint32Array", "Uint8Array", "XML",
|
"Uint16Array", "Uint32Array", "Uint8Array", "XML",
|
||||||
"XMLHttpProgressEvent", "XMLList", "XMLSerializer", "XPCNativeWrapper",
|
"XMLHttpProgressEvent", "XMLList", "XMLSerializer", "XPCNativeWrapper",
|
||||||
"XPCSafeJSWrapper", "XULControllers", "decodeURI", "decodeURIComponent",
|
"XPCSafeJSWrapper", "XULControllers", "decodeURI", "decodeURIComponent",
|
||||||
"encodeURI", "encodeURIComponent", "eval", "isFinite", "isNaN",
|
"encodeURI", "encodeURIComponent", "escape", "eval", "isFinite", "isNaN",
|
||||||
"isXMLName", "parseFloat", "parseInt", "undefined", "uneval"
|
"isXMLName", "parseFloat", "parseInt", "undefined", "unescape", "uneval"
|
||||||
].concat([k.substr(6) for (k in keys(Ci)) if (/^nsIDOM/.test(k))])
|
].concat([k.substr(6) for (k in keys(Ci)) if (/^nsIDOM/.test(k))])
|
||||||
.concat([k.substr(3) for (k in keys(Ci)) if (/^nsI/.test(k))])
|
.concat([k.substr(3) for (k in keys(Ci)) if (/^nsI/.test(k))])
|
||||||
.concat(this.magicalNames)
|
.concat(this.magicalNames)
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ var Services = Module("Services", {
|
|||||||
this.add("windowWatcher", "@mozilla.org/embedcomp/window-watcher;1", Ci.nsIWindowWatcher);
|
this.add("windowWatcher", "@mozilla.org/embedcomp/window-watcher;1", Ci.nsIWindowWatcher);
|
||||||
this.add("zipReader", "@mozilla.org/libjar/zip-reader-cache;1", Ci.nsIZipReaderCache);
|
this.add("zipReader", "@mozilla.org/libjar/zip-reader-cache;1", Ci.nsIZipReaderCache);
|
||||||
|
|
||||||
|
this.addClass("CharsetConv", "@mozilla.org/intl/scriptableunicodeconverter", Ci.nsIScriptableUnicodeConverter, "charset");
|
||||||
this.addClass("File", "@mozilla.org/file/local;1", Ci.nsILocalFile);
|
this.addClass("File", "@mozilla.org/file/local;1", Ci.nsILocalFile);
|
||||||
this.addClass("file:", "@mozilla.org/network/protocol;1?name=file", Ci.nsIFileProtocolHandler);
|
this.addClass("file:", "@mozilla.org/network/protocol;1?name=file", Ci.nsIFileProtocolHandler);
|
||||||
this.addClass("Find", "@mozilla.org/embedcomp/rangefind;1", Ci.nsIFind);
|
this.addClass("Find", "@mozilla.org/embedcomp/rangefind;1", Ci.nsIFind);
|
||||||
|
|||||||
@@ -1070,21 +1070,35 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
|
|||||||
// Nuances gleaned from browser.jar/content/browser/browser.js
|
// Nuances gleaned from browser.jar/content/browser/browser.js
|
||||||
parseForm: function parseForm(field) {
|
parseForm: function parseForm(field) {
|
||||||
function encode(name, value, param) {
|
function encode(name, value, param) {
|
||||||
if (param)
|
param = param ? "%s" : "";
|
||||||
value = value + "%s";
|
if (post) // Seems wrong.
|
||||||
if (post)
|
return encodeComponent(name + "=" + value + param);
|
||||||
return name + "=" + value;
|
return encodeComponent(name) + "=" + encodeComponent(value) + param;
|
||||||
return encodeURIComponent(name) + "=" + (param ? value : encodeURIComponent(value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let form = field.form;
|
let form = field.form;
|
||||||
let doc = form.ownerDocument;
|
let doc = form.ownerDocument;
|
||||||
let charset = doc.charset;
|
|
||||||
|
let charset = doc.characterSet;
|
||||||
|
let converter = services.CharsetConv(charset);
|
||||||
|
for each (let cs in form.acceptCharset.split(/\s*,\s*|\s+/)) {
|
||||||
|
let c = services.CharsetConv(cs);
|
||||||
|
if (c) {
|
||||||
|
converter = services.CharsetConv(cs);
|
||||||
|
charset = cs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let uri = util.newURI(doc.baseURI.replace(/\?.*/, ""), charset);
|
let uri = util.newURI(doc.baseURI.replace(/\?.*/, ""), charset);
|
||||||
let url = util.newURI(form.action, charset, uri).spec;
|
let url = util.newURI(form.action, charset, uri).spec;
|
||||||
|
|
||||||
let post = form.method.toUpperCase() == "POST";
|
let post = form.method.toUpperCase() == "POST";
|
||||||
|
|
||||||
|
let encodeComponent = encodeURIComponent;
|
||||||
|
if (charset !== "UTF-8")
|
||||||
|
encodeComponent = function encodeComponent(str)
|
||||||
|
escape(converter.ConvertFromUnicode(str) + converter.Finish());
|
||||||
|
|
||||||
let elems = [];
|
let elems = [];
|
||||||
if (field instanceof Ci.nsIDOMHTMLInputElement && field.type == "submit")
|
if (field instanceof Ci.nsIDOMHTMLInputElement && field.type == "submit")
|
||||||
elems.push(encode(field.name, field.value));
|
elems.push(encode(field.name, field.value));
|
||||||
@@ -1101,8 +1115,8 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (post)
|
if (post)
|
||||||
return [url, elems.map(encodeURIComponent).join('&'), elems];
|
return [url, elems.join('&'), elems, charset];
|
||||||
return [url + "?" + elems.join('&'), null];
|
return [url + "?" + elems.join('&'), null, charset];
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user