mobile_bisect.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. """One-off diagnostic: which subtree creates the page-level +N px?
  3. Bisects by hiding top-level subtrees (topbar vs main etc.) and reading
  4. documentElement.scrollWidth, then descends into the guilty container's
  5. children. Prints the culprit chain. Not part of the bundle; a T12
  6. development aid kept for reproducibility.
  7. """
  8. import json
  9. import os
  10. import sys
  11. HERE = os.path.dirname(os.path.abspath(__file__))
  12. sys.path.insert(0, HERE)
  13. import mobile_audit as ma # noqa: E402
  14. import harness # noqa: E402
  15. if "WEBSTACK_WORK" not in os.environ:
  16. os.environ["WEBSTACK_WORK"] = os.path.join(
  17. harness.REPO, "build", "ws-mobile")
  18. def bisect(chrome):
  19. js = r"""
  20. (() => {
  21. const de = document.documentElement;
  22. const base = de.scrollWidth - de.clientWidth;
  23. const report = {base: base, steps: []};
  24. const direct = [];
  25. // step 1: each direct child of body toggled alone
  26. for (const child of document.body.children) {
  27. const prev = child.style.display;
  28. child.style.display = 'none';
  29. report.steps.push({hide: child.tagName + '.' + child.className,
  30. dx: de.scrollWidth - de.clientWidth});
  31. child.style.display = prev;
  32. }
  33. // step 2: every element with scrollWidth > clientWidth (visible ox),
  34. // skipping elements inside a scroll container
  35. const inScroller = (el) => {
  36. for (let a = el.parentElement; a; a = a.parentElement) {
  37. const ox = getComputedStyle(a).overflowX;
  38. if (ox !== 'visible') return true;
  39. }
  40. return false;
  41. };
  42. for (const el of document.querySelectorAll('*')) {
  43. if (inScroller(el)) continue;
  44. const cs = getComputedStyle(el);
  45. if (cs.position === 'fixed') continue;
  46. if (el.scrollWidth - el.clientWidth > 4 && el.clientWidth > 0) {
  47. direct.push({el: el.tagName + '.' + (el.className || ''),
  48. over: el.scrollWidth - el.clientWidth,
  49. rectRight: Math.round(el.getBoundingClientRect().right)});
  50. }
  51. }
  52. // step 3: text nodes whose range right edge exceeds the viewport
  53. const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
  54. const texts = [];
  55. let node;
  56. while ((node = walker.nextNode())) {
  57. const range = document.createRange();
  58. range.selectNodeContents(node);
  59. const rects = range.getClientRects();
  60. for (const r of rects) {
  61. if (r.width > 0 && r.right > de.clientWidth + 0.5) {
  62. texts.push({text: node.textContent.slice(0, 60),
  63. right: Math.round(r.right),
  64. parent: node.parentElement.tagName + '.'
  65. + (node.parentElement.className || '')});
  66. break;
  67. }
  68. }
  69. }
  70. report.direct = direct.slice(0, 20);
  71. report.texts = texts.slice(0, 20);
  72. return report;
  73. })()
  74. """
  75. return chrome._eval(js)
  76. def main():
  77. route = sys.argv[1] if len(sys.argv) > 1 else "/rules"
  78. width = int(sys.argv[2]) if len(sys.argv) > 2 else 360
  79. chromium = ma.find_chromium()
  80. ctx = harness.start_bundle("t12dbg")
  81. harness.CTX = ctx
  82. try:
  83. ids = ma.seed(ctx)
  84. # fresh login frame for the browser
  85. frames = harness.directives("/login")
  86. content = json.loads(frames[0]["snapshot_frame"]["content"])
  87. action = content["public"]["login"]
  88. status, body = harness.http("POST", action["uri"],
  89. {"username": "admin",
  90. "password": "adminpass123"})
  91. frame = None
  92. for directive in json.loads(body):
  93. if directive["type"] == "set":
  94. candidate = json.loads(directive["snapshot_frame"]["content"])
  95. if candidate["slot"]["type"] == "auth":
  96. frame = directive["snapshot_frame"]
  97. chrome = ma.Chrome(chromium, harness.free_port())
  98. try:
  99. chrome.connect()
  100. chrome.add_init_script(
  101. "try { localStorage.setItem('statum:auth', %s); } catch (e) {}"
  102. % json.dumps(json.dumps(frame)))
  103. chrome.set_viewport(width)
  104. chrome.goto(harness.WEB + route)
  105. print(json.dumps(bisect(chrome), indent=1))
  106. finally:
  107. chrome.close()
  108. finally:
  109. try:
  110. ctx.app.terminate()
  111. ctx.app.wait(timeout=10)
  112. except OSError:
  113. pass
  114. if __name__ == "__main__":
  115. main()