Skip to content

Commit

Permalink
Fetch favicon for each search engine in Omnibar, and add switch for
Browse files Browse the repository at this point in the history
search alias in basic settings.
  • Loading branch information
brookhong committed Feb 3, 2022
1 parent 3d1de8c commit 0a608e3
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 53 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[![Node CI](https://github.com/brookhong/Surfingkeys/workflows/Node%20CI/badge.svg?branch=master)](https://github.com/brookhong/Surfingkeys/actions?query=workflow%3A%22Node+CI%22+branch%3Amaster)

Surfingkeys is another web browser(including Google Chrome, Chromium based browsers, Firefox, Safari) extension that provides keyboard-based navigation and control of the web in the spirit of the VIM editor. But it's not for VIM users only, it's for anyone who just needs some more shortcuts to his own functions.
Surfingkeys is another web browser(including Google Chrome, Chromium based browsers, Firefox, Safari) extension that provides keyboard-based navigation and control of the web in the spirit of the VIM editor. But it's not for VIM users only, it's for anyone who just needs some more shortcuts to his/her own functions.

Surfingkeys is created with all settings described in Javascript, so it's easy for anyone to map any keystrokes to his own defined Javascript function. For example,
Surfingkeys is created with all settings described in Javascript, so it's easy for anyone to map any keystrokes to his/her own defined Javascript function. For example,

mapkey('<Ctrl-y>', 'Show me the money', function() {
Front.showPopup('a well-known phrase uttered by characters in the 1996 film Jerry Maguire (Escape to close).');
Expand Down
1 change: 1 addition & 0 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function modifyManifest(browser, mode, buffer) {
manifest.permissions.push("cookies",
"contextualIdentities");
} else {
manifest.permissions.push("proxy");
manifest.permissions.push("tts");
manifest.permissions.push("downloads.shelf");
manifest.background.persistent = false;
Expand Down
16 changes: 16 additions & 0 deletions src/background/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,22 @@ function start(browser) {
});
}, message.headers, message.data);
};
self.requestImage = function(message, sender, sendResponse) {
const img = document.createElement("img");
img.src = message.url;
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.height = img.naturalHeight;
canvas.width = img.naturalWidth;
const ctx = canvas.getContext('2d');

ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
_response(message, sendResponse, {
text: canvas.toDataURL()
});
};

};
self.nextFrame = function(message, sender, sendResponse) {
var tid = sender.tab.id;
chrome.tabs.executeScript(tid, {
Expand Down
6 changes: 3 additions & 3 deletions src/content_scripts/common/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,18 @@ module.exports = function(api) {
return r.phrase;
});
});
addSearchAlias('b', 'baidu', 'https://www.baidu.com/s?wd=', 's', 'http://suggestion.baidu.com/su?cb=&wd=', function(response) {
addSearchAlias('b', 'baidu', 'https://www.baidu.com/s?wd=', 's', 'https://suggestion.baidu.com/su?cb=&wd=', function(response) {
var res = response.text.match(/,s:\[("[^\]]+")]}/);
return res ? res[1].replace(/"/g, '').split(",") : [];
});
addSearchAlias('e', 'wikipedia', 'https://en.wikipedia.org/wiki/', 's', 'https://en.wikipedia.org/w/api.php?action=opensearch&format=json&formatversion=2&namespace=0&limit=40&search=', function(response) {
return JSON.parse(response.text)[1];
});
addSearchAlias('w', 'bing', 'http://global.bing.com/search?setmkt=en-us&setlang=en-us&q=', 's', 'http://api.bing.com/osjson.aspx?query=', function(response) {
addSearchAlias('w', 'bing', 'https://www.bing.com/search?setmkt=en-us&setlang=en-us&q=', 's', 'https://api.bing.com/osjson.aspx?query=', function(response) {
var res = JSON.parse(response.text);
return res[1];
});
addSearchAlias('s', 'stackoverflow', 'http://stackoverflow.com/search?q=');
addSearchAlias('s', 'stackoverflow', 'https://stackoverflow.com/search?q=');
addSearchAlias('h', 'github', 'https://github.com/search?q=', 's', 'https://api.github.com/search/repositories?order=desc&q=', function(response) {
var res = JSON.parse(response.text)['items'];
return res ? res.map(function(r){
Expand Down
16 changes: 13 additions & 3 deletions src/content_scripts/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ function applySettings(api, normal, rs) {
if ('findHistory' in rs) {
runtime.conf.lastQuery = rs.findHistory.length ? rs.findHistory[0] : "";
}
if (!rs.showAdvanced && rs.basicMappings) {
applyBasicMappings(api, normal, rs.basicMappings);
if (!rs.showAdvanced) {
if (rs.basicMappings) {
applyBasicMappings(api, normal, rs.basicMappings);
}
if (rs.disabledSearchAliases) {
for (const key in rs.disabledSearchAliases) {
api.removeSearchAlias(key);
}
}
}
if (rs.showAdvanced && 'snippets' in rs && rs.snippets && !isInUIFrame()) {
var delta = runScript(api, rs.snippets);
Expand Down Expand Up @@ -144,12 +151,15 @@ function _initModules() {

const api = createAPI(clipboard, insert, normal, hints, visual, front, _browser);
createDefaultMappings(api);
if (typeof(_browser.plugin) === "function") {
_browser.plugin({ front });
}

dispatchSKEvent('defaultSettingsLoaded', {normal, api});
RUNTIME('getSettings', null, function(response) {
var rs = response.settings;
applySettings(api, normal, rs);
dispatchSKEvent('userSettingsLoaded', {settings: rs, api});
dispatchSKEvent('userSettingsLoaded', {settings: rs, api, front});
});
return {
normal,
Expand Down
44 changes: 22 additions & 22 deletions src/content_scripts/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createFront(insert, normal, hints, visual, browser) {
var _uiUserSettings = [];
function applyUserSettings() {
for (var cmd of _uiUserSettings) {
frontendCommand(cmd);
self.command(cmd);
}
}

Expand All @@ -36,7 +36,7 @@ function createFront(insert, normal, hints, visual, browser) {
});

var _callbacks = {};
function frontendCommand(args, successById) {
self.command = function(args, successById) {
args.commandToFrontend = true;
args.origin = getDocumentOrigin();
args.id = generateQuickGuid();
Expand All @@ -47,7 +47,7 @@ function createFront(insert, normal, hints, visual, browser) {
frontendPromise.then(function() {
runtime.postTopMessage(args);
});
}
};

document.addEventListener("surfingkeys:setUserSettings", function(evt) {
_uiUserSettings.push({
Expand Down Expand Up @@ -117,7 +117,7 @@ function createFront(insert, normal, hints, visual, browser) {
};

self.executeCommand = function (cmd) {
frontendCommand({
self.command({
action: 'executeCommand',
cmdline: cmd
});
Expand Down Expand Up @@ -175,14 +175,14 @@ function createFront(insert, normal, hints, visual, browser) {
}

self.showUsage = function() {
frontendCommand({
self.command({
action: 'showUsage',
metas: getAllAnnotations()
});
};

self.getUsage = function(cb) {
frontendCommand({
self.command({
action: 'getUsage',
metas: getAllAnnotations()
}, function(response) {
Expand All @@ -192,14 +192,14 @@ function createFront(insert, normal, hints, visual, browser) {

document.addEventListener("surfingkeys:showPopup", function(evt) {
const [ content ] = evt.detail;
frontendCommand({
self.command({
action: 'showPopup',
content: content
});
});

function hidePopup() {
frontendCommand({
self.command({
action: 'hidePopup'
});
}
Expand Down Expand Up @@ -279,14 +279,14 @@ function createFront(insert, normal, hints, visual, browser) {
if (useNeovim || runtime.conf.useNeovim) {
cmd.file_name = `${new URL(window.location.origin).host}/${elementBehindEditor.nodeName.toLowerCase()}`;
}
frontendCommand(cmd);
self.command(cmd);
};

self.chooseTab = function() {
if (normal.repeats !== "") {
RUNTIME('focusTabByIndex');
} else {
frontendCommand({
self.command({
action: 'chooseTab'
});
}
Expand Down Expand Up @@ -319,7 +319,7 @@ function createFront(insert, normal, hints, visual, browser) {
*/
self.openOmnibar = function(args) {
args.action = 'openOmnibar';
frontendCommand(args);
self.command(args);
};

var _inlineQuery;
Expand Down Expand Up @@ -385,14 +385,14 @@ function createFront(insert, normal, hints, visual, browser) {
};

document.addEventListener("surfingkeys:openFinder", function(evt) {
frontendCommand({
self.command({
action: "openFinder"
});
});

document.addEventListener("surfingkeys:showBanner", function(evt) {
const [ msg, linger_time ] = evt.detail;
frontendCommand({
self.command({
action: "showBanner",
content: msg,
linger_time: linger_time
Expand All @@ -410,7 +410,7 @@ function createFront(insert, normal, hints, visual, browser) {
pos.winX = window.frameElement.offsetLeft;
pos.winY = window.frameElement.offsetTop;
}
frontendCommand({
self.command({
action: "showBubble",
content: msg,
position: pos,
Expand All @@ -420,7 +420,7 @@ function createFront(insert, normal, hints, visual, browser) {
});

document.addEventListener("surfingkeys:hideBubble", function(evt) {
frontendCommand({
self.command({
action: 'hideBubble'
});
});
Expand All @@ -434,7 +434,7 @@ function createFront(insert, normal, hints, visual, browser) {
document.addEventListener("surfingkeys:hideKeystroke", function(evt) {
_keyHints.accumulated = "";
_keyHints.candidates = {};
frontendCommand({
self.command({
action: 'hideKeystroke'
});
});
Expand All @@ -456,7 +456,7 @@ function createFront(insert, normal, hints, visual, browser) {
});
}

frontendCommand({
self.command({
action: 'showKeystroke',
keyHints: _keyHints
});
Expand All @@ -467,15 +467,15 @@ function createFront(insert, normal, hints, visual, browser) {
});

self.showStatus = function (pos, msg, duration) {
frontendCommand({
self.command({
action: "showStatus",
content: msg,
duration: duration,
position: pos
});
};
self.toggleStatus = function (visible) {
frontendCommand({
self.command({
action: "toggleStatus",
visible: visible
});
Expand Down Expand Up @@ -523,7 +523,7 @@ function createFront(insert, normal, hints, visual, browser) {
}
}

frontendCommand({
self.command({
action: 'updateOmnibarResult',
words: queryResult
});
Expand All @@ -534,7 +534,7 @@ function createFront(insert, normal, hints, visual, browser) {
RUNTIME('executeScript', {
code: message.cmdline
}, function (response) {
frontendCommand({
self.command({
action: 'updateOmnibarResult',
words: response.response
});
Expand Down Expand Up @@ -565,7 +565,7 @@ function createFront(insert, normal, hints, visual, browser) {
clearPendingQuery();
_pendingQuery = setTimeout(function() {
visual.visualUpdate(message.query);
frontendCommand({
self.command({
action: "visualUpdated"
});
}, 500);
Expand Down
60 changes: 56 additions & 4 deletions src/content_scripts/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function createMappingEditor(elmId) {
}

if (getBrowserName() === "Firefox") {
document.querySelector("#localPathForSettings").style.display = "none";
document.querySelector("#localPathForSettings").style.display = "";
document.querySelector("#proxySettings").style.display = "none";
} else if (getBrowserName().startsWith("Safari")) {
document.querySelector("#localPathForSettings").style.display = "none";
Expand Down Expand Up @@ -221,16 +221,17 @@ function _updateProxy(data) {
});
}

var basicSettingsDiv = document.getElementById("basicSettings");
var basicMappingsDiv = document.getElementById("basicMappings");
var advancedSettingDiv = document.getElementById("advancedSetting");
var advancedTogglerDiv = document.getElementById("advancedToggler");
function showAdvanced(flag) {
if (flag) {
basicMappingsDiv.hide();
basicSettingsDiv.hide();
advancedSettingDiv.show();
advancedTogglerDiv.setAttribute('checked', 'checked');
} else {
basicMappingsDiv.show();
basicSettingsDiv.show();
advancedSettingDiv.hide();
advancedTogglerDiv.removeAttribute('checked');
}
Expand Down Expand Up @@ -351,6 +352,55 @@ document.addEventListener("surfingkeys:defaultSettingsLoaded", function(evt) {
}).filter((m) => m !== null);;
});

function renderSearchAlias(front, disabledSearchAliases) {
const aliasesPromise = new Promise((r, j) => {
const getSearchAliases = () => {
front.command({
action: 'getSearchAliases'
}, function(response) {
if (Object.keys(response.aliases).length > 0) {
r(response.aliases);
} else {
setTimeout(getSearchAliases, 300);
}
});
};
getSearchAliases();
});
aliasesPromise.then((aliases) => {
const allAliases = {};
for (const key in aliases) {
let prompt = aliases[key].prompt;
if (!prompt.startsWith("<img src=")) {
prompt = prompt.replace(/<span class='separator'>.*/, '');
}
allAliases[key] = { prompt, checked: "checked" };
}
for (const key in disabledSearchAliases) {
allAliases[key] = { prompt: disabledSearchAliases[key], checked: "" };
}
for (const key in allAliases) {
const { prompt, checked } = allAliases[key];
const elm = createElementWithContent("div", `<div class='remove'><input type="checkbox" ${checked} /></div><span class='prompt'>${prompt}</span>`);
document.querySelector("#searchAliases").appendChild(elm);

elm.querySelector("input").onchange = () => {
if (disabledSearchAliases.hasOwnProperty(key)) {
delete disabledSearchAliases[key];
} else {
disabledSearchAliases[key] = prompt;
}

RUNTIME('updateSettings', {
settings: {
disabledSearchAliases
}
});
};
}
});
}

function renderKeyMappings(rs) {
initL10n(function (locale) {
var customization = basicMappings.map(function (w, i) {
Expand All @@ -374,7 +424,9 @@ function renderKeyMappings(rs) {
}

document.addEventListener("surfingkeys:userSettingsLoaded", function(evt) {
renderKeyMappings(evt.detail.settings);
const { settings, front } = evt.detail;
renderSearchAlias(front, settings.disabledSearchAliases || {});
renderKeyMappings(settings);
});

var KeyPicker = (function() {
Expand Down
Loading

2 comments on commit 0a608e3

@patwickdane
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this makes it impossible for us to override/manually set favicons for each search engine. Is this intentional?

@b0o
Copy link
Collaborator

@b0o b0o commented on 0a608e3 Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pdanem The capability to override favicons was added in #1657 (I don't think it's been released yet on the extension stores, though?)

Please sign in to comment.