Chrome extension: blank new tab + address bar

I am using this extension to get a blank page every time I open a new tab, unfortunately the address bar is not focused after the new tab is open.

I changed the new page content to send a 9 key press to simulate a tab key. which makes the browser focus on the address bar, but it doesn't work.

<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>

      

Is there any alternative?

+3


source to share


1 answer


Extension problems

  • It doesn't use the updated version of the manifest
  • Did it in accordance with the Content Script policy.

After fixing the above problems, I got your code.

Working version

manifest.json

Added version of the manifest

{
    "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



Added tag <Script>

for CSP matching.

<!--
Chrome insists on putting "chrome://newtab"
as title
if there  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>

      

empty.js

Used your code and added DOMContentLoaded

an event listener

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();
});

      

Links

+3


source







All Articles