mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-01-07 03:24:11 +01:00
added search suggestions (thanks to M.Terada)
This commit is contained in:
@@ -178,20 +178,46 @@ liberator.Completion = function () //{{{
|
||||
return [0, buildLongestCommonSubstring(mapped, filter)];
|
||||
},
|
||||
|
||||
googleSuggest: function(filter)
|
||||
searchEngineSuggest: function(filter, engineAliases)
|
||||
{
|
||||
const endPoint = "http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=" +
|
||||
liberator.options.getPref("font.language.group", "en") + "&qu=";
|
||||
var xhr = new XMLHttpRequest();
|
||||
var completions = [];
|
||||
if (!filter)
|
||||
return [0,null];
|
||||
|
||||
xhr.open("GET", endPoint + encodeURIComponent(filter), false);
|
||||
xhr.send(null);
|
||||
var response = window.eval(xhr.responseText)[1];
|
||||
var engineList = (engineAliases || liberator.options["suggestengines"]).split(",");
|
||||
var responseType = "application/x-suggestions+json";
|
||||
var ss = Components.classes["@mozilla.org/browser/search-service;1"]
|
||||
.getService(Components.interfaces.nsIBrowserSearchService);
|
||||
|
||||
for each (var item in response)
|
||||
completions.push([item, "Google Suggestion"]);
|
||||
return [0, completions];
|
||||
var completions = [];
|
||||
engineList.forEach (function(name)
|
||||
{
|
||||
var query = filter;
|
||||
var queryURI;
|
||||
var engine = ss.getEngineByAlias(name);
|
||||
var reg = new RegExp("^\s*(" + name + "\\s+)(.*)$");
|
||||
var matches = query.match(reg);
|
||||
if (matches)
|
||||
query = matches[2];
|
||||
|
||||
if (engine && engine.supportsResponseType(responseType))
|
||||
queryURI = engine.getSubmission(query, responseType).uri.asciiSpec;
|
||||
else
|
||||
return;
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", queryURI, false);
|
||||
xhr.send(null);
|
||||
var results = window.eval(xhr.responseText)[1];
|
||||
if (!results)
|
||||
return;
|
||||
|
||||
results.forEach(function(item)
|
||||
{
|
||||
completions.push([(matches ? matches[1] : "") + item, name + " suggestion"]);
|
||||
});
|
||||
});
|
||||
|
||||
return [0, completions];
|
||||
},
|
||||
|
||||
// filter a list of urls
|
||||
@@ -213,6 +239,7 @@ liberator.Completion = function () //{{{
|
||||
}
|
||||
|
||||
var cpt = complete || liberator.options["complete"];
|
||||
var suggestEngineAlias = liberator.options["suggestengines"] || "google";
|
||||
// join all completion arrays together
|
||||
for (var i = 0; i < cpt.length; i++)
|
||||
{
|
||||
@@ -224,8 +251,8 @@ liberator.Completion = function () //{{{
|
||||
completions = completions.concat(liberator.bookmarks.get(filter));
|
||||
else if (cpt[i] == "h")
|
||||
completions = completions.concat(liberator.history.get(filter));
|
||||
else if (cpt[i] == "g")
|
||||
completions = completions.concat(this.googleSuggest(filter)[1]);
|
||||
else if (cpt[i] == "S")
|
||||
completions = completions.concat(this.searchEngineSuggest(filter, suggestEngineAlias)[1]);
|
||||
}
|
||||
|
||||
return [start, completions];
|
||||
|
||||
@@ -476,7 +476,10 @@ liberator.Events = function () //{{{
|
||||
dump("waited: " + (now - then) + " ms\n");
|
||||
|
||||
if (liberator.buffer.loaded > 0)
|
||||
{
|
||||
liberator.sleep(250);
|
||||
break;
|
||||
}
|
||||
else
|
||||
liberator.echo("Waiting for page to load...");
|
||||
}
|
||||
@@ -488,6 +491,10 @@ liberator.Events = function () //{{{
|
||||
liberator.echoerr("Page did not load completely in " + ms + " milliseconds. Macro stopped.");
|
||||
dump("done waiting: " + ret + "\n");
|
||||
|
||||
// sometimes the input widget had focus when replaying a macro
|
||||
// maybe this call should be moved somewhere else?
|
||||
// liberator.focusContent(true);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -784,8 +791,13 @@ liberator.Events = function () //{{{
|
||||
evt.noremap = noremap;
|
||||
elem.dispatchEvent(evt);
|
||||
// stop feeding keys if page loading failed
|
||||
if (liberator.modes.isReplaying && !waitForPageLoaded())
|
||||
return;
|
||||
if (liberator.modes.isReplaying)
|
||||
{
|
||||
if (!waitForPageLoaded())
|
||||
return;
|
||||
// else // a short break between keys often helps
|
||||
// liberator.sleep(50);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
@@ -286,9 +286,25 @@ liberator.CommandLine = function () //{{{
|
||||
"Items which are completed at the :[tab]open prompt",
|
||||
"charlist", "sfbh",
|
||||
{
|
||||
validator: function (value) { return !/[^sfbhg]/.test(value); }
|
||||
validator: function (value) { return !/[^sfbhS]/.test(value); }
|
||||
});
|
||||
|
||||
liberator.options.add(["suggestengines"],
|
||||
"Engine Alias which has a feature of suggest",
|
||||
"stringlist", "google",
|
||||
{
|
||||
validator: function (value)
|
||||
{
|
||||
var ss = Components.classes["@mozilla.org/browser/search-service;1"]
|
||||
.getService(Components.interfaces.nsIBrowserSearchService);
|
||||
return value.split(",").every(function (item)
|
||||
{
|
||||
var e = ss.getEngineByAlias(item);
|
||||
return (e && e.supportsResponseType("application/x-suggestions+json")) ? true : false;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
liberator.options.add(["showmode", "smd"],
|
||||
"Show the current mode in the command line",
|
||||
"boolean", true);
|
||||
|
||||
Reference in New Issue
Block a user