tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

simple_navigator_clipboard_read.html (2189B)


      1 <!DOCTYPE html>
      2 <html>
      3  <head>
      4  <meta charset="utf-8">
      5  <!-- Required by the .js part of the test. In a more ideal world, the script
      6    could be loaded in the .js part; however, currently, that causes other
      7    problems, which would require other changes in test framework code. -->
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <script src="/tests/SimpleTest/paint_listener.js"></script>
     10  <script src="/tests/gfx/layers/apz/test/mochitest/apz_test_native_event_utils.js"></script>
     11  <script src="/tests/gfx/layers/apz/test/mochitest/apz_test_utils.js"></script>
     12 
     13  <script>
     14    function onLoad() {
     15      const readResult = document.getElementById("readResultId");
     16 
     17      async function getClipboardText() {
     18        let items = await navigator.clipboard.read();
     19        if (items.length != 1) {
     20          throw Error(`incorrect number of clipboard item (${items.length})`);
     21        }
     22 
     23        let item = items[0];
     24        for (let type of item.types) {
     25          if (type == "text/plain") {
     26            let blob = await item.getType(type);
     27            return await blob.text();
     28          }
     29        }
     30 
     31        throw Error("no text/plain type");
     32      }
     33 
     34      const b1 = document.getElementById("invokeReadOnceId");
     35      b1.addEventListener("click", async () => {
     36        getClipboardText().then(text => {
     37          readResult.textContent = `Resolved: ${text}`;
     38        }, (e) => { readResult.textContent = `Rejected: ${e.message}`});
     39      });
     40 
     41      const b2 = document.getElementById("invokeReadTwiceId");
     42      b2.addEventListener("click", async () => {
     43        const t1 = getClipboardText();
     44        const t2 = getClipboardText();
     45 
     46        const r1 = await t1.then(text => {
     47          return `Resolved 1: ${text}`;
     48        }, (e) => { return `Rejected 1: ${e.message}`;});
     49 
     50        const r2 = await t2.then(text => {
     51          return "Resolved 2: " + text;
     52        }, (e) => { return `Rejected 2: ${e.message}`;});
     53 
     54        readResult.textContent = r1 + "; " + r2;
     55      });
     56    }
     57  </script>
     58  </head>
     59  <body onload="onLoad()">
     60   <button id="invokeReadOnceId">1</button>
     61   <button id="invokeReadTwiceId">2</button>
     62   <div id="readResultId"/>
     63  </body>
     64 </html>