# -*- coding: utf-8 -*-
import sys, socket, json, time, urllib.request
sys.stdout.reconfigure(encoding='utf-8')

HOST = "localhost"
PORT = 9222

def get_targets():
    url = f'http://{HOST}:{PORT}/json'
    with urllib.request.urlopen(url) as r:
        return json.loads(r.read().decode())

page = None
for t in get_targets():
    if 'appmsg' in t.get('url', '') and 'action=edit' in t.get('url', ''):
        page = t
        break

import websocket
ws = websocket.create_connection(page['webSocketDebuggerUrl'])
ws.settimeout(30)

TITLE = '放春假的孩子开心到"模糊"，而家长还在焦虑"弯道超车"？'

# 检查元素是否存在并可见
ws.send(json.dumps({
    'id': 1,
    'method': 'Runtime.evaluate',
    'params': {
        'expression': '''
        (function() {
            var el = document.getElementById("title");
            if (!el) return "not found";
            var rect = el.getBoundingClientRect();
            return "found, visible: " + (rect.width > 0 && rect.height > 0) + ", display: " + getComputedStyle(el).display;
        })()
        '''
    }
}))

time.sleep(0.5)
response = ws.recv()
print('元素状态:', response[:200])

ws.close()
