~glacambre/firenvim

b0aeb4bf978dc3b3eaac778039499699fa5f67bb — Ghjuvan Lacambre 2 years ago 6f4d06b + 5aef1b4
Merge pull request #136 from glacambre/firenvim_shortcut

Enable shortcut to trigger firenvim
6 files changed, 61 insertions(+), 23 deletions(-)

M src/background.ts
M src/content.ts
M src/manifest.json
M src/page/functions.ts
M tslint.json
M webpack.config.js
M src/background.ts => src/background.ts +8 -1
@@ 214,7 214,7 @@ Object.assign(window, {
browser.runtime.onMessage.addListener(async (request: any, sender: any, sendResponse: any) => {
    const fn = request.funcName.reduce((acc: any, cur: string) => acc[cur], window);
    if (!fn) {
        throw new Error(`Error: unhandled content request: ${request.toString()}.`);
        throw new Error(`Error: unhandled content request: ${JSON.stringify(request)}.`);
    }
    return fn(sender, request.args !== undefined ? request.args : []);
});


@@ 224,3 224,10 @@ browser.tabs.onActivated.addListener(async ({ tabId }: { tabId: number }) => {
});

updateIcon();

browser.commands.onCommand.addListener(async (command: string) => {
    if (command === "nvimify") {
        const id = (await browser.tabs.query({ active: true }))[0].id;
        browser.tabs.sendMessage(id, { args: [], funcName: ["forceNvimify"] });
    }
});

M src/content.ts => src/content.ts +1 -1
@@ 121,7 121,7 @@ browser.runtime.onMessage.addListener(async (
) => {
    const fn = request.funcName.reduce((acc: any, cur: string) => acc[cur], window);
    if (!fn) {
        throw new Error(`Error: unhandled content request: ${request.toString()}.`);
        throw new Error(`Error: unhandled content request: ${JSON.stringify(request)}.`);
    }
    // If this is a selector-specific request and we don't know about this
    // selector, the message is not for us, so we mustn't reply. It'd be better

M src/manifest.json => src/manifest.json +28 -18
@@ 1,9 1,23 @@
{
  "manifest_version": 2,
  "name": "Firenvim",
  "version": "FIRENVIM_VERSION",
  "background": {
    "scripts": ["background.js"]
  },

  "description": "PACKAGE_JSON_DESCRIPTION",
  "browser_action": {
    "browser_style": true,
    "default_icon": "firenvim.svg",
    "default_title": "Firenvim",
    "default_popup": "browserAction.html"
  },

  "commands": {
    "nvimify": {
      "suggested_key": {
        "default": "Ctrl+E"
      },
      "description": "Turn the currently focused element into a neovim iframe."
    }
  },

  "content_scripts": [
    {


@@ 17,25 31,21 @@
    }
  ],

  "background": {
    "scripts": ["background.js"]
  "description": "PACKAGE_JSON_DESCRIPTION",

  "icons": {
    "48": "firenvim.svg"
  },

  "web_accessible_resources": ["NeovimFrame.html"],
  "manifest_version": 2,

  BROWSER_SPECIFIC_SETTINGS,
  "name": "Firenvim",

  "browser_action": {
    "browser_style": true,
    "default_icon": "firenvim.svg",
    "default_title": "Firenvim",
    "default_popup": "browserAction.html"
  },
  "permissions": ["nativeMessaging", "storage", "tabs"],

  "version": "FIRENVIM_VERSION",

  "permissions": ["nativeMessaging", "storage", "tabs"],
  "web_accessible_resources": ["NeovimFrame.html"],

  "icons": {
    "48": "firenvim.svg"
  }
  BROWSER_SPECIFIC_SETTINGS
}

M src/page/functions.ts => src/page/functions.ts +20 -0
@@ 61,6 61,26 @@ export function getFunctions(global: {
    disabled: boolean | Promise<boolean>,
}) {
    return {
        forceNvimify: () => {
            let elem = document.activeElement;
            if (!elem || elem === document.documentElement || elem === document.body) {
                function isVisible(e: HTMLElement) {
                    const rect = e.getBoundingClientRect();
                    const viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
                    return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
                }
                elem = Array.from(document.getElementsByTagName("textarea"))
                    .find(isVisible);
                if (!elem) {
                    elem = Array.from(document.getElementsByTagName("input"))
                        .find(e => e.type === "text" && isVisible(e));
                }
                if (!elem) {
                    return;
                }
            }
            global.nvimify({ target: elem } as any);
        },
        getEditorLocation: () => {
            // global.lastEditorLocation[1] is a selector. If no selector is
            // defined, we're not the script that should answer this question

M tslint.json => tslint.json +1 -0
@@ 2,6 2,7 @@
  "extends": ["tslint:recommended", "tslint-sonarts"],
  "rules": {
    "arrow-parens": false,
    "cognitive-complexity": false,
    "max-line-length": [
      true,
      {

M webpack.config.js => webpack.config.js +3 -3
@@ 73,7 73,7 @@ module.exports = [
        switch(path.basename(src)) {
          case "manifest.json":
            return content.toString()
              .replace('BROWSER_SPECIFIC_SETTINGS,', '"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk3pkgh862ElxtREZVPLxVNbiFWo9SnvZtZXZavNvs2GsUTY/mB9yHTPBGJiBMJh6J0l+F5JZivXDG7xdQsVD5t39CL3JGtt93M2svlsNkOEYIMM8tHbp69shNUKKjZOfT3t+aZyigK2OUm7PKedcPeHtMoZAY5cC4L1ytvgo6lge+VYQiypKF87YOsO/BGcs3D+MMdS454tLBuMp6LxMqICQEo/Q7nHGC3eubtL3B09s0l17fJeq/kcQphczKbUFhTVnNnIV0JX++UCWi+BP4QOpyk5FqI6+SVi+gxUosbQPOmZR4xCAbWWpg3OqMk4LqHaWpsBfkW9EUt6EMMMAfQIDAQAB",\n')
              .replace('BROWSER_SPECIFIC_SETTINGS', '"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk3pkgh862ElxtREZVPLxVNbiFWo9SnvZtZXZavNvs2GsUTY/mB9yHTPBGJiBMJh6J0l+F5JZivXDG7xdQsVD5t39CL3JGtt93M2svlsNkOEYIMM8tHbp69shNUKKjZOfT3t+aZyigK2OUm7PKedcPeHtMoZAY5cC4L1ytvgo6lge+VYQiypKF87YOsO/BGcs3D+MMdS454tLBuMp6LxMqICQEo/Q7nHGC3eubtL3B09s0l17fJeq/kcQphczKbUFhTVnNnIV0JX++UCWi+BP4QOpyk5FqI6+SVi+gxUosbQPOmZR4xCAbWWpg3OqMk4LqHaWpsBfkW9EUt6EMMMAfQIDAQAB"\n')
              .replace("FIRENVIM_VERSION", package_json.version)
              .replace("PACKAGE_JSON_DESCRIPTION", package_json.description)
              // Chrome doesn't support svgs in its manifest


@@ 96,13 96,13 @@ module.exports = [
      transform: (content, src) => {
        switch(path.basename(src)) {
          case "manifest.json":
            return content.toString().replace("BROWSER_SPECIFIC_SETTINGS,", `
            return content.toString().replace("BROWSER_SPECIFIC_SETTINGS", `
  "browser_specific_settings": {
    "gecko": {
      "id": "firenvim@lacamb.re",
      "strict_min_version": "65.0"
    }
  },`)
  }`)
              .replace("FIRENVIM_VERSION", package_json.version)
              .replace("PACKAGE_JSON_DESCRIPTION", package_json.description)
            ;