{"flag":true,"single":true,"pageTitle":"Basic Extension, Content Scripts,","post":{"id":263,"user_id":"1","slug":"basic-extension-ocr6","title":"Basic Extension, Content Scripts,","body":"<p>1. create a new file in directory called <strong>manifest.json<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>{\r\n  \"name\": \"Hello Extensions\",\r\n  \"description\": \"Base Level Extension\",\r\n  \"version\": \"1.0\",\r\n  \"manifest_version\": 3,\r\n  \"action\": {\r\n    \"default_popup\": \"hello.html\",\r\n    \"default_icon\": \"icon.png\"\r\n  }\r\n}<\/code><\/pre>\r\n<p>2. get <strong>icon.png<\/strong> from <strong><a href=\"https:\/\/developer.chrome.com\/static\/docs\/extensions\/get-started\/tutorial\/hello-world\/image\/icon.png\">https:\/\/developer.chrome.com\/static\/docs\/extensions\/get-started\/tutorial\/hello-world\/image\/icon.png<\/a><br><\/strong><\/p>\r\n<p>3. create <strong>hello.html<\/strong> file in same directory<\/p>\r\n<pre class=\"language-markup\"><code>&lt;html&gt;\r\n  &lt;body&gt;\r\n    &lt;h1&gt;Hello Extensions&lt;\/h1&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\r\n<p><strong>4. Load an unpacked extension&nbsp;<\/strong><br>type <a href=\"chrome:\/\/extensions\">chrome:\/\/extensions<\/a> in url and enable devloper option<\/p>\r\n<p>5.<br>Click the Load unpacked button and select the extension directory.<br><br><\/p>\r\n<p><strong>ICONS USAGE:<\/strong> Edit your manifest.json&nbsp;<\/p>\r\n<pre class=\"language-markup\"><code>{\r\n  \"icons\": {\r\n    \"16\": \"images\/icon-16.png\",\r\n    \"32\": \"images\/icon-32.png\",\r\n    \"48\": \"images\/icon-48.png\",\r\n    \"128\": \"images\/icon-128.png\"\r\n  }\r\n}<\/code><\/pre>\r\n<p>16x16 &nbsp; &nbsp;Favicon on the extension's pages and context menu.<br>32x32 &nbsp; &nbsp;Windows computers often require this size.<br>48x48 &nbsp; &nbsp;Displays on the Extensions page.<br>128x128 &nbsp; &nbsp;Displays on installation and in the Chrome Web Store.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>Notes:<\/p>\r\n<ul>\r\n<li>There are many ways to <strong>structure<\/strong> an extension project; however, the only prerequisite is to place the <strong>manifest.json file in the extension's root directory<\/strong><\/li>\r\n<li>Any modification in<strong> manifest.json, Service worker,&nbsp;Content scripts&nbsp;<\/strong>files, you need to reload the extention else no need<\/li>\r\n<li>you can add any js code in html pages or design<\/li>\r\n<li><strong>\"manifest_version\", \"name\", and \"version\"<\/strong> only required in manifest.json<\/li>\r\n<\/ul>\r\n<p>&nbsp;<\/p>\r\n<h1 class=\"devsite-page-title\" tabindex=\"-1\">Run scripts on every page using Content Scripts<\/h1>\r\n<table style=\"border-collapse: collapse; width: 100%; height: 134.375px;\" border=\"1\"><colgroup><col style=\"width: 33.3333%;\"><col style=\"width: 33.3333%;\"><col style=\"width: 33.3333%;\"><\/colgroup>\r\n<tbody>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"height: 22.3958px;\"><strong>Feature &nbsp; &nbsp;<\/strong><\/td>\r\n<td style=\"height: 22.3958px;\"><strong>Content Scripts<\/strong><\/td>\r\n<td style=\"height: 22.3958px;\"><strong>Scripts in Popup<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"height: 22.3958px;\"><strong>Environment &nbsp; &nbsp;<\/strong><\/td>\r\n<td style=\"height: 22.3958px;\">Webpage context<\/td>\r\n<td style=\"height: 22.3958px;\">Popup window (extension UI)<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"height: 22.3958px;\"><strong>Access to DOM<\/strong><\/td>\r\n<td style=\"height: 22.3958px;\">Full access to the webpage DOM<\/td>\r\n<td style=\"height: 22.3958px;\">Only the popup DOM<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"height: 22.3958px;\"><strong>Access to Extension APIs &nbsp;<\/strong><\/td>\r\n<td style=\"height: 22.3958px;\">Limited (via message passing)<\/td>\r\n<td style=\"height: 22.3958px;\">Full access directly<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"height: 22.3958px;\"><strong>Primary Use<\/strong><\/td>\r\n<td style=\"height: 22.3958px;\">Interacting with or modifying web pages<\/td>\r\n<td style=\"height: 22.3958px;\">Managing extension UI and controls<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"height: 22.3958px;\"><strong>Communication<\/strong><\/td>\r\n<td style=\"height: 22.3958px;\">Requires messaging to background\/popup scripts<\/td>\r\n<td style=\"height: 22.3958px;\">Can communicate with background\/content scripts<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><strong>Trigger &nbsp; &nbsp;<\/strong><\/td>\r\n<td>Runs automatically based on manifest.json matches<\/td>\r\n<td>Runs when the popup is opened by the user<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p>To use content Scripts<\/p>\r\n<p>1. add scripts inside manifest.json<\/p>\r\n<pre class=\"language-markup\"><code>{\r\n  \"name\": \"Hello Extensions\",\r\n  \"version\": \"1.0\",\r\n  \"manifest_version\": 3,\r\n  \"content_scripts\": [\r\n    {\r\n      \"js\": [\"scripts\/content.js\"],\r\n      \"matches\": [\r\n        \"https:\/\/developer.chrome.com\/docs\/extensions\/*\",\r\n        \"https:\/\/developer.chrome.com\/docs\/webstore\/*\"\r\n      ]\r\n    }\r\n  ]\r\n}\r\n<\/code><\/pre>\r\n<p>content.js will be executed on only above give matches&nbsp;<\/p>\r\n<p>2. Add code inside content.js<\/p>\r\n<pre class=\"language-markup\"><code>const article = document.querySelector(\"article\");\r\n\r\nif (article) {\r\n  const text = article.textContent;\r\n  const wordMatchRegExp = \/[^\\s]+\/g; \/\/ Regular expression\r\n  const words = text.matchAll(wordMatchRegExp);\r\n  \/\/ matchAll returns an iterator, convert to array to get word count\r\n  const wordCount = [...words].length;\r\n  const readingTime = Math.round(wordCount \/ 200);\r\n  const badge = document.createElement(\"p\");\r\n  \/\/ Use the same styling as the publish information in an article's header\r\n  badge.classList.add(\"color-secondary-text\", \"type--caption\");\r\n  badge.textContent = `?????? ${readingTime} min read`;\r\n\r\n  \/\/ Support for API reference docs\r\n  const heading = article.querySelector(\"h1\");\r\n  \/\/ Support for article docs with date\r\n  const date = article.querySelector(\"time\")?.parentNode;\r\n\r\n  (date ?? heading).insertAdjacentElement(\"afterend\", badge);\r\n}<\/code><\/pre>\r\n<p>https:\/\/developer.chrome.com\/docs\/extensions\/develop\/concepts\/content-scripts<br>&nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp;<\/p>\r\n<p>&nbsp;<\/p>","category_id":"35","is_private":"0","created_at":"2025-01-01T12:55:14.000000Z","updated_at":"2025-01-01T13:48:45.000000Z","category":{"id":35,"user_id":"1","name":"ChromeExtention","slug":"chromeextention-b15c","parent_id":"12","created_at":"2025-01-01T12:42:04.000000Z","updated_at":"2025-01-01T12:42:04.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"1. create a new file in directory called manifest.json {   \"name\": \"Hello Extensions\",   \"description\": \"Base Level Extension\",   \"version\": - Basic Extension, Content Scripts, (Updated: January 1, 2025) - Read more about Basic Extension, Content Scripts, at my programming site [SITE]","categories":[]}