Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two hurdles to be taken before a Typekit font can be used in a Chrome extension:</p> <ol> <li>By default, Chrome prevents external scripts from being loaded in the extension's process.</li> <li>Chrome prevents inline scripts from being executed.</li> <li>Typekit will reply with 403 Forbidden, unless the correct Referer header is set.</li> </ol> <hr> <p>I uploaded the source code of a demo to <a href="https://robwu.nl/typekit-demo.zip">https://robwu.nl/typekit-demo.zip</a>. To see that the extension works, open the options page of the sample extension.</p> <hr> <p>For the next example, I have created a Typekit kit with the following settings:</p> <p><img src="https://i.stack.imgur.com/1FAE1.png" alt="https://typekit.com/kit_editor/kits/ekl0khv Name: Chrome extension demo Domains: hjdaoalallnjkokclafeljbcmpogfiig"></p> <p>To fix the first problem, edit your manifest file and relax the <a href="http://developer.chrome.com/extensions/contentSecurityPolicy.html">Content Security policy</a>:</p> <pre><code>"content_security_policy": "script-src 'self' https://use.typekit.net; object-src 'self'" </code></pre> <p>To solve the second issue, move the loader to an external file. Your extension page should look like this:</p> <pre class="lang-html prettyprint-override"><code> &lt;script src="https://use.typekit.net/ekl0khv.js"&gt;&lt;/script&gt; &lt;script src="load-typekit.js"&gt;&lt;/script&gt; </code></pre> <pre class="lang-js prettyprint-override"><code>// load-typekit.js try{Typekit.load();}catch(e){} </code></pre> <p>To solve the last issue, you need to modify the Referer header. This bit of code requires the <code>webRequest</code>, <code>webRequestBlocking</code> and <code>*://use.typekit.net/</code> permissions in the manifest file; <a href="https://developer.chrome.com/extensions/webRequest.html#event-onBeforeSendHeaders"><code>chrome.webRequest.onBeforeSendHeaders</code></a> is used to modify the headers.</p> <pre><code>// background.js chrome.webRequest.onBeforeSendHeaders.addListener(function(details) { var requestHeaders = details.requestHeaders; for (var i=0; i&lt;requestHeaders.length; ++i) { if (requestHeaders[i].name.toLowerCase() === 'referer') { // The request was certainly not initiated by a Chrome extension... return; } } // Set Referer requestHeaders.push({ name: 'referer', // Host must match the domain in your Typekit kit settings value: 'https://hjdaoalallnjkokclafeljbcmpogfiig/' }); return { requestHeaders: requestHeaders }; }, { urls: ['*://use.typekit.net/*'], types: ['stylesheet'] }, ['requestHeaders','blocking']); </code></pre> <h2>Minimal manifest.json</h2> <p>This is the minimal <a href="https://developer.chrome.com/extensions/manifest.html"><code>manifest.json</code></a> file for getting the extension to work. <code>background.js</code> is referenced in the previous section.</p> <pre><code>{ "name": "Name of extension", "version": "1", "manifest_version": 2, "background": { "scripts": ["background.js"] }, "permissions": [ "*://use.typekit.net/*", "webRequest", "webRequestBlocking" ], "content_security_policy": "script-src 'self' https://use.typekit.net; object-src 'self'" } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload