@guanzhu.me/pw-cli 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/pw-cli.js +19 -15
- package/package.json +1 -1
package/bin/pw-cli.js
CHANGED
|
@@ -744,29 +744,26 @@ async function main() {
|
|
|
744
744
|
}
|
|
745
745
|
}
|
|
746
746
|
|
|
747
|
-
// ── goto: navigate the
|
|
747
|
+
// ── goto: navigate the tracked active tab ────────────────────────────────
|
|
748
748
|
if (command === 'goto') {
|
|
749
749
|
const gotoIdx = rawArgv.indexOf('goto');
|
|
750
750
|
const afterGoto = rawArgv.slice(gotoIdx + 1);
|
|
751
751
|
const rawUrl = afterGoto.find(a => !a.startsWith('-'));
|
|
752
752
|
if (rawUrl) {
|
|
753
753
|
const fullUrl = /^https?:\/\//.test(rawUrl) ? rawUrl : `https://${rawUrl}`;
|
|
754
|
+
const activeTabFile = JSON.stringify(path.join(PW_CLI_DIR, 'active-tab.json'));
|
|
754
755
|
const navCode = `async (page, context) => {
|
|
755
756
|
const pages = context.pages();
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
return { p, hidden, focused };
|
|
763
|
-
} catch { return { p, hidden: true, focused: false }; }
|
|
764
|
-
}));
|
|
765
|
-
const focused = info.find(v => v.focused);
|
|
766
|
-
const visible = info.filter(v => !v.hidden);
|
|
767
|
-
const target = (focused || visible[visible.length - 1] || info[info.length - 1]).p;
|
|
757
|
+
let target = pages[pages.length - 1] || page;
|
|
758
|
+
try {
|
|
759
|
+
const saved = JSON.parse(require('fs').readFileSync(${activeTabFile}, 'utf8'));
|
|
760
|
+
const match = pages.find(p => p.url() === saved.url);
|
|
761
|
+
if (match) target = match;
|
|
762
|
+
} catch {}
|
|
768
763
|
await target.goto(${JSON.stringify(fullUrl)}, { waitUntil: 'domcontentloaded', timeout: 0 });
|
|
769
|
-
|
|
764
|
+
const resultUrl = target.url();
|
|
765
|
+
try { require('fs').writeFileSync(${activeTabFile}, JSON.stringify({ url: resultUrl })); } catch {}
|
|
766
|
+
return resultUrl;
|
|
770
767
|
}`;
|
|
771
768
|
await ensureBrowserRunning();
|
|
772
769
|
await handleRunCode(['run-code', navCode]);
|
|
@@ -815,7 +812,14 @@ async function main() {
|
|
|
815
812
|
const rawUrlArg = afterOpen.find(a => !a.startsWith('-') && /^(https?:\/\/|[a-zA-Z0-9]([a-zA-Z0-9-]*\.)+[a-zA-Z]{2,})/.test(a));
|
|
816
813
|
const urlArg = rawUrlArg && !/^https?:\/\//.test(rawUrlArg) ? `https://${rawUrlArg}` : rawUrlArg;
|
|
817
814
|
if (urlArg) {
|
|
818
|
-
const
|
|
815
|
+
const activeTabFile = JSON.stringify(path.join(PW_CLI_DIR, 'active-tab.json'));
|
|
816
|
+
const navCode = `async page => {
|
|
817
|
+
const newPage = await page.context().newPage();
|
|
818
|
+
await newPage.goto(${JSON.stringify(urlArg)}, { waitUntil: 'domcontentloaded', timeout: 0 });
|
|
819
|
+
const resultUrl = newPage.url();
|
|
820
|
+
try { require('fs').writeFileSync(${activeTabFile}, JSON.stringify({ url: resultUrl })); } catch {}
|
|
821
|
+
return resultUrl;
|
|
822
|
+
}`;
|
|
819
823
|
await ensureBrowserRunning();
|
|
820
824
|
await handleRunCode(['run-code', navCode]);
|
|
821
825
|
return;
|
package/package.json
CHANGED