I am building a chrome extension that overrides the default new tab. I have a search input field on this new tab page that I want to focus this field on when a user opens a new tab. I have seen many answers, but none of them is satisfactory since they all have some URLs on the address bar. This extension Ocean Hero has managed to achieve this neat and clean. One thing I notice with this extension is when we open a new tab, initially, the focus is on the address bar then after a blink, it goes to the below searchfield.
if you look into their background.js minified version. I found out that this piece of code is doing this thing.
self.addEventListener("fetch", function (t) {
var e = t.request.url;
e.startsWith(location.origin + "/index.html") &&
!e.match("focus") &&
((O = !0),
setTimeout(function () {
O = !1;
}, 50));
});
function l(t, e) {
return new Promise(function (r) {
return s("tabs")
? chrome.tabs.update(t, e, function () {
return r(null);
})
: r(null);
});
}
function o(t, e) {
(null == e || e > t.length) && (e = t.length);
for (var r = 0, n = new Array(e); r < e; r++) n[r] = t[r];
return n;
}
function d() {
var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : null;
if (s("storage"))
return new Promise(function (e) {
return chrome.storage.local.get(t, e);
});
var e = t || Object.keys(localStorage);
return Promise.resolve(
e.reduce(function (t, e) {
return o(o({}, t), {}, i({}, e, u.a.getItem(e)));
}, {}),
);
}
function s(t) {
return function () {
var e = this,
r = arguments;
return new Promise(function (n, o) {
function i(t) {
c(a, n, o, i, u, "next", t);
}
function u(t) {
c(a, n, o, i, u, "throw", t);
}
var a = t.apply(e, r);
i(void 0);
});
};
}
function c(t, e, r, n, o, i, u) {
try {
var a = t[i](u),
c = a.value;
} catch (t) {
return void r(t);
}
a.done ? e(c) : Promise.resolve(c).then(n, o);
}
chrome.tabs.onCreated.addListener(function (t) {
O &&
(console.log("onCreated"),
(function (t) {
if (P.includes(t.id)) return null;
s(function* () {
if (void 0 === l) {
var t,
e = yield Object(d.f)(["search"]);
l = null === (t = e.search) || void 0 === t ? void 0 : t.isAutofocus;
}
})(),
l &&
chrome.tabs.create(
{
url: "".concat(
"chrome-extension://".concat(chrome.runtime.id, "/index.html"),
"?focus=true",
),
active: !0,
},
function (e) {
P.unshift(e.id),
P.splice(100),
chrome.tabs.remove(t.id),
chrome.tabs.update(e.id, {
url: "chrome://newtab/",
});
},
);
})(t));
O = !1
});
--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/83a248fc-681c-4640-b5c9-047d4d88b7ban%40chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/73937775-759c-4af4-83c2-f53551bb27ean%40chromium.org.