Skip to content

Commit

Permalink
[mv2] load user script directly without remote stub
Browse files Browse the repository at this point in the history
  • Loading branch information
brookhong committed Nov 16, 2024
1 parent 56ccb7f commit 5db21d4
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 84 deletions.
2 changes: 1 addition & 1 deletion config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ module.exports = (env, argv) => {
};
const moduleEntries = {
'pages/options': './src/content_scripts/options.js',
'api': './src/user_scripts/index.js'
};
const pagesCopyOptions = {
ignore: [
Expand All @@ -100,6 +99,7 @@ module.exports = (env, argv) => {
entry['pages/neovim'] = './src/pages/neovim.js';
entry['pages/pdf_viewer'] = './src/content_scripts/pdf_viewer.js';
moduleEntries['pages/neovim_lib'] = './src/nvim/renderer.ts';
moduleEntries['api'] = './src/user_scripts/index.js';
}
if (browser !== "safari") {
entry['pages/markdown'] = './src/content_scripts/markdown.js';
Expand Down
52 changes: 48 additions & 4 deletions src/content_scripts/common/api.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { dispatchSKEvent } from './runtime.js';
import { RUNTIME, dispatchSKEvent } from './runtime.js';
import Trie from './trie';
import Mode from './mode';
import KeyboardUtils from './keyboardUtils';
import {
LOG,
} from '../../common/utils.js';
import {
aceVimMap,
addVimMapKey,
constructSearchURL,
getBrowserName,
getClickableElements,
initSKFunctionListener,
isElementPartiallyInViewport,
isInUIFrame,
mapInMode,
parseAnnotation,
showBanner,
showPopup,
tabOpenLink,
} from './utils.js';

Expand Down Expand Up @@ -454,16 +461,53 @@ function createAPI(clipboard, insert, normal, hints, visual, front, browser) {
},
});
return {
Clipboard: clipboard,
RUNTIME,
aceVimMap,
addVimMapKey,
addSearchAlias,
cmap,
imap,
imapkey,
isElementPartiallyInViewport,
getBrowserName,
getClickableElements,
lmap,
map,
unmap,
unmapAllExcept,
iunmap,
vunmap,
mapkey,
imapkey,
readText: browser.readText,
vmapkey,
removeSearchAlias,
searchSelectedWith,
tabOpenLink,
vmap,
vmapkey,
Clipboard: clipboard,
Normal: {
feedkeys: normal.feedkeys,
jumpVIMark: normal.jumpVIMark,
passThrough: normal.passThrough,
scroll: normal.scroll,
},
Hints: {
click: hints.click,
create: hints.create,
dispatchMouseClick: hints.dispatchMouseClick,
style: hints.style,
setNumeric: hints.setNumeric,
setCharacters: hints.setCharacters,
},
Visual: {
style: visual.style,
},
Front: {
openOmnibar: front.openOmnibar,
registerInlineQuery: front.registerInlineQuery,
showBanner,
showPopup,
},
};
}

Expand Down
67 changes: 67 additions & 0 deletions src/content_scripts/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,70 @@ import * as DOMPurify from 'dompurify';
import KeyboardUtils from './keyboardUtils';
import { RUNTIME, dispatchSKEvent, runtime } from './runtime.js';

/**
* Map the key sequence `lhs` to `rhs` for mode `ctx` in ACE editor.
*
* @param {string} lhs a key sequence to replace
* @param {string} rhs a key sequence to be replaced
* @param {string} ctx a mode such as `insert`, `normal`.
*
* @example aceVimMap('J', ':bn', 'normal');
*/
function aceVimMap(lhs, rhs, ctx) {
dispatchSKEvent("front", ['addVimMap', lhs, rhs, ctx]);
}

/**
* Add map key in ACE editor.
*
* @param {object} objects multiple objects to define key map in ACE, see more from [ace/keyboard/vim.js](https://github.com/ajaxorg/ace/blob/ec450c03b51aba3724cf90bb133708078d1f3de6/lib/ace/keyboard/vim.js#L927-L1099)
*
* @example
* addVimMapKey(
* {
* keys: 'n',
* type: 'motion',
* motion: 'moveByCharacters',
* motionArgs: {
* forward: false
* }
* },
*
* {
* keys: 'e',
* type: 'motion',
* motion: 'moveByLines',
* motionArgs: {
* forward: true,
* linewise: true
* }
* }
* );
*/
function addVimMapKey() {
dispatchSKEvent("front", ['addVimKeyMap', Array.from(arguments)]);
}

function isEmptyObject(obj) {
for (var name in obj) {
return false;
}
return true;
}

function applyUserSettings(delta) {
if (delta.error !== "") {
if (window === top) {
showPopup("[SurfingKeys] Error found in settings: " + delta.error);
} else {
console.log("[SurfingKeys] Error found in settings({0}): {1}".format(window.location.href, delta.error));
}
}
if (!isEmptyObject(delta.settings)) {
dispatchSKEvent("front", ['applySettingsFromSnippets', delta.settings]);
}
}

/**
* Get current browser name
* @returns {string} "Chrome" | "Firefox" | "Safari"
Expand Down Expand Up @@ -862,7 +926,10 @@ function attachFaviconToImgSrc(tab, imgEl) {
}

export {
aceVimMap,
actionWithSelectionPreserved,
addVimMapKey,
applyUserSettings,
attachFaviconToImgSrc,
constructSearchURL,
createElementWithContent,
Expand Down
26 changes: 12 additions & 14 deletions src/content_scripts/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import createVisual from './common/visual.js';
import createHints from './common/hints.js';
import createClipboard from './common/clipboard.js';
import {
generateQuickGuid,
getRealEdit,
isInUIFrame,

applyUserSettings,
createElementWithContent,
generateQuickGuid,
getBrowserName,
getRealEdit,
htmlEncode,
initL10n,
isInUIFrame,
reportIssue,
setSanitizedContent,
showBanner,
Expand Down Expand Up @@ -117,16 +117,14 @@ function applySettings(api, normal, rs) {
api.removeSearchAlias(key);
}
}
} else if (!rs.isMV3) {
import(/* webpackIgnore: true */ chrome.runtime.getURL("/api.js")).then((module) => {
module.default(chrome.runtime.getURL("/"), (api, settings) => {
try {
(new Function('settings', 'api', rs.snippets))(settings, api);
} catch (e) {
showBanner(e.toString(), 3000);
}
});
});
} else if (!rs.isMV3 && rs.snippets && !document.location.href.startsWith(chrome.runtime.getURL("/"))) {
var settings = {}, error = "";
try {
(new Function('settings', 'api', rs.snippets))(settings, api);
} catch (e) {
error = e.toString();
}
applyUserSettings({settings, error});
}

applyRuntimeConf(normal);
Expand Down
69 changes: 4 additions & 65 deletions src/user_scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { RUNTIME, dispatchSKEvent } from '../content_scripts/common/runtime.js';
import {
httpRequest,
aceVimMap,
addVimMapKey,
applyUserSettings,
getBrowserName,
getClickableElements,
httpRequest,
initSKFunctionListener,
isElementPartiallyInViewport,
showBanner,
Expand All @@ -25,50 +28,6 @@ function isInUIFrame() {
}
}

/**
* Map the key sequence `lhs` to `rhs` for mode `ctx` in ACE editor.
*
* @param {string} lhs a key sequence to replace
* @param {string} rhs a key sequence to be replaced
* @param {string} ctx a mode such as `insert`, `normal`.
*
* @example aceVimMap('J', ':bn', 'normal');
*/
function aceVimMap(lhs, rhs, ctx) {
dispatchSKEvent("front", ['addVimMap', lhs, rhs, ctx]);
}

/**
* Add map key in ACE editor.
*
* @param {object} objects multiple objects to define key map in ACE, see more from [ace/keyboard/vim.js](https://github.com/ajaxorg/ace/blob/ec450c03b51aba3724cf90bb133708078d1f3de6/lib/ace/keyboard/vim.js#L927-L1099)
*
* @example
* addVimMapKey(
* {
* keys: 'n',
* type: 'motion',
* motion: 'moveByCharacters',
* motionArgs: {
* forward: false
* }
* },
*
* {
* keys: 'e',
* type: 'motion',
* motion: 'moveByLines',
* motionArgs: {
* forward: true,
* linewise: true
* }
* }
* );
*/
function addVimMapKey() {
dispatchSKEvent("front", ['addVimKeyMap', Array.from(arguments)]);
}

const userDefinedFunctions = {};
function mapkey(keys, annotation, jscode, options) {
if (!options || _isDomainApplicable(options.domain)) {
Expand Down Expand Up @@ -144,26 +103,6 @@ function addSearchAlias(alias, prompt, search_url, search_leader_key, suggestion
dispatchSKEvent('api', ['addSearchAlias', alias, prompt, search_url, search_leader_key, suggestion_url, "user", only_this_site_key, options]);
}

function isEmptyObject(obj) {
for (var name in obj) {
return false;
}
return true;
}

function applyUserSettings(delta) {
if (delta.error !== "") {
if (window === top) {
showPopup("[SurfingKeys] Error found in settings: " + delta.error);
} else {
console.log("[SurfingKeys] Error found in settings({0}): {1}".format(window.location.href, delta.error));
}
}
if (!isEmptyObject(delta.settings)) {
dispatchSKEvent("front", ['applySettingsFromSnippets', delta.settings]);
}
}

const api = {
RUNTIME,
aceVimMap,
Expand Down

0 comments on commit 5db21d4

Please sign in to comment.