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

Side by Side Diff: chrome/common/extensions/docs/templates/articles/devtools.html

Issue 632793002: Docs: devtools extensions. Messaging flow from inject script to devtools (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Created 6 years, 2 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 <h1>Extending DevTools</h1> 1 <h1>Extending DevTools</h1>
2 2
3 <h2 id="overview">Overview</h2> 3 <h2 id="overview">Overview</h2>
4 4
5 <p>A DevTools extension adds functionality to the Chrome DevTools. It can add ne w 5 <p>A DevTools extension adds functionality to the Chrome DevTools. It can add ne w
6 UI panels and sidebars, interact with the inspected page, get information about 6 UI panels and sidebars, interact with the inspected page, get information about
7 network requests, and more. DevTools extensions have access to an additional set 7 network requests, and more. DevTools extensions have access to an additional set
8 of DevTools-specific extension APIs:</p> 8 of DevTools-specific extension APIs:</p>
9 9
10 <ul> 10 <ul>
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 var backgroundPageConnection = chrome.runtime.connect({ 354 var backgroundPageConnection = chrome.runtime.connect({
355 name: "panel" 355 name: "panel"
356 }); 356 });
357 357
358 backgroundPageConnection.postMessage({ 358 backgroundPageConnection.postMessage({
359 name: 'init', 359 name: 'init',
360 tabId: chrome.devtools.inspectedWindow.tabId 360 tabId: chrome.devtools.inspectedWindow.tabId
361 }); 361 });
362 </pre> 362 </pre>
363 363
364
365 <h3 id="evaluated-scripts-to-devtools">Messaging from Injected Scripts to the De vTools Page</h3>
366
367 <p>While the above solution works for content scripts, code that is injected
368 directly into the page (e.g. through appending a <code>&lt;script&gt;</code> tag
369 or through <code>$(ref:inspectedWindow.eval)</code>) requires a different strate gy.
370 In this context, <code>$(ref:runtime.sendMessage)</code> will not pass messages
371 to the background script as expected.</p>
372
373 <p>As a workaround, you can combine your injected script with a content script
374 that acts as an intermediary. To pass messages to the content script, you can
375 use the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window.postMes sage">
376 <code>window.postMessage</code></a> API. Here's an example, assuming the
377 background script from the previous section:</p>
378
379 <pre>
380 // injected-script.js
381
382 window.postMessage({
383 greeting: 'hello there!',
384 source: 'my-devtools-extension'
385 }, '*');
386 </pre>
387
388 <pre>
389 // content-script.js
390
391 window.addEventListener('message', function(event) {
392 // Only accept messages from the same frame
393 if (event.source !== window) {
394 return;
395 }
396
397 var message = event.data;
398
399 // Only accept messages that we know are ours
400 if (typeof message !== 'object' || message === null ||
401 !message.source === 'my-devtools-extension') {
402 return;
403 }
404
405 chrome.runtime.sendMessage(message);
406 });
407 </pre>
408
409 Your message will now flow from the injected script, to the content script, to
410 the background script, and finally to the DevTools page.
411
364 <h3 id="detecting-open-close">Detecting When DevTools Opens and Closes</h3> 412 <h3 id="detecting-open-close">Detecting When DevTools Opens and Closes</h3>
365 413
366 <p>If your extension needs to track whether the DevTools window is open, you can 414 <p>If your extension needs to track whether the DevTools window is open, you can
367 add an $(ref:runtime.onConnect onConnect) listener to the background page, and c all 415 add an $(ref:runtime.onConnect onConnect) listener to the background page, and c all
368 $(ref:runtime.connect connect) from the DevTools page. Since each tab can have i ts 416 $(ref:runtime.connect connect) from the DevTools page. Since each tab can have i ts
369 own DevTools window open, you may receive multiple connect events. To track whet her 417 own DevTools window open, you may receive multiple connect events. To track whet her
370 any DevTools window is open, you need to count the connect and disconnect events as 418 any DevTools window is open, you need to count the connect and disconnect events as
371 shown below:</p> 419 shown below:</p>
372 420
373 <pre>// background.js 421 <pre>// background.js
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 <p> 457 <p>
410 <a href="http://groups.google.com/group/google-chrome-developer-tools/topics">Gi ve 458 <a href="http://groups.google.com/group/google-chrome-developer-tools/topics">Gi ve
411 us feedback!</a> 459 us feedback!</a>
412 Your comments and suggestions help us improve the APIs.</p> 460 Your comments and suggestions help us improve the APIs.</p>
413 461
414 <h2 id="examples">Examples</h2> 462 <h2 id="examples">Examples</h2>
415 463
416 <p>You can find examples that use DevTools APIs in 464 <p>You can find examples that use DevTools APIs in
417 <a href="http://developer.chrome.com/extensions/samples.html#devtools">Samples</ a>. 465 <a href="http://developer.chrome.com/extensions/samples.html#devtools">Samples</ a>.
418 </p> 466 </p>
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698