dina1 commited on
Commit
fe018f7
·
verified ·
1 Parent(s): ee6b40d

Update playwright_model.py

Browse files
Files changed (1) hide show
  1. playwright_model.py +45 -39
playwright_model.py CHANGED
@@ -13,17 +13,16 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
13
  async with async_playwright() as p:
14
  browser = await p.chromium.launch(headless=True)
15
  page = await browser.new_page()
16
- print(f"🌐 Opening page: {public_url}")
17
  await page.goto(public_url, wait_until="load")
18
 
19
- # ✅ Inject enhanced logic to capture main and sub screens dynamically
20
  js_logic = """
21
  (function(){
22
  const menus=[...document.querySelectorAll('.menu-item')];
23
  const screens=[...document.querySelectorAll('.screen')];
24
  window.__ordered=[];
25
  const seen=new Set();
26
-
27
  for(const m of menus){
28
  let id=m.dataset.screen||m.dataset.target;
29
  if(!id){
@@ -34,20 +33,20 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
34
  if(s && !seen.has(s)){seen.add(s);window.__ordered.push({menu:m,screen:s});}
35
  }
36
  for(const s of screens) if(!seen.has(s)) window.__ordered.push({menu:null,screen:s});
37
-
38
  window.__visitedWorkflows=[];
39
  window.__currentIndex=0;
40
  window.__done=false;
41
 
42
- async function getSubScreens(screen){
43
- const tabs=[...screen.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab]')];
44
- const results=[];
 
45
  for(const t of tabs){
46
- const subName=t.textContent?.trim().replace(/\\s+/g,'-').toLowerCase();
47
- if(subName) results.push({tab:t, name:subName});
48
  }
49
- return results;
50
- }
51
 
52
  window.__captureNext=async function(){
53
  if(window.__done) return false;
@@ -55,52 +54,38 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
55
  const pair=window.__ordered[window.__currentIndex];
56
  const {menu,screen}=pair;
57
  if(!screen){window.__done=true;return false;}
58
-
59
  const wfName=screen.id || screen.getAttribute('data-name') || ('screen_'+window.__currentIndex);
60
  if(window.__visitedWorkflows.includes(wfName)){
61
  window.__currentIndex++;
62
  return window.__captureNext();
63
  }
64
-
65
  window.__visitedWorkflows.push(wfName);
66
  document.querySelectorAll('.screen').forEach(s=>s.classList.remove('active'));
67
  document.querySelectorAll('.menu-item').forEach(m=>m.classList.remove('active'));
68
  screen.classList.add('active');
69
  if(menu) menu.classList.add('active');
70
  screen.scrollIntoView({behavior:'smooth',block:'center'});
71
-
72
- const mainTitle = screen.querySelector('.page-title, .header-title, h2')?.textContent?.trim() || wfName;
73
- document.title = mainTitle;
74
-
75
- const subs = await getSubScreens(screen);
76
  window.__currentIndex++;
77
- return {screenName: wfName, subScreens: subs.map(x=>x.name)};
 
78
  };
79
 
80
- window.__clickSubScreen = async function(mainName, subName){
81
- const screen = document.getElementById(mainName);
82
- if(!screen) return false;
83
- const tab=[...screen.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab]')]
84
- .find(t=>t.textContent.trim().toLowerCase().includes(subName));
85
- if(tab){
86
- tab.click();
87
- await new Promise(r=>setTimeout(r,700));
88
- const title = screen.querySelector('.page-title, .header-title, h2')?.textContent?.trim() || mainName;
89
- document.title = title + ' - ' + subName;
90
- return true;
91
- }
92
  return false;
93
  };
94
  })();
95
  """
96
-
97
  await page.evaluate(js_logic)
98
  await asyncio.sleep(1)
99
 
100
  screenshots = []
101
  index = 0
102
 
103
- # ✅ Loop through main workflows and their sub-screens dynamically
104
  while True:
105
  result = await page.evaluate("window.__captureNext()")
106
  if not result:
@@ -108,21 +93,42 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
108
 
109
  screen_name = result.get("screenName", f"screen_{index}")
110
  sub_screens = result.get("subScreens", [])
111
-
112
- # Capture main screen
113
  screenshot_path = os.path.join(OUTPUT_DIR, f"{screen_name}.png")
114
  print(f"📸 Capturing main screen: {screen_name}")
115
 
 
 
 
 
 
 
 
 
 
 
116
  await asyncio.sleep(1.2)
117
  await page.screenshot(path=screenshot_path, full_page=True)
118
  screenshots.append(screenshot_path)
119
 
120
- # Capture sub-screens/tabs
121
  for sub in sub_screens:
122
- print(f" ↳ Capturing sub-screen: {sub}")
123
- await page.evaluate(f"window.__clickSubScreen('{screen_name}','{sub}')")
124
  await asyncio.sleep(1.0)
125
- sub_path = os.path.join(OUTPUT_DIR, f"{screen_name}_{sub}.png")
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  await page.screenshot(path=sub_path, full_page=True)
127
  screenshots.append(sub_path)
128
 
 
13
  async with async_playwright() as p:
14
  browser = await p.chromium.launch(headless=True)
15
  page = await browser.new_page()
16
+ print(f"Opening page: {public_url}")
17
  await page.goto(public_url, wait_until="load")
18
 
19
+ # ✅ Inject workflow traversal + sub-screen logic
20
  js_logic = """
21
  (function(){
22
  const menus=[...document.querySelectorAll('.menu-item')];
23
  const screens=[...document.querySelectorAll('.screen')];
24
  window.__ordered=[];
25
  const seen=new Set();
 
26
  for(const m of menus){
27
  let id=m.dataset.screen||m.dataset.target;
28
  if(!id){
 
33
  if(s && !seen.has(s)){seen.add(s);window.__ordered.push({menu:m,screen:s});}
34
  }
35
  for(const s of screens) if(!seen.has(s)) window.__ordered.push({menu:null,screen:s});
 
36
  window.__visitedWorkflows=[];
37
  window.__currentIndex=0;
38
  window.__done=false;
39
 
40
+ // helper: get all visible sub-screen tabs dynamically
41
+ window.__getSubScreens = function(screen){
42
+ const tabs=[...screen.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
43
+ const list=[];
44
  for(const t of tabs){
45
+ const sub=t.textContent?.trim();
46
+ if(sub && !list.includes(sub)) list.push(sub);
47
  }
48
+ return list;
49
+ };
50
 
51
  window.__captureNext=async function(){
52
  if(window.__done) return false;
 
54
  const pair=window.__ordered[window.__currentIndex];
55
  const {menu,screen}=pair;
56
  if(!screen){window.__done=true;return false;}
 
57
  const wfName=screen.id || screen.getAttribute('data-name') || ('screen_'+window.__currentIndex);
58
  if(window.__visitedWorkflows.includes(wfName)){
59
  window.__currentIndex++;
60
  return window.__captureNext();
61
  }
 
62
  window.__visitedWorkflows.push(wfName);
63
  document.querySelectorAll('.screen').forEach(s=>s.classList.remove('active'));
64
  document.querySelectorAll('.menu-item').forEach(m=>m.classList.remove('active'));
65
  screen.classList.add('active');
66
  if(menu) menu.classList.add('active');
67
  screen.scrollIntoView({behavior:'smooth',block:'center'});
 
 
 
 
 
68
  window.__currentIndex++;
69
+ const subs = window.__getSubScreens(screen);
70
+ return {screenName:wfName, subScreens:subs};
71
  };
72
 
73
+ // click sub-screen tab by text
74
+ window.__clickSubScreen = async function(name){
75
+ const tabs=[...document.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
76
+ const t=tabs.find(x=>x.textContent.trim()===name);
77
+ if(t){t.click(); return true;}
 
 
 
 
 
 
 
78
  return false;
79
  };
80
  })();
81
  """
 
82
  await page.evaluate(js_logic)
83
  await asyncio.sleep(1)
84
 
85
  screenshots = []
86
  index = 0
87
 
88
+ # ✅ Loop through each workflow
89
  while True:
90
  result = await page.evaluate("window.__captureNext()")
91
  if not result:
 
93
 
94
  screen_name = result.get("screenName", f"screen_{index}")
95
  sub_screens = result.get("subScreens", [])
 
 
96
  screenshot_path = os.path.join(OUTPUT_DIR, f"{screen_name}.png")
97
  print(f"📸 Capturing main screen: {screen_name}")
98
 
99
+ # Update visible + browser tab title dynamically
100
+ await page.evaluate(
101
+ """(name) => {
102
+ const titleEl = document.querySelector('.page-title, .header-title, main h2');
103
+ if (titleEl) titleEl.textContent = name;
104
+ document.title = name;
105
+ }""",
106
+ screen_name
107
+ )
108
+
109
  await asyncio.sleep(1.2)
110
  await page.screenshot(path=screenshot_path, full_page=True)
111
  screenshots.append(screenshot_path)
112
 
113
+ # Capture sub-screens dynamically
114
  for sub in sub_screens:
115
+ print(f" ↳ Capturing sub-screen: {sub}")
116
+ await page.evaluate(f"window.__clickSubScreen('{sub}')")
117
  await asyncio.sleep(1.0)
118
+ sub_name_clean = sub.replace(" ", "_").lower()
119
+ sub_path = os.path.join(OUTPUT_DIR, f"{screen_name}_{sub_name_clean}.png")
120
+
121
+ # update tab title for sub-screen
122
+ await page.evaluate(
123
+ """(mainName, subName) => {
124
+ const titleEl = document.querySelector('.page-title, .header-title, main h2');
125
+ const newTitle = mainName + ' - ' + subName;
126
+ if (titleEl) titleEl.textContent = newTitle;
127
+ document.title = newTitle;
128
+ }""",
129
+ screen_name, sub
130
+ )
131
+
132
  await page.screenshot(path=sub_path, full_page=True)
133
  screenshots.append(sub_path)
134