用户:天明/common.js:修订间差异

天明留言 | 贡献
创建页面,内容为“mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Volker_E._(WMF)/dark-mode.js&action=raw&ctype=text/javascript' ); // User:Volker_E._(WMF)/dark-mode.js
 
天明留言 | 贡献
无编辑摘要
第1行: 第1行:
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Volker_E._(WMF)/dark-mode.js&action=raw&ctype=text/javascript' ); // [[User:Volker_E._(WMF)/dark-mode.js]]
mw.loader.using(['jquery']).then(function () {
 
  function focusSearch() {
    // 常见搜索输入选择器:不同皮肤都尽量兼容
    var $input = $('#searchInput, input[name="search"], .vector-search-box-input').filter(':visible').first();
    if ($input.length) {
      $input.trigger('focus')[0].select();
      return true;
    }
 
    // 若搜索输入未见:尝试点击“放大镜/搜索开关”,再聚焦
    var $toggle = $(
      // Vector 2022 / 其他皮肤常见按钮(尽量广覆盖)
      'button[aria-controls*="search"], [data-action="search"], .vector-header .mw-ui-icon-search, .citizen-search-toggle, .mw-searchToggle, .search-toggle'
    ).filter(':visible').first();
 
    if ($toggle.length) {
      $toggle.trigger('click');
      setTimeout(function () {
        var $input2 = $('#searchInput, input[name="search"], .vector-search-box-input').filter(':visible').first();
        if ($input2.length) {
          $input2.trigger('focus')[0].select();
        }
      }, 50);
      return true;
    }
    return false;
  }
 
  $(document).on('keydown', function (e) {
    // 在输入框/编辑区里就不要拦截
    var $t = $(e.target);
    if ($t.is(':input, [contenteditable], [role="textbox"]')) return;
    if (e.ctrlKey || e.altKey || e.metaKey) return;
 
    // 捕获“/”键。兼容不同浏览器/键盘布局
    if (e.key === '/' || e.code === 'Slash' || e.keyCode === 191) {
      e.preventDefault();          // 阻止浏览器把“/”打到页面或触发 FF 的快速查找
      focusSearch();
    }
  });
});