OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Return the information of asynchronous script execution. |
| 7 * |
| 8 * @return {Object.<string, ?>} Information of asynchronous script execution. |
| 9 */ |
| 10 function getAsyncScriptInfo() { |
| 11 var key = 'chromedriverAsyncScriptInfo'; |
| 12 if (!(key in document)) |
| 13 document[key] = {'id': 0, 'finished': false}; |
| 14 return document[key]; |
| 15 } |
| 16 |
| 17 /** |
| 18 * Execute the given script and save its asynchronous result. |
| 19 * |
| 20 * If script1 finishes after script2 is executed, then script1's result will be |
| 21 * discarded while script2's will be saved. |
| 22 * |
| 23 * @param {!string} script The asynchronous script to be executed. |
| 24 * @param {Array.<*>} args Arguments to be passed to the script. |
| 25 */ |
| 26 function executeAsyncScript(script, args) { |
| 27 var info = getAsyncScriptInfo(); |
| 28 info.id++; |
| 29 info.finished = false; |
| 30 delete info.result; |
| 31 var id = info.id; |
| 32 |
| 33 function callback(result) { |
| 34 if (id == info.id) { |
| 35 info.result = result; |
| 36 info.finished = true; |
| 37 } |
| 38 } |
| 39 args.push(callback); |
| 40 |
| 41 try { |
| 42 new Function(script).apply(null, args); |
| 43 } catch (error) { |
| 44 error.code = 17; // Error code for JavaScriptError. |
| 45 throw error; |
| 46 } |
| 47 } |
OLD | NEW |