Setting the file. One moment.
Compare Preview · Wix Harmony Editor · wix/skills · Skills Docs
ContentsBack to the top of the page scripts/copy/compare-preview.mjs
scripts/copy/ compare-preview.mjs
JavaScript · 76 lines · 4 KB
14 import { chromium } from 'playwright' ;
15 import fs from 'node:fs' ;
16
17 const args = process.argv. slice ( 2 );
18 const opt = ( name , dflt ) => { const i = args. indexOf ( `--${ name }` ); return i >= 0 ? args[i + 1 ] : dflt; };
19 const [ sourceUrl , outDir ] = args. filter (( a , i ) => ! a. startsWith ( '--' ) && ! (i > 0 && args[i - 1 ]. startsWith ( '--' )));
20 if ( ! sourceUrl || ! outDir) { console. error ( 'usage: compare-preview.mjs <sourceUrl> <outDir> [--width 1280] [--height 1254] [--crops "x,y,w,h,s;..."]' ); process. exit ( 2 ); }
21 const width = Number ( opt ( 'width' , 1280 ));
22 const height = Number ( opt ( 'height' , 1254 ));
23 const crops = ( opt ( 'crops' , '' ) || '' ). split ( ';' ). filter (Boolean). map (( c ) => c. split ( ',' ). map (Number));
24 fs. mkdirSync (outDir, { recursive: true });
25
26 const browser = await chromium. connectOverCDP ( `http://127.0.0.1:${ process . env . PW_PORT || 9333 }` );
27 const ctx = browser. contexts ()[ 0 ];
28 const page = browser. contexts (). flatMap (( c ) => c. pages ()). find (( p ) => p. url (). includes ( 'editor.wix.com/edit/' ));
29 if ( ! page) { console. error ( 'No Harmony editor page is open.' ); process. exit ( 3 ); }
30 const before = page. viewportSize ();
31
32 // 1) Harmony Preview: preview chrome (~90px) + optional ads banner sit above the page.
33 await page. setViewportSize ({ width, height: height + 200 });
34 await page. waitForTimeout ( 800 );
35 await page. getByText ( 'Preview' , { exact: true }). first (). click ();
36 await page. waitForTimeout ( 7000 );
37 let top = null ;
38 for ( const f of page. frames ()) {
39 const t = await f. evaluate (() => { const e = document. querySelector ( 'header, [id^="SITE_HEADER"], #masterPage > *' ); return e ? e. getBoundingClientRect ().top : null ; }). catch (() => null );
40 if (t == null ) continue ;
41 const fe = await f. frameElement (). catch (() => null );
42 const fb = fe ? await fe. boundingBox () : null ;
43 if (fb) { top = fb.y + t; break ; }
44 }
45 if (top == null ) top = 90 ;
46 await page. setViewportSize ({ width, height: Math. round (top + height) });
47 await page. waitForTimeout ( 2500 );
48 await page. screenshot ({ path: `${ outDir }/harmony.png` , clip: { x: 0 , y: top, width, height } });
49 await page. getByText ( 'Edit Site' , { exact: true }). first (). click (). catch (() => {});
50 await page. waitForTimeout ( 2500 );
51 if (before) await page. setViewportSize (before);
52
53 // 2) Source at the same size, in the same browser (same fonts and colour handling).
54 const s = await ctx. newPage ();
55 await s. setViewportSize ({ width, height });
56 await s. goto (sourceUrl, { waitUntil: 'networkidle' , timeout: 90000 });
57 await s. waitForTimeout ( 2500 );
58 await s. screenshot ({ path: `${ outDir }/source.png` });
59
60 // 3) Stacked crops for close review (source on top, copy below, magenta divider).
61 const b64 = ( f ) => 'data:image/png;base64,' + fs. readFileSync (f). toString ( 'base64' );
62 for ( const [ i , [ x , y , w , h , sc = 1 ]] of crops. entries ()) {
63 await s. setViewportSize ({ width: Math. round (w * sc), height: Math. round (h * sc * 2 + 10 ) });
64 await s. setContent ( `<body style="margin:0;background:#f0f"><canvas id=c width=${ w * sc } height=${ h * sc * 2 + 10 }></canvas></body>` );
65 await s. evaluate ( async ([ A , B , x , y , w , h , sc ]) => {
66 const c = document. getElementById ( 'c' ). getContext ( '2d' ); c.imageSmoothingEnabled = false ;
67 const load = ( src ) => new Promise (( r ) => { const im = new Image (); im. onload = () => r (im); im.src = src; });
68 const [ a , b ] = await Promise . all ([ load ( A ), load ( B )]);
69 c. drawImage (a, x, y, w, h, 0 , 0 , w * sc, h * sc);
70 c. drawImage (b, x, y, w, h, 0 , h * sc + 10 , w * sc, h * sc);
71 }, [ b64 ( `${ outDir }/source.png` ), b64 ( `${ outDir }/harmony.png` ), x, y, w, h, sc]);
72 await s. screenshot ({ path: `${ outDir }/crop-${ i + 1 }.png` });
73 }
74 await s. close ();
75 console. log ( JSON . stringify ({ previewTop: top, written: [ 'source.png' , 'harmony.png' , ... crops. map (( _ , i ) => `crop-${ i + 1 }.png` )]. map (( f ) => `${ outDir }/${ f }` ) }));
76 await browser. close ();