{"id":14670,"date":"2020-12-06T00:07:49","date_gmt":"2020-12-06T00:07:49","guid":{"rendered":"https:\/\/www.gmass.co\/blog\/?p=14670"},"modified":"2020-12-12T07:47:08","modified_gmt":"2020-12-12T07:47:08","slug":"timing-gmail-chrome-extension-content-script","status":"publish","type":"post","link":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/","title":{"rendered":"Getting the timing of a Gmail Chrome extension content script just right"},"content":{"rendered":"<p>Today, after having been in this business for more than five years, I realized that a <strong>small percentage of people who installed our GMass Chrome extension never got it to work<\/strong>. Why? Because our <strong>buttons never showed up for them<\/strong>. <em>Why?<\/em> Because the extension&#8217;s &#8220;content script&#8221; never ran. In this article, I&#8217;ll dig into the <strong>mistake I was making<\/strong> and <strong>how I fixed it<\/strong>. If you&#8217;re developing a Chrome extension for Gmail, this is a <strong>critical concept to understand<\/strong>.<\/p>\n<h2>The three timing options for content scripts<\/h2>\n<p>When you designate your content script in your <strong>manifest.json<\/strong> file, there are <strong>three timing options<\/strong> for when your content script should run. They are:<\/p>\n<ul>\n<li>document_idle<\/li>\n<li>document_start<\/li>\n<li>document_end<\/li>\n<\/ul>\n<p>Google <a href=\"https:\/\/developer.chrome.com\/extensions\/content_scripts#run_time:~:text=the-,run_at\" target=\"_blank\" rel=\"noopener noreferrer\">explains<\/a> them in detail. They recommend that most extensions use <strong>document_idle, <\/strong>which fires<strong> in between the time the DOM is loaded and right after the window is loaded<\/strong>. However, if you&#8217;re developing an extension specifically for Gmail where you&#8217;re manipulating the Gmail DOM (Document Object Model), <strong>this will get you into trouble<\/strong>. In fact, all three of these can cause your script never to run. A complicating factor comes into play if you&#8217;re using the awesome <a href=\"https:\/\/www.inboxsdk.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Inbox SDK<\/a> library with your extension.<\/p>\n<p>Inbox SDK recommends that you <a href=\"https:\/\/www.inboxsdk.com\/docs\/#code:~:text=Remote%20App\" target=\"_blank\" rel=\"noopener noreferrer\">remotely load your content script<\/a> via its &#8220;loader&#8221; function. This is a beautiful concept because it allows you to make major changes to your extension <em>without having to update your package with the Chrome Web Store<\/em> and then wait for approval, and then wait again for all of your users&#8217; browsers to get the update. The downside of this approach, however, is that your script could run later than you expect. The big flaw I discovered today was that in my content script, I had all my code wrapped in a window.onload event, like so:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nwindow.onload = function(){\r\nGMassReady();\r\n}\r\n\r\nfunction GMassReady(){...\r\n\r\n}\r\n<\/pre>\n<p>In my <strong>manifest.json<\/strong>, I had my content script set to run at <strong>document_end<\/strong>:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&quot;content_scripts&quot;: [ {\r\n&quot;js&quot;: [ &quot;inboxsdk.js&quot;, &quot;gmass.js&quot;],\r\n&quot;matches&quot;: [ &quot;http:\/\/mail.google.com\/*&quot;, &quot;https:\/\/mail.google.com\/*&quot; ],\r\n&quot;run_at&quot;: &quot;document_end&quot;\r\n} ],\r\n<\/pre>\n<p><strong>This worked for most users, but didn&#8217;t work for some.<\/strong> If your browser <strong>loaded Gmail particularly fast<\/strong> but <strong>loaded my remote content script particularly slowly<\/strong>, then the main code in the content script would never run. <em>Why?<\/em> Because <strong>window.onload<\/strong> would never fire, as it had <em>already<\/em> fired long before the remotely loaded content script was, well&#8230;remotely loaded. So window.onload fired before the script even existed in the browser&#8217;s scope.<\/p>\n<p>Now, <strong>one solution<\/strong> to this problem is simply to <strong>package the script as part of the extension <\/strong>and stop loading it remotely. Then, using the window.onload wrapper would work, because it would guarantee that the script would be available by the time window.onload ran. However, I didn&#8217;t want to do that for the reason stated above.<\/p>\n<h2>How Gmail loads differently<\/h2>\n<p>The confusion for me in reading Google&#8217;s documentation on the <strong>run_at<\/strong> setting is that the rules for Gmail are different. The documentation says that if you&#8217;re using <strong>document_idle<\/strong>, you don&#8217;t need to wait for window.onload in your content script. <em>This is wrong.<\/em> Based on this, you might think you can use <strong>document_end <\/strong>and then call your content script, but this won&#8217;t work well either. That&#8217;s because the way the Gmail interface loads, the <strong>document_end<\/strong> event doesn&#8217;t actually fire when the DOM is ready, as the documentation states. Let&#8217;s prove this.<\/p>\n<p>Here&#8217;s what happens when the script is run locally using document_end, so waiting until after the DOM is supposedly ready.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&quot;run_at&quot;: &quot;document_end&quot;\r\n<\/pre>\n<figure id=\"attachment_14806\" aria-describedby=\"caption-attachment-14806\" style=\"width: 3888px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/001-DOM-is-not-ready-942kb.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-14806 size-full\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/001-DOM-is-not-ready-942kb.png\" alt=\"DOM is not ready\" width=\"3888\" height=\"2288\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/001-DOM-is-not-ready-942kb.png 3888w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/001-DOM-is-not-ready-942kb-300x177.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/001-DOM-is-not-ready-942kb-768x452.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/001-DOM-is-not-ready-942kb-1024x603.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/001-DOM-is-not-ready-942kb-24x14.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/001-DOM-is-not-ready-942kb-36x21.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/001-DOM-is-not-ready-942kb-48x28.png 48w\" sizes=\"auto, (max-width: 3888px) 100vw, 3888px\" \/><\/a><figcaption id=\"caption-attachment-14806\" class=\"wp-caption-text\">Just from the look of Gmail, it&#8217;s obvious that the DOM isn&#8217;t ready when the script fires after document_end.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_14807\" aria-describedby=\"caption-attachment-14807\" style=\"width: 3871px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/002-query-for-DOM-proof-544kb.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-14807 size-full\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/002-query-for-DOM-proof-544kb.png\" alt=\"query for DOM proof\" width=\"3871\" height=\"1416\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/002-query-for-DOM-proof-544kb.png 3871w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/002-query-for-DOM-proof-544kb-300x110.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/002-query-for-DOM-proof-544kb-768x281.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/002-query-for-DOM-proof-544kb-1024x375.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/002-query-for-DOM-proof-544kb-24x9.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/002-query-for-DOM-proof-544kb-36x13.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/002-query-for-DOM-proof-544kb-48x18.png 48w\" sizes=\"auto, (max-width: 3871px) 100vw, 3871px\" \/><\/a><figcaption id=\"caption-attachment-14807\" class=\"wp-caption-text\">To prove that the Gmail DOM isn&#8217;t ready, we&#8217;ll query for an element that I know the Gmail DOM has, the &#8220;search&#8221; box. It&#8217;s not found.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<hr \/>\n<figure id=\"attachment_14808\" aria-describedby=\"caption-attachment-14808\" style=\"width: 2686px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/003-Gmail-UI-is-ready-1800kb.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-14808 size-full\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/003-Gmail-UI-is-ready-1800kb.png\" alt=\"Gmail UI is ready\" width=\"2686\" height=\"1554\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/003-Gmail-UI-is-ready-1800kb.png 2686w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/003-Gmail-UI-is-ready-1800kb-300x174.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/003-Gmail-UI-is-ready-1800kb-768x444.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/003-Gmail-UI-is-ready-1800kb-1024x592.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/003-Gmail-UI-is-ready-1800kb-24x14.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/003-Gmail-UI-is-ready-1800kb-36x21.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/003-Gmail-UI-is-ready-1800kb-48x28.png 48w\" sizes=\"auto, (max-width: 2686px) 100vw, 2686px\" \/><\/a><figcaption id=\"caption-attachment-14808\" class=\"wp-caption-text\">caption: To compare, this is what the Gmail UI looks like when window.onload fires. Clearly the Gmail UI is ready.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_14809\" aria-describedby=\"caption-attachment-14809\" style=\"width: 3861px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/004-Gmail-search-box-element-1700kb.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-14809 size-full\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/004-Gmail-search-box-element-1700kb.png\" alt=\"Gmail search box element\" width=\"3861\" height=\"1410\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/004-Gmail-search-box-element-1700kb.png 3861w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/004-Gmail-search-box-element-1700kb-300x110.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/004-Gmail-search-box-element-1700kb-768x280.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/004-Gmail-search-box-element-1700kb-1024x374.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/004-Gmail-search-box-element-1700kb-24x9.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/004-Gmail-search-box-element-1700kb-36x13.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/004-Gmail-search-box-element-1700kb-48x18.png 48w\" sizes=\"auto, (max-width: 3861px) 100vw, 3861px\" \/><\/a><figcaption id=\"caption-attachment-14809\" class=\"wp-caption-text\">And as expected, after window.onload has fired, the &#8220;search&#8221; box&#8217;s element can easily be found.<\/figcaption><\/figure>\n<h2>Chrome extension world vs. real life<\/h2>\n<p>The &#8220;<strong>document_end<\/strong>&#8221; option corresponds to the real-life JavaScript &#8220;<strong>DOMContentLoaded<\/strong>&#8221; event. Let&#8217;s look at the exact definitions of each.<\/p>\n<p><a href=\"https:\/\/developer.chrome.com\/extensions\/content_scripts#document_end:~:text=run.-,document_end\" target=\"_blank\" rel=\"noopener noreferrer\">document_end<\/a>: &#8220;after the DOM is complete, but before subresources like images and frames have loaded.&#8221;<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/readyState\" target=\"_blank\" rel=\"noopener noreferrer\">DOMContentLoaded<\/a>: &#8220;The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.&#8221;<\/p>\n<p>See the similarities? So theoretically, if you are using a <strong>local<\/strong> content script, and you used <strong>document_start<\/strong> in <strong>manifest.json<\/strong> and wrapped your content script code inside a <strong>DOMContentLoaded<\/strong> event, that is the <em>same<\/em> thing as using <strong>document_end<\/strong> in your <strong>manifest.json<\/strong> and <strong>not wrapping<\/strong> your script inside a <strong>DOMContentLoaded<\/strong> event. If you&#8217;re using a remote content script, these rules go out the window because you simply don&#8217;t know how long it will take to load your script from its server. It&#8217;s <strong>possible that your script loads after the DOMContentLoaded event<\/strong> has already fired, and so wrapping your code in that event would <strong>cause it never to run<\/strong>.<\/p>\n<h2>So what&#8217;s the best approach?<\/h2>\n<p>The optimal setting, then, regardless of whether your content script is local or remote, is to set the script to <strong>document_start<\/strong> in <strong>manifest.json<\/strong> and then in your actual content script, wrap your code in an if\/then condition based on whether or not window.onload has fired yet. By using <strong>document_start<\/strong>, it gets your script into the browser&#8217;s context as quickly as possible, and by using the\u00a0 if\/then window.onload logic, it allows your script to run after it&#8217;s loaded if Gmail is ready or wait until the Gmail interface is ready. You <em>could<\/em> use <strong>document_idle<\/strong> or <strong>document_end<\/strong>, but that will just delay the inevitable&#8230;your script running. As long as you&#8217;re wrapped in a window.onload if\/then checking system, you&#8217;re good.<\/p>\n<p>Here&#8217;s the code:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nif (document.readyState === &quot;complete&quot;)\r\n{\r\nGMassReady();\r\n}\r\nelse\r\n{\r\nwindow['onload'] = function () {\r\nGMassReady();\r\n}\r\n}\r\n\r\nfunction GMassReady(){...\r\n<\/pre>\n<p><em><strong>Note:<\/strong><\/em> You might notice that my extension still uses <strong>document_end<\/strong>. <em>Why?<\/em> Because I haven&#8217;t updated my Chrome extension package yet in the Chrome Web Store. A few years ago, it was a simple process. You just uploaded a new package, and the Chrome Web Store updated \u2014 then worldwide, everyone&#8217;s browsers updated over the next couple of days. Now there&#8217;s a strict human review process before changes go live. Since I was able to fix my issue by altering my content script to include the <strong>new window.onload logic<\/strong>, that suffices for now.<\/p>\n<h2>What do other Gmail Chrome extensions do?<\/h2>\n<p>It turns out <strong>none of the other major Gmail extensions I tested<\/strong> use the default <strong>document_idle<\/strong> setting. As with many things in life, it seems I was the last to know about this. Let&#8217;s look at the manifest.json files of a couple of other extensions.<\/p>\n<p><strong>Mailtrack<\/strong><\/p>\n<p>Here&#8217;s the relevant portion of manifest.json:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ncontent_scripts&quot;: [\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;https:\/\/mail.google.com\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;scripts\/lib\/intercom-snippet.js&quot;,\r\n                &quot;scripts\/lib\/snowplowSnippet.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_start&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;https:\/\/mail.google.com\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;scripts\/gmail.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_start&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;https:\/\/mail.google.com\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;scripts\/bundles\/gmail.start.bundle.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_start&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;https:\/\/mail.google.com\/*&quot;\r\n            ],\r\n            &quot;css&quot;: [\r\n                &quot;styles\/style.css&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_end&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;https:\/\/mail.google.com\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;scripts\/bundles\/gmail.end.bundle.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_end&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;*:\/\/mailtrack.io\/*\/dashboard\/welcome*&quot;,\r\n                &quot;*:\/\/mailtrack.io\/*\/dashboard\/reauthorized*&quot;,\r\n                &quot;*:\/\/mailtrack.io\/*\/dashboard\/install-success*&quot;,\r\n                &quot;*:\/\/mailtrack.io\/*\/dashboard\/payment\/teams\/success*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;scripts\/bundles\/setup.bundle.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_start&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;*:\/\/mailtrack.io\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;scripts\/bundles\/dashboard.bundle.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_end&quot;\r\n        }\r\n    ],\r\n<\/pre>\n<p>I haven&#8217;t dug into each content script here, but we can see that none of them use Google&#8217;s recommendation of document_idle.<\/p>\n<p><strong>Mixmax<\/strong><\/p>\n<p>Here&#8217;s a pertinent snippet of manifest.json:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&quot;content_scripts&quot;: [\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;*:\/\/*.mixmax.com\/*&quot;\r\n            ],\r\n            &quot;exclude_matches&quot;: [\r\n                &quot;*:\/\/*.mixmax.com\/public\/analyticsbridge.html&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;src\/content\/globals.js&quot;,\r\n                &quot;src\/assets\/lib\/raven-3.3.0.js&quot;,\r\n                &quot;src\/assets\/lib\/Environment.js&quot;,\r\n                &quot;src\/assets\/lib\/raven-config.js&quot;,\r\n                &quot;src\/assets\/lib\/error.js&quot;,\r\n                &quot;src\/content\/ExtensionMessageBus.js&quot;\r\n            ],\r\n            &quot;all_frames&quot;: true,\r\n            &quot;run_at&quot;: &quot;document_start&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;*:\/\/mail.google.com\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;src\/content\/unblock.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_start&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;*:\/\/mail.google.com\/*&quot;,\r\n                &quot;*:\/\/*.force.com\/*&quot;,\r\n                &quot;*:\/\/*.salesforce.com\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;src\/content\/globals.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_start&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;*:\/\/mail.google.com\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;src\/content\/pageInterop.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_end&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;*:\/\/mail.google.com\/*&quot;,\r\n                &quot;*:\/\/*.force.com\/*&quot;,\r\n                &quot;*:\/\/*.salesforce.com\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;src\/assets\/lib\/raven-3.3.0.js&quot;,\r\n                &quot;src\/assets\/lib\/Environment.js&quot;,\r\n                &quot;src\/assets\/lib\/raven-config.js&quot;,\r\n                &quot;src\/assets\/lib\/error.js&quot;,\r\n                &quot;src\/content\/ExtensionMessageBus.js&quot;,\r\n                &quot;src\/content\/app.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_end&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;*:\/\/www.linkedin.com\/sales\/widget\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;src\/content\/globals.js&quot;\r\n            ],\r\n            &quot;all_frames&quot;: true,\r\n            &quot;run_at&quot;: &quot;document_start&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;*:\/\/www.linkedin.com\/sales\/widget\/*&quot;\r\n            ],\r\n            &quot;js&quot;: [\r\n                &quot;src\/assets\/lib\/raven-3.3.0.js&quot;,\r\n                &quot;src\/assets\/lib\/Environment.js&quot;,\r\n                &quot;src\/assets\/lib\/raven-config.js&quot;,\r\n                &quot;src\/assets\/lib\/error.js&quot;,\r\n                &quot;src\/content\/app.js&quot;\r\n            ],\r\n            &quot;all_frames&quot;: true,\r\n            &quot;run_at&quot;: &quot;document_end&quot;\r\n        },\r\n        {\r\n            &quot;matches&quot;: [\r\n                &quot;&amp;lt;all_urls&amp;gt;&quot;\r\n            ],\r\n            &quot;exclude_matches&quot;: [\r\n                &quot;*:\/\/mail.google.com\/*&quot;\r\n            ],\r\n            &quot;all_frames&quot;: true,\r\n            &quot;js&quot;: [\r\n                &quot;src\/content\/mailTo.js&quot;,\r\n                &quot;src\/content\/callTo.js&quot;\r\n            ],\r\n            &quot;run_at&quot;: &quot;document_idle&quot;\r\n        }\r\n    ],\r\n<\/pre>\n<p>Again I haven&#8217;t dug into each one, but none of them use document_idle except the last one, which is the one script that does not run inside Gmail.<\/p>\n<h2>In Conclusion&#8230;<\/h2>\n<p>Developing an extension for Gmail is different than for other sites. We love Inbox SDK, and we love the ability to load content scripts remotely, but it&#8217;s important to get the timing right. Set your manifest.json to use document_start and wrap your content script code in an if\/then that checks for the window.load event.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, after having been in this business for more than five years, I realized that a small percentage of people who installed our GMass Chrome extension never got\u2026<\/p>\n","protected":false},"author":2,"featured_media":14804,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3770,3462],"tags":[],"class_list":["post-14670","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chrome-extensions","category-developers"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\r\n<title>Getting the timing of a Gmail Chrome extension content script just right<\/title>\r\n<meta name=\"description\" content=\"If you&#039;re developing a Chrome extension for Gmail, you should know that Google&#039;s documentation on when a content script runs is wrong.\" \/>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"Getting the timing of a Gmail Chrome extension content script just right\" \/>\r\n<meta property=\"og:description\" content=\"If you&#039;re developing a Chrome extension for Gmail, you should know that Google&#039;s documentation on when a content script runs is wrong.\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/\" \/>\r\n<meta property=\"og:site_name\" content=\"GMass Blog\" \/>\r\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GmailMailMerge\/\" \/>\r\n<meta property=\"article:published_time\" content=\"2020-12-06T00:07:49+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2020-12-12T07:47:08+00:00\" \/>\r\n<meta property=\"og:image\" content=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/Featured-image-Gmail-Chrome-Extension-Content-Script-90kb.png\" \/>\r\n\t<meta property=\"og:image:width\" content=\"1001\" \/>\r\n\t<meta property=\"og:image:height\" content=\"467\" \/>\r\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\r\n<meta name=\"author\" content=\"Ajay Goel\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:creator\" content=\"@PartTimeSnob\" \/>\r\n<meta name=\"twitter:site\" content=\"@GMassForGmail\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ajay Goel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/\"},\"author\":{\"name\":\"Ajay Goel\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#\\\/schema\\\/person\\\/b5fa74f8765b860701158fd77162fff8\"},\"headline\":\"Getting the timing of a Gmail Chrome extension content script just right\",\"datePublished\":\"2020-12-06T00:07:49+00:00\",\"dateModified\":\"2020-12-12T07:47:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/\"},\"wordCount\":1987,\"commentCount\":1,\"image\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/Featured-image-Gmail-Chrome-Extension-Content-Script-90kb.png\",\"articleSection\":[\"Chrome Extensions\",\"Developers\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/\",\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/\",\"name\":\"Getting the timing of a Gmail Chrome extension content script just right\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/Featured-image-Gmail-Chrome-Extension-Content-Script-90kb.png\",\"datePublished\":\"2020-12-06T00:07:49+00:00\",\"dateModified\":\"2020-12-12T07:47:08+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#\\\/schema\\\/person\\\/b5fa74f8765b860701158fd77162fff8\"},\"description\":\"If you're developing a Chrome extension for Gmail, you should know that Google's documentation on when a content script runs is wrong.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/Featured-image-Gmail-Chrome-Extension-Content-Script-90kb.png\",\"contentUrl\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/Featured-image-Gmail-Chrome-Extension-Content-Script-90kb.png\",\"width\":1001,\"height\":467,\"caption\":\"Gmail Chrome Extension Content Script\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/timing-gmail-chrome-extension-content-script\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Getting the timing of a Gmail Chrome extension content script just right\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/\",\"name\":\"GMass Blog\",\"description\":\"Tips and tricks for sending mail merge and mass email campaigns directly from Gmail\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#\\\/schema\\\/person\\\/b5fa74f8765b860701158fd77162fff8\",\"name\":\"Ajay Goel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g\",\"caption\":\"Ajay Goel\"},\"description\":\"Ajay is the founder of GMass and has been developing email sending software for 20 years.\",\"sameAs\":[\"https:\\\/\\\/twitter.com\\\/PartTimeSnob\",\"https:\\\/\\\/x.com\\\/PartTimeSnob\"],\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/author\\\/ajay-goel\\\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Getting the timing of a Gmail Chrome extension content script just right","description":"If you're developing a Chrome extension for Gmail, you should know that Google's documentation on when a content script runs is wrong.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/","og_locale":"en_US","og_type":"article","og_title":"Getting the timing of a Gmail Chrome extension content script just right","og_description":"If you're developing a Chrome extension for Gmail, you should know that Google's documentation on when a content script runs is wrong.","og_url":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/","og_site_name":"GMass Blog","article_publisher":"https:\/\/www.facebook.com\/GmailMailMerge\/","article_published_time":"2020-12-06T00:07:49+00:00","article_modified_time":"2020-12-12T07:47:08+00:00","og_image":[{"width":1001,"height":467,"url":"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/Featured-image-Gmail-Chrome-Extension-Content-Script-90kb.png","type":"image\/png"}],"author":"Ajay Goel","twitter_card":"summary_large_image","twitter_creator":"@PartTimeSnob","twitter_site":"@GMassForGmail","twitter_misc":{"Written by":"Ajay Goel","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/#article","isPartOf":{"@id":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/"},"author":{"name":"Ajay Goel","@id":"https:\/\/www.gmass.co\/blog\/#\/schema\/person\/b5fa74f8765b860701158fd77162fff8"},"headline":"Getting the timing of a Gmail Chrome extension content script just right","datePublished":"2020-12-06T00:07:49+00:00","dateModified":"2020-12-12T07:47:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/"},"wordCount":1987,"commentCount":1,"image":{"@id":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/#primaryimage"},"thumbnailUrl":"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/Featured-image-Gmail-Chrome-Extension-Content-Script-90kb.png","articleSection":["Chrome Extensions","Developers"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/","url":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/","name":"Getting the timing of a Gmail Chrome extension content script just right","isPartOf":{"@id":"https:\/\/www.gmass.co\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/#primaryimage"},"image":{"@id":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/#primaryimage"},"thumbnailUrl":"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/Featured-image-Gmail-Chrome-Extension-Content-Script-90kb.png","datePublished":"2020-12-06T00:07:49+00:00","dateModified":"2020-12-12T07:47:08+00:00","author":{"@id":"https:\/\/www.gmass.co\/blog\/#\/schema\/person\/b5fa74f8765b860701158fd77162fff8"},"description":"If you're developing a Chrome extension for Gmail, you should know that Google's documentation on when a content script runs is wrong.","breadcrumb":{"@id":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/#primaryimage","url":"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/Featured-image-Gmail-Chrome-Extension-Content-Script-90kb.png","contentUrl":"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2020\/12\/Featured-image-Gmail-Chrome-Extension-Content-Script-90kb.png","width":1001,"height":467,"caption":"Gmail Chrome Extension Content Script"},{"@type":"BreadcrumbList","@id":"https:\/\/www.gmass.co\/blog\/timing-gmail-chrome-extension-content-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.gmass.co\/blog\/"},{"@type":"ListItem","position":2,"name":"Getting the timing of a Gmail Chrome extension content script just right"}]},{"@type":"WebSite","@id":"https:\/\/www.gmass.co\/blog\/#website","url":"https:\/\/www.gmass.co\/blog\/","name":"GMass Blog","description":"Tips and tricks for sending mail merge and mass email campaigns directly from Gmail","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.gmass.co\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.gmass.co\/blog\/#\/schema\/person\/b5fa74f8765b860701158fd77162fff8","name":"Ajay Goel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g","caption":"Ajay Goel"},"description":"Ajay is the founder of GMass and has been developing email sending software for 20 years.","sameAs":["https:\/\/twitter.com\/PartTimeSnob","https:\/\/x.com\/PartTimeSnob"],"url":"https:\/\/www.gmass.co\/blog\/author\/ajay-goel\/"}]}},"_links":{"self":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts\/14670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/comments?post=14670"}],"version-history":[{"count":20,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts\/14670\/revisions"}],"predecessor-version":[{"id":14814,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts\/14670\/revisions\/14814"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/media\/14804"}],"wp:attachment":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/media?parent=14670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/categories?post=14670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/tags?post=14670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}