Apple Hungarian Keyboard Layout to Hungarian PC Layout

Replaces special characters as you type with the equivalent regular 101-button Hungarian PC keyboard characters.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Apple Hungarian Keyboard Layout to Hungarian PC Layout
// @namespace    apple-hun-keyboard-layout-to-pc-hun-layout
// @version      1.0
// @description  Replaces special characters as you type with the equivalent regular 101-button Hungarian PC keyboard characters.
// @author       hlorand.hu
// @match        https://*/*
// @exclude      https://vscode.dev/*
// @exclude      https://github.dev/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        none
// @license      https://creativecommons.org/licenses/by-nc-sa/4.0/
// ==/UserScript==

//'use strict';

(function () {

// Error FIX: This document requires 'TrustedScript' assignment.
window.trustedTypes.createPolicy('default', {createHTML: (string, sink) => string})

  const map = {
    'Ķ': '<',
    ';': '>',
    '|': '<',
    '«': '>',
    //'–': '*',// altgr + -
    '^': '*', // changed from alrgr + - to altgr + á
    '”': '{',
    '~': '}', // not working
    'ń': '[',
    '©': ']',
    'ę': '|',
    '»': '#',
    'ć': '&',
    '„': '@',
    '…': '$',
    '–': ';', // not working on original pos
    '£': '^',
    '{': '`',
    '@': '\\',
    '&' : '~'
  };

  function isEditable(el) {
    return el.isContentEditable || ['INPUT', 'TEXTAREA'].includes(el.tagName);
  }

  function handleInput(e) {
    const el = e.target;
    if (!isEditable(el)) return;

    if (el.isContentEditable) {
      const sel = window.getSelection();
      if (!sel.rangeCount) return;

      const range = sel.getRangeAt(0);
      const node = range.startContainer;

      if (node.nodeType === 3) { // text node
        const text = node.nodeValue;
        const lastChar = text[text.length - 1];
        const replacement = map[lastChar];
        if (replacement) {
          node.nodeValue = text.slice(0, -1) + replacement;
          range.setStart(node, node.nodeValue.length);
          range.collapse(true);
          sel.removeAllRanges();
          sel.addRange(range);
        }
      }
    } else {
      const value = el.value;
      const pos = el.selectionStart;
      const lastChar = value[pos - 1];
      const replacement = map[lastChar];
      if (replacement) {
        el.value = value.slice(0, pos - 1) + replacement + value.slice(pos);
        el.setSelectionRange(pos, pos); // preserve caret
      }
    }
  }

  document.addEventListener('input', handleInput);
})();