每次打开新标签页时,我都会使用this extension获取空白页,遗憾的是,新标签页打开后,地址栏没有聚焦。
我更改了新页面内容以发送击键9以模拟Tab键。这导致浏览器专注于地址栏,但它不起作用。
<title></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>
还有其他选择吗?
答案 0 :(得分:3)
消除了上述问题后,我的代码正常运行。
添加了清单版本
{
"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
}
添加了<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>
使用您的代码并添加了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();
});