Skip to content

Instantly share code, notes, and snippets.

@szhu
Created December 31, 2025 16:19
Show Gist options
  • Select an option

  • Save szhu/f3d862d8dc06b623ba3fc9f1ba32da91 to your computer and use it in GitHub Desktop.

Select an option

Save szhu/f3d862d8dc06b623ba3fc9f1ba32da91 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Prevent Input Zoom (robust)
// @match *://*/*
// @run-at document-start
// ==/UserScript==
const VIEWPORT_CONTENT =
"width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no";
function upsertViewportMeta() {
let [meta, ...others] = document.querySelectorAll('meta[name="viewport"]');
for (let otherMeta of others) {
otherMeta.remove();
}
if (!meta) {
meta = document.createElement("meta");
meta.name = "viewport";
}
if (meta.getAttribute("content") != VIEWPORT_CONTENT) {
// alert(meta.getAttribute("content"));
meta.setAttribute("content", VIEWPORT_CONTENT);
}
if (!meta.parent) {
document.head.appendChild(meta);
}
}
function debouncedUpsertViewportMeta() {
clearTimeout(debouncedUpsertViewportMeta.timeout);
debouncedUpsertViewportMeta.timeout = setTimeout(upsertViewportMeta, 50);
}
debouncedUpsertViewportMeta();
new MutationObserver(debouncedUpsertViewportMeta) //
.observe(document.head, {
childList: true,
subtree: false,
attributes: true,
attributeFilter: ["content", "name"],
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment