3

每次打开新标签时,我都使用 此扩展程序获取空白页,不幸的是,打开新标签后地址栏没有聚焦。

我更改了新页面内容以发送击键 9 来模拟 Tab 键。这会导致浏览器专注于地址栏,但它不起作用。

<title>&#65279;</title>
<script>
function init() {
    var k = 9;

    var oEvent = document.createEvent('KeyboardEvent');

    // Chromium Hack
    Object.defineProperty(oEvent, 'keyCode', {
                get : function() {
                    return this.keyCodeVal;
                }
    });     
    Object.defineProperty(oEvent, 'which', {
                get : function() {
                    return this.keyCodeVal;
                }
    });     

    if (oEvent.initKeyboardEvent) {
        oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, false, false, false, false, k, k);
    } else {
        oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, k, 0);
    }

    oEvent.keyCodeVal = k;

    if (oEvent.keyCode !== k) {
        alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
    }

    document.dispatchEvent(oEvent);
}
</script>
<body onload="init()">
</body>

有没有其他方法可以做到这一点?

4

1 回答 1

3

扩展中的问题

  • 它没有使用更新的清单版本
  • 使其符合内容脚本政策。

消除上述问题后,我让您的代码正常工作。

工作版本

清单.json

添加清单版本

{
    "update_url": "http://clients2.google.com/service/update2/crx",
    "name": "Empty New Tab Page",
    "version": "1.1",
    "description": "With this extension, new tabs display a blank page instead of the usual new tab page with thumbnails.",
    "icons": {
        "128": "icon_128.png"
    },
    "chrome_url_overrides": {
        "newtab": "empty_ntp.html"
    },
    "manifest_version": 2
}

empty_ntp.html

添加<Script>标签以符合 CSP。

<!--
Chrome insists on putting "chrome://newtab"
as title
if there 's no title,
instead of putting something useful like a localized "New Tab" there.

As a workaround, use a space as title. An empty tab is better than one saying
something cryptic. Chrome puts "chrome://newtab" if the title is whitespace too,
but it doesn'
t recognize all the whitespace characters listed at
http: //en.wikipedia.org/wiki/Space_(punctuation) :-)
-->
<
title > & #65279;</title>
<script src= "empty.js" > < /script>

空.js

使用您的代码并添加了一个DOMContentLoaded事件监听器

function init() {
    var k = 9;

    var oEvent = document.createEvent('KeyboardEvent');

    // Chromium Hack
    Object.defineProperty(oEvent, 'keyCode', {
        get: function () {
            return this.keyCodeVal;
        }
    });
    Object.defineProperty(oEvent, 'which', {
        get: function () {
            return this.keyCodeVal;
        }
    });

    if (oEvent.initKeyboardEvent) {
        oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, false, false, false, false, k, k);
    } else {
        oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, k, 0);
    }

    oEvent.keyCodeVal = k;

    if (oEvent.keyCode !== k) {
        alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
    }

    document.dispatchEvent(oEvent);
}//Added an Event Listener
document.addEventListener("DOMContentLoaded", function () {
    init();
});

参考

于 2013-01-21T06:39:04.323 回答