Home | History | Annotate | Download | only in intros
      1 <h2 id="usage">Usage</h2>
      2 
      3 <p>
      4 The Declarative Content API allows you to show your extension's
      5 $ref:[pageAction page action] depending on the URL of a web page and
      6 the CSS selectors its content matches, without needing to take a <a
      7 href="declare_permissions.html#host-permission">host permission</a> or
      8 inject a <a href="content_scripts.html">content script</a>.  Use the
      9 <a href="activeTab.html">activeTab</a> permission in order to be able
     10 to interact with a page after the user clicks on your page action.
     11 </p>
     12 
     13 <p>
     14 If you need more precise control over when your page action appears or
     15 you need to change its appearance to match the current tab before the
     16 user clicks on it, you'll have to keep using the $ref:pageAction API.
     17 </p>
     18 
     19 <h2 id="rules">Rules</h2>
     20 
     21 <p>As a <a href="events.html#declarative">declarative API</a>, this
     22 API lets you register rules on the
     23 <code>$ref:declarativeContent.onPageChanged</code> event object which
     24 take an action (currently just <code>$ref:ShowPageAction</code>) when
     25 a set of conditions, represented as a
     26 <code>$ref:[PageStateMatcher]</code>, are met.
     27 </p>
     28 
     29 <p>
     30 The <code>$ref:PageStateMatcher</code> matches web pages if and only
     31 if all listed criteria are met. The following rule would show a page
     32 action for pages on <code>https://www.google.com/</code> when a
     33 password field is present on it:
     34 </p>
     35 
     36 <pre>
     37 var rule1 = {
     38   conditions: [
     39     new $ref:[declarativeContent.PageStateMatcher chrome.declarativeContent.PageStateMatcher]({
     40       $ref:[PageStateMatcher.pageUrl pageUrl]: { $ref:[events.UrlFilter.hostEquals hostEquals]: 'www.google.com', $ref:[events.UrlFilter.schemes schemes]: ['https'] },
     41       $ref:[PageStateMatcher.css css]: ["input[type='password']"]
     42     })
     43   ],
     44   actions: [ new $ref:[declarativeContent.ShowPageAction chrome.declarativeContent.ShowPageAction]() ]
     45 };
     46 </pre>
     47 
     48 <p class="note">
     49 <strong>Note:</strong> All conditions and actions are created via a constructor
     50 as shown in the example above.
     51 <p>
     52 
     53 <p>
     54 In order to also show a page action for sites with a video, you can
     55 add a second condition, as each condition is sufficient to trigger all
     56 specified actions:
     57 </p>
     58 <pre>
     59 var rule2 = {
     60   conditions: [
     61     new $ref:[declarativeContent.PageStateMatcher chrome.declarativeContent.PageStateMatcher]({
     62       $ref:[PageStateMatcher.pageUrl pageUrl]: { $ref:[events.UrlFilter.hostEquals hostEquals]: 'www.google.com', $ref:[events.UrlFilter.schemes schemes]: ['https'] },
     63       $ref:[PageStateMatcher.css css]: ["input[type='password']"]
     64     })<strong>,
     65     new chrome.declarativeContent.PageStateMatcher({
     66       $ref:[PageStateMatcher.css css]: ["video"]
     67     })</strong>
     68   ],
     69   actions: [ new $ref:[declarativeContent.ShowPageAction chrome.declarativeContent.ShowPageAction]() ]
     70 };
     71 </pre>
     72 
     73 <p>
     74 <a href="events.html#addingrules">Added rules</a> are saved across
     75 browser restarts, so register them as follows:
     76 </p>
     77 <pre>
     78 $ref:[runtime.onInstalled chrome.runtime.onInstalled].addListener(function(details) {
     79   $ref:[declarativeContent.onPageChanged chrome.declarativeContent.onPageChanged].<a href="events.html#removingrules">removeRules</a>(undefined, function() {
     80     $ref:[declarativeContent.onPageChanged chrome.declarativeContent.onPageChanged].<a href="events.html#addingrules">addRules</a>([rule2]);
     81   });
     82 });
     83 </pre>
     84 
     85 <p class="note">
     86 <strong>Note:</strong> You should always register or unregister rules in bulk
     87 rather than individually because each of these operations recreates internal
     88 data structures.  This re-creation is computationally expensive but facilitates
     89 a faster matching algorithm.
     90 </p>
     91 
     92 <p>
     93 Combine the above rule with the <a href="activeTab.html">activeTab</a>
     94 permission to create an extension that doesn't need any install-time
     95 permissions but can invite the user to click its page action on
     96 relevant pages and can run on those pages when the user clicks the
     97 page action.
     98 </p>
     99