Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1255)

Unified Diff: native_client_sdk/src/build_tools/screenshot_extension/injected.js

Issue 23458018: [NaCl SDK] Screen capture extension for use with SDK visual testing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix nits Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: native_client_sdk/src/build_tools/screenshot_extension/injected.js
diff --git a/native_client_sdk/src/build_tools/screenshot_extension/injected.js b/native_client_sdk/src/build_tools/screenshot_extension/injected.js
new file mode 100644
index 0000000000000000000000000000000000000000..f90e233b67896a22dd64989222f57be60d739e85
--- /dev/null
+++ b/native_client_sdk/src/build_tools/screenshot_extension/injected.js
@@ -0,0 +1,75 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Only inject once.
+if (!document.body.getAttribute('screenshot_extension_injected')) {
+ document.body.setAttribute('screenshot_extension_injected', true);
+ (function() {
+
+ // Bounce message from webpage to background page.
+ //
+ // Expecting a message called with:
+ // window.postMessage({
+ // id: <a value that is passed back unchanged to the response for
+ // identification>,
+ // target: 'background'
+ // }, '*');
+ //
+ // When the screenshot is captured, a message will be posted to the window.
+ // Listen for it like this:
+ //
+ // window.addEventListener('message', function(event) {
+ // if (event.source !== window)
+ // return;
+ //
+ // if (event.data.target !== 'page')
+ // return;
+ //
+ // // event.data is an object:
+ // // {
+ // // id: <the id passed to the request>,
+ // // target: 'page',
+ // // data: <a data URI of MIMEtype image/png with the tab screenshot>
+ // // }
+ // //
+ // // or if there is an error:
+ // //
+ // // {
+ // // id: <the id passed to the request>,
+ // // target: 'page',
+ // // error: <an error string>
+ // // }
+ // }, false);
+ //
+ window.addEventListener('message', function(event) {
+ if (event.source !== window)
+ return;
+
+ // Ignore messages not destined for the background page.
+ if (event.data.target !== 'background')
+ return;
+
+ var id = event.data.id;
+ console.log('sending message: id=' + id);
+
+ chrome.runtime.sendMessage(null, {},
+ function(responseData) {
+ // Bounce response from background page back to webpage.
+ var lastError = chrome.runtime.lastError;
+ if (lastError) {
+ console.log('lastError: ' + lastError);
+
+ window.postMessage({id: id, target: 'page', error: lastError},
+ '*');
+ return;
+ }
+
+ console.log('received response: id=' + id);
+
+ window.postMessage({id: id, target: 'page', data: responseData},
+ '*');
+ });
+ }, false);
+ })();
+}

Powered by Google App Engine
This is Rietveld 408576698