Browse Source

Fix #7: Make scripts work with caveats

George S. Baugh 5 năm trước cách đây
mục cha
commit
3c62fb3c33
3 tập tin đã thay đổi với 39 bổ sung7 xóa
  1. 12 0
      bin/playwright.js
  2. 5 6
      example.pl
  3. 22 1
      lib/Playwright.pm

+ 12 - 0
bin/playwright.js

@@ -83,6 +83,18 @@ app.post('/command', async (req, res) => {
 
     if (subject && spec[type] && spec[type].members[command]) {
         try {
+
+            //XXX We have to do a bit of 'special' handling for scripts
+            // This has implications for the type of scripts you can use
+            if (command == 'evaluate' || command == 'evaluateHandle') {
+                var toEval = args.shift();
+                const fun = new Function (toEval);
+                args = [
+                    fun,
+                    ...args
+                ];
+            }
+
             const res = await subject[command](...args);
             result = { error : false, message : res };
 

+ 5 - 6
example.pl

@@ -35,16 +35,15 @@ print Dumper($cookies);
 my $frameset = $page->mainFrame();
 print Dumper($frameset->childFrames());
 
-# Run some JS XXX not yet working unfortunately
+# Run some JS
 my $fun = "
-    (input) => {
-      return {
+    var input = arguments[0];
+    return {
         width: document.documentElement.clientWidth,
         height: document.documentElement.clientHeight,
         deviceScaleFactor: window.devicePixelRatio,
-        arguments: input
-      }
-    }";
+        arg: input
+    };";
 my $result = $page->evaluate($fun, 'zippy');
 print Dumper($result);
 

+ 22 - 1
lib/Playwright.pm

@@ -42,7 +42,10 @@ Currently understands commands you can send to all the playwright classes define
 See L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=>
 for what the classes do, and their usage.
 
-There is one major exception in how things work versus the documentation.
+There are two major exceptions in how things work versus the documentation.
+
+=head3 Selectors
+
 The selector functions have to be renamed from starting with $ for obvious reasons.
 The renamed functions are as follows:
 
@@ -60,6 +63,24 @@ The renamed functions are as follows:
 
 These functions are present as part of the Page, Frame and ElementHandle classes.
 
+=head3 Scripts
+
+The evaluate() and evaluateHandle() functions can only be run in string mode.
+To maximize the usefulness of these, I have wrapped the string passed with the following function:
+
+    const fun = new Function (toEval);
+    args = [
+        fun,
+        ...args
+    ];
+
+As such you can effectively treat the script string as a function body.
+The same restriction on only being able to pass one arg remains from the upstream:
+L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=pageevaluatepagefunction-arg>
+
+You will have to refer to the arguments array as described here:
+L<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments> 
+
 =head1 CONSTRUCTOR
 
 =head2 new(HASH) = (Playwright)