{"id":6041,"date":"2026-03-15T20:23:37","date_gmt":"2026-03-15T13:23:37","guid":{"rendered":"https:\/\/dev.artru.net\/?p=6041"},"modified":"2026-03-16T09:20:40","modified_gmt":"2026-03-16T02:20:40","slug":"tong-hop-code-console-debug","status":"publish","type":"post","link":"https:\/\/artru.net\/zh\/tong-hop-code-console-debug\/","title":{"rendered":"\u63a7\u5236\u53f0\u8c03\u8bd5\u4ee3\u7801\u7684\u7f16\u8bd1"},"content":{"rendered":"<h2 class=\"wp-block-heading\">\u8be5\u4ee3\u7801\u4f1a\u68c0\u67e5 {body} \u7684\u9ad8\u5ea6\u4f55\u65f6\u53d1\u751f\u53d8\u5316\u3002.<\/h2>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">(() =&gt; {\n  const H = () =&gt; Math.max(\n    document.documentElement.scrollHeight,\n    document.documentElement.clientHeight,\n    document.body?.scrollHeight || 0,\n    document.body?.clientHeight || 0\n  );\n\n  let last = H(), raf = null;\n\n  const check = () =&gt; {\n    raf = null;\n    const h = H();\n    if (h !== last) {\n      console.log('&#91;HEIGHT] =',h,'|',h - last);\n      last = h;\n    }\n  };\n\n  const s = () =&gt; raf || (raf = requestAnimationFrame(check));\n\n  new ResizeObserver(s).observe(document.documentElement);\n  document.body &amp;&amp; new ResizeObserver(s).observe(document.body);\n  new MutationObserver(s).observe(document.documentElement, { childList: true, subtree: true });\n\n  addEventListener('load', s, { passive: true });\n  addEventListener('resize', s, { passive: true });\n\n  console.log('\u2705 Height watcher ON');\n})();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\"\u538b\u529b\u6d4b\u8bd5\"\u63a5\u53e3\uff08\u5e03\u5c40\u8010\u4e45\u6027\u6d4b\u8bd5\uff09<\/h2>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06\u60a8\u7f51\u7ad9\u4e0a\u7684\u6240\u6709\u6587\u672c\u66ff\u6362\u4e3a\u4e00\u6bb5\u6781\u957f\u7684\u6587\u672c\uff08Lorum Ipsum\uff09\u3002\u5b83\u5bf9\u4e8e\u68c0\u67e5\u6587\u7ae0\u6807\u9898\u8fc7\u957f\u65f6\u5361\u7247\u3001\u6309\u94ae\u6216\u7f51\u683c\u662f\u5426\u51fa\u73b0\u9519\u4f4d\u975e\u5e38\u6709\u7528\u3002.<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">document.querySelectorAll('p, h1, h2, h3, h4, h5, h6, span, a').forEach(el => {\n  el.innerText = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. \".repeat(3);\n});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\"X\u5c04\u7ebf\"\u6a21\u5f0f\uff08\u63a2\u6d4b\u5d4c\u5957\u7ed3\u6784\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">&#91;].forEach.call(document.querySelectorAll('*'), function(el) {\n  el.style.outline = '1px solid #' + (Math.random() * 0xFFFFFF &lt;&lt; 0).toString(16);\n  el.style.backgroundColor = 'rgba(0,0,0,0.05)';\n});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5217\u51fa\u8fc7\u5927\u7684\u56fe\u50cf\uff08\u901f\u5ea6\u8f83\u6162\u7684 LCP\uff09\u3002<\/h2>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u626b\u63cf\u9875\u9762\u4e0a\u7684\u6240\u6709\u56fe\u50cf\uff0c\u5e76\u8bb0\u5f55\u5b9e\u9645\u5c3a\u5bf8\uff08\u81ea\u7136\u5c3a\u5bf8\uff09\u660e\u663e\u5927\u4e8e\u663e\u793a\u5c3a\u5bf8\u7684\u56fe\u50cf\uff0c\u4ece\u800c\u5e2e\u52a9\u60a8\u786e\u5b9a\u7f51\u7ad9\u6027\u80fd\u7f13\u6162\u7684\u539f\u56e0\u3002.<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">(async () => {\n  for (const img of $$('img')) {\n    try {\n      const res = await fetch(img.src);\n      const blob = await res.blob();\n      const bm = await createImageBitmap(blob);\n      const { width: nW, height: nH } = bm;\n      const { width: dW, height: dH } = img;\n\n      if (dW > 0 &amp;&amp; nW \/ dW > 1.5) {\n        const ratio = (nW \/ dW).toFixed(2);\n        const waste = (((nW * nH - dW * dH) \/ (nW * nH)) * 100).toFixed(1);\n        \n        console.log(\n          `%c &#91;HEAVY] ${ratio}x %c (${waste}% waste) %c\\n` +\n          `\u2022 Natural: ${nW}x${nH}px\\n` +\n          `\u2022 Display: ${dW}x${dH}px\\n` +\n          `%c\u2022 RECOMMEND: Resize to ${dW * 2}px%c\\n` +\n          `\u2022 URL: ${img.src}\\n` +\n          `\u2022 DOM:`,\n          'color:white;background:#e67e22;font-weight:bold;border-radius:3px;padding:2px 5px;',\n          'color:red;font-weight:bold;', \n          'color:inherit;',\n          'color:green;font-weight:bold;text-decoration:underline;',\n          'color:inherit;',\n          img\n        );\n\n        img.style.outline = '4px dashed #e67e22';\n        img.style.outlineOffset = '-4px';\n      }\n      bm.close();\n    } catch (e) {}\n  }\n})();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u76f4\u63a5\u7f16\u8f91\u5185\u5bb9<\/h2>\n\n\n\n<p>\u5c06\u6574\u4e2a\u7f51\u7ad9\u53d8\u6210\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u5668\u3002\u60a8\u53ef\u4ee5\u70b9\u51fb\u4efb\u610f\u4f4d\u7f6e\u5220\u9664\u6587\u672c\uff0c\u8f93\u5165\u65b0\u6587\u672c\u6765\u6d4b\u8bd5\u54ea\u79cd\u63aa\u8f9e\u6700\u9002\u5408\u5e03\u5c40\uff0c\u7136\u540e\u518d\u5728\u540e\u53f0\u8fdb\u884c\u66f4\u6539\u3002.<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">document.designMode = 'on';<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u68c0\u67e5\u662f\u5426\u5b58\u5728\"\u6df7\u4e71\"\u7684 z-index \u503c\"<\/h2>\n\n\n\n<p>\u5982\u679c\u9047\u5230\u5143\u7d20\u91cd\u53e0\u7684\u95ee\u9898\uff08\u4f8b\u5982\u6807\u9898\u4e0e\u5f39\u51fa\u7a97\u53e3\u91cd\u53e0\uff09\uff0c\u6b64\u4ee3\u7801\u5c06\u5217\u51fa\u6240\u6709\u5177\u6709\u6307\u5b9a\u5c5e\u6027\u7684\u5143\u7d20\u3002 <code data-no-translation=\"\" data-no-auto-translation=\"\">z-index<\/code> \u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u6bd4\u8f83\u8ba2\u5355\u4e86\u3002.<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">const elements = &#91;...document.querySelectorAll('*')]\n  .map(el => ({\n    element: el,\n    zIndex: window.getComputedStyle(el).zIndex\n  }))\n  .filter(obj => obj.zIndex !== 'auto');\nconsole.table(elements);<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>Code ki\u1ec3m tra chi\u1ec1u cao {body} khi c\u00f3 s\u1ef1 thay \u0111\u1ed5i. \"Stress Test\" Giao di\u1ec7n (Ki\u1ec3m tra \u0111\u1ed9 b\u1ec1n Layout) \u0110o\u1ea1n code n\u00e0y s\u1ebd thay th\u1ebf to\u00e0n b\u1ed9 v\u0103n b\u1ea3n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-6041","post","type-post","status-publish","format-standard","hentry","category-website"],"_links":{"self":[{"href":"https:\/\/artru.net\/zh\/wp-json\/wp\/v2\/posts\/6041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/artru.net\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artru.net\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artru.net\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artru.net\/zh\/wp-json\/wp\/v2\/comments?post=6041"}],"version-history":[{"count":0,"href":"https:\/\/artru.net\/zh\/wp-json\/wp\/v2\/posts\/6041\/revisions"}],"wp:attachment":[{"href":"https:\/\/artru.net\/zh\/wp-json\/wp\/v2\/media?parent=6041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artru.net\/zh\/wp-json\/wp\/v2\/categories?post=6041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artru.net\/zh\/wp-json\/wp\/v2\/tags?post=6041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}