playwright.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/node
  2. "use strict";
  3. const yargs = require('yargs');
  4. const uuid = require('uuid');
  5. const express = require('express');
  6. const { chromium, firefox, webkit, devices } = require('playwright');
  7. const fs = require('fs');
  8. //TODO use this to define the interface for /command
  9. let rawdata = fs.readFileSync('api.json');
  10. let spec = JSON.parse(rawdata);
  11. //console.log(spec);
  12. //TODO support device commands
  13. const argv = yargs
  14. .command('firefox', 'Starts a playwright instance of firefox', {
  15. firefox: {
  16. description: 'Start a firefox instance',
  17. alias: 'f',
  18. type: 'boolean',
  19. }
  20. })
  21. .command('chrome', 'Starts a playwright instance of chrome', {
  22. chrome: {
  23. description: 'Start a chrome instance',
  24. alias: 'c',
  25. type: 'boolean',
  26. }
  27. })
  28. .command('webkit', 'Starts a playwright instance of webkit', {
  29. webkit: {
  30. description: 'Start a webkit instance',
  31. alias: 'w',
  32. type: 'boolean',
  33. }
  34. })
  35. .option('port', {
  36. alias: 'p',
  37. description: 'Run on specified port',
  38. type: 'number',
  39. })
  40. .option('visible', {
  41. alias: 'v',
  42. description: 'Run with headless mode off',
  43. type: 'boolean',
  44. })
  45. .help()
  46. .alias('help', 'h')
  47. .argv;
  48. const app = express();
  49. const port = argv.port || 6969;
  50. var browser;
  51. var pages = {};
  52. //XXX this is probably a race but I don't care yet
  53. (async () => {
  54. var browser;
  55. if (argv._.includes('firefox')) {
  56. browser = await firefox.launch( { "headless" : !argv.visible } );
  57. }
  58. if (argv._.includes('chrome')) {
  59. browser = await chrome.launch( { "headless" : !argv.visible } );
  60. }
  61. if (argv._.includes('webkit')) {
  62. browser = await webkit.launch( { "headless" : !argv.visible } );
  63. }
  64. if (!browser) {
  65. console.log(argv);
  66. console.log('no browser selected, begone');
  67. process.exit(1);
  68. }
  69. pages.default = await browser.newPage();
  70. pages.default.goto('http://google.com');
  71. console.log('Browser Ready for use');
  72. })();
  73. var results = {};
  74. app.use(express.json())
  75. //app.use(express.urlencoded({ extended: true }))
  76. app.get('/command', (req, res) => {
  77. var payload = req.body;
  78. var page = payload.page;
  79. var command = payload.command;
  80. var result = {};
  81. if (pages[page]) {
  82. if (pages[page][command]) {
  83. //TODO execute, return result
  84. result = { error : false, value : command, type : "page" };
  85. } else {
  86. result = { error : true, message : "Invalid command '" + command + "' to issue to page '" + page + "'." };
  87. }
  88. } else if (browser[command]) {
  89. //TODO execute, return result
  90. result = { error : false, value : command, type : "global" };
  91. } else {
  92. result = { error : true, message : "No such page, or " + command + " is not a globally recognized command for puppeteer" };
  93. }
  94. res.json(result);
  95. });
  96. // XXX this hangs for some reason.
  97. // Maybe I shouldn't care and just send SIGTERM tho
  98. // ^C seems to not leave zommies
  99. app.get('/shutdown', (req, res) => {
  100. (async () => {i
  101. console.log('shutting down...');
  102. await browser.close();
  103. console.log('done');
  104. process.exit(0);
  105. res.send("Sent kill signal to browser");
  106. });
  107. });
  108. //Modulino
  109. if (require.main === module) {
  110. app.listen( port, () => {
  111. console.log(`Listening on port ${port}`);
  112. });
  113. }