<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Łukasz Kaproń</title>
    <description>The latest articles on Forem by Łukasz Kaproń (@lukaszkapron).</description>
    <link>https://forem.com/lukaszkapron</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1060098%2Fee18631a-a80a-42b7-9f1a-a0d9198b0f55.png</url>
      <title>Forem: Łukasz Kaproń</title>
      <link>https://forem.com/lukaszkapron</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lukaszkapron"/>
    <language>en</language>
    <item>
      <title>How to Create a Chrome Extension to Restore the Google Maps Button</title>
      <dc:creator>Łukasz Kaproń</dc:creator>
      <pubDate>Wed, 17 Jul 2024 20:02:11 +0000</pubDate>
      <link>https://forem.com/lukaszkapron/how-to-create-a-chrome-extension-to-restore-the-google-maps-button-2d9a</link>
      <guid>https://forem.com/lukaszkapron/how-to-create-a-chrome-extension-to-restore-the-google-maps-button-2d9a</guid>
      <description>&lt;p&gt;Hi!&lt;br&gt;
It is a tutorial how to create very simple &lt;strong&gt;Chrome Extension&lt;/strong&gt; to restore Google Maps button. In compliance with the EU Digital Markets Act (DMA), Google made changes to Google Search in the European Economic Area (EEA), removing the Maps link at the top of the search page that links to Google Maps. I decided to create an extension to restore this button.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We will cover how to:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a manifest.json file - required for Chrome Extension&lt;/li&gt;
&lt;li&gt;Inspect the Google search results page to understand its structure.&lt;/li&gt;
&lt;li&gt;Write a JavaScript file to inject the Maps button.&lt;/li&gt;
&lt;li&gt;Add the new Chrome extension to your browser.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  How to start?
&lt;/h2&gt;

&lt;p&gt;Google provides excellent &lt;a href="https://developer.chrome.com/docs/extensions/get-started" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; about creating Chrome extensions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's start
&lt;/h2&gt;

&lt;p&gt;Firstly, we have to create &lt;code&gt;manifest.json&lt;/code&gt; file. I won't go into details about this file. You can read more about it &lt;a href="https://developer.chrome.com/docs/extensions/reference/manifest" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "manifest_version": 3,
    "name": "Google Maps button restored",
    "version": "1.0",
    "description": "Restore google maps button on search page",
    "content_scripts": [
      {
        "js": ["content.js"],
        "matches": [
          "*://www.google.com/search*"
        ]
      }
    ]
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What do we want to do?
&lt;/h2&gt;

&lt;p&gt;Before we start coding, we need to understand how Google displays &lt;strong&gt;search results&lt;/strong&gt;. For example, we can type "Warsaw" in Google and examine the results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6l4603czww2lz36gde2b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6l4603czww2lz36gde2b.png" alt="Start" width="800" height="136"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create needed HTML code
&lt;/h2&gt;

&lt;p&gt;Now, let's inspect the HTML code of this page. Press &lt;strong&gt;F12&lt;/strong&gt; to open Developer Tools and find the &lt;strong&gt;div&lt;/strong&gt; that contains elements like Graphics, Videos, etc. It should be tagged with the class &lt;strong&gt;crJ18e&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="crJ18e"&amp;gt;
  &amp;lt;div role="list" style="display:contents"&amp;gt;
    &amp;lt;div role="listitem"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to copy this &lt;strong&gt;structure&lt;/strong&gt; to add the Maps button. We will copy each attribute and also inner &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag. We will use the &lt;code&gt;document.querySelector&lt;/code&gt; method to find the desired tag and the &lt;code&gt;document.createElement&lt;/code&gt; method to create a new tag. Let's create a new &lt;strong&gt;JavaScript&lt;/strong&gt; file named &lt;code&gt;content.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const outerDiv = document.querySelector('.crJ18e');

if (outerDiv) {
  const innerDiv = outerDiv.querySelector('[role="list"]');

  if (innerDiv) {
    const newDiv = document.createElement('div');
    newDiv.setAttribute('role', 'listitem');
    newDiv.setAttribute('data-hveid', 'CBYQAA');
    newDiv.setAttribute('data-ved', '2ahUKEwj1n-q81qOHAsXwCqvEDHahCC8MqQJAB6BAgBEAA');

    const aTag = document.createElement('a');
    aTag.href = ``;
    aTag.className = 'LatpMc nPDzT T3FoJb';
    aTag.setAttribute('role', 'link');
    aTag.setAttribute('data-hveid', 'CBYQAA');
    aTag.setAttribute('data-ved', '2ahUKEwj1n-q81qOHAsXwCqvEDHahCC8MqQJegQIFhAB');

    const innerLinkDiv = document.createElement('div');
    innerLinkDiv.className = 'YmvwI';
    innerLinkDiv.textContent = 'Maps';

    aTag.appendChild(innerLinkDiv);

    newDiv.appendChild(aTag);

    innerDiv.appendChild(newDiv);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Redirect link to Google Maps
&lt;/h2&gt;

&lt;p&gt;How to know what is the &lt;strong&gt;URL&lt;/strong&gt; of Google Maps search? I found in &lt;a href="https://developers.google.com/maps/documentation/urls/get-started" rel="noopener noreferrer"&gt;Google documntantion&lt;/a&gt; that it looks like this:&lt;br&gt;
&lt;code&gt;https://www.google.com/maps/search/?api=1&amp;amp;query=parameters&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;All we need to to do is to get what user searched for in google and replace &lt;code&gt;parameters&lt;/code&gt;. The search URL for "Warsaw" starts like this:&lt;br&gt;
&lt;code&gt;https://www.google.com/search?q=Warsaw&lt;/code&gt;. So, we need to get the &lt;strong&gt;value&lt;/strong&gt; of the &lt;code&gt;q&lt;/code&gt; parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const urlParams = new URLSearchParams(window.location.search);
const searchQuery = urlParams.get('q');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Include other languages pages
&lt;/h2&gt;

&lt;p&gt;Remember that "&lt;a href="https://www.google.com/" rel="noopener noreferrer"&gt;https://www.google.com/&lt;/a&gt;" is not the only &lt;strong&gt;Google Serach Page&lt;/strong&gt;. That can be specified for country. For example, the &lt;strong&gt;Polish&lt;/strong&gt; page URL is "&lt;a href="https://www.google.pl/" rel="noopener noreferrer"&gt;https://www.google.pl/&lt;/a&gt;". We need to include this in the &lt;code&gt;manifest.json&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Full code
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;contents.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const urlParams = new URLSearchParams(window.location.search);
const searchQuery = urlParams.get('q');

if (searchQuery) {
  const outerDiv = document.querySelector('.crJ18e');

  if (outerDiv) {
    const innerDiv = outerDiv.querySelector('[role="list"]');

    if (innerDiv) {
      const newDiv = document.createElement('div');
      newDiv.setAttribute('role', 'listitem');
      newDiv.setAttribute('data-hveid', 'CBYQAA');
      newDiv.setAttribute('data-ved', '2ahUKEwj1n-q81qOHAsXwCqvEDHahCC8MqQJAB6BAgBEAA');

      const aTag = document.createElement('a');
      aTag.href = `https://www.google.com/maps/search/?api=1&amp;amp;query=${encodeURIComponent(searchQuery)}`;
      aTag.className = 'LatpMc nPDzT T3FoJb';
      aTag.setAttribute('role', 'link');
      aTag.setAttribute('data-hveid', 'CBYQAA');
      aTag.setAttribute('data-ved', '2ahUKEwj1n-q81qOHAsXwCqvEDHahCC8MqQJegQIFhAB');

      const innerLinkDiv = document.createElement('div');
      innerLinkDiv.className = 'YmvwI';
      innerLinkDiv.textContent = 'Maps';

      aTag.appendChild(innerLinkDiv);

      newDiv.appendChild(aTag);

      innerDiv.appendChild(newDiv);
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;manifest.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "manifest_version": 3,
    "name": "Google Maps button restored",
    "version": "1.0",
    "description": "Restore google maps button on search page",
    "content_scripts": [
      {
        "js": ["content.js"],
        "matches": [
          "*://www.google.com/search*",
          "*://www.google.pl/search*"
        ]
      }
    ]
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Add new chrome extension
&lt;/h2&gt;

&lt;p&gt;It is very simple to add our extension to the browser. According to &lt;a href="https://support.google.com/chrome/a/answer/2714278?hl=en" rel="noopener noreferrer"&gt;&lt;br&gt;
Google documentation&lt;/a&gt;, these are &lt;strong&gt;steps&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Save the extension (2 files) &lt;strong&gt;folder&lt;/strong&gt; on your test device.&lt;/li&gt;
&lt;li&gt;Go to chrome://extensions/.&lt;/li&gt;
&lt;li&gt;At the top right, turn on &lt;strong&gt;Developer mode&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Load unpacked&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Find and select the app or extension folder.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, when you &lt;strong&gt;type&lt;/strong&gt; something in Google, a new Maps button should appear in line with other Google buttons.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbo8e1hbdozht39w6u8z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbo8e1hbdozht39w6u8z.png" alt="Result" width="800" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Troubleshooting and Common Issues
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Button Not Appearing&lt;/strong&gt;: Ensure that the class names and structure of the Google search page haven't changed. Inspect the page to confirm.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JavaScript Errors&lt;/strong&gt;: Open Developer Tools and check the console for any errors in your script.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advanced Features&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Change Button Placement&lt;/strong&gt;: Adjust the position of the Maps button relative to other Google buttons.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Localized Versions&lt;/strong&gt;: Extend support to more languages and Google domains.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Additional Buttons&lt;/strong&gt;: Add more useful buttons like "Images" or "News".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error handling&lt;/strong&gt;: Implement more error handling.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Note: Remember that Google can change their HTML code, so you will need to update your code accordingly.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By following these steps, you can restore the Google Maps button on the search page. Try it out and let me know if you encounter any issues or have suggestions for improvement. &lt;em&gt;This is my first Dev Community Post&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Hello</title>
      <dc:creator>Łukasz Kaproń</dc:creator>
      <pubDate>Thu, 06 Apr 2023 11:09:37 +0000</pubDate>
      <link>https://forem.com/lukaszkapron/hello-3ge5</link>
      <guid>https://forem.com/lukaszkapron/hello-3ge5</guid>
      <description></description>
    </item>
  </channel>
</rss>
