Search code examples
jqueryhtmlgreasemonkeytampermonkeyuserscripts

Swap Website Title and Link URL in Google Search Results


This is part of the Google Search Results page source code. I want to move the div elements into the href container. In this case, I want to swap the first div.TbwUpd and last h3.LC20lb. So, I want this:

<div class="g">
    <div data-hveid="CAMQAA" data-ved="2ahUKEwiy1bOI8IPlAhWVMN4KHfE2DXcQFSgAMAt6BAgDEAA">
        <div class "rc">
            <div class "r"> 
                <a href="https://www.merriam-webster.com/dictionary/meh" onmousedown="return>
                    <div class="TbwUpd">
                        <img class="xA33Gc" alt="https://www.merriam-webster.com/" height="16" src="data:image/png;base64,I=" width="16" data-atf="3"><cite class="iUh30 bc rpCHfe">Merriam-Webster › dictionary › meh</cite></div>
                    <br>
                    <h3 class="LC20lb">
                        <div class="ellip">Meh | Definition of Meh by Merriam-Webster</div></h3>
                </a>
            </div>
        </div>
    </div>
</div>

to become this:

<div class="g">
    <div data-hveid="CAMQAA" data-ved="2ahUKEwiy1bOI8IPlAhWVMN4KHfE2DXcQFSgAMAt6BAgDEAA">
        <div class "rc">
            <div class "r"> 
                <a href="https://www.merriam-webster.com/dictionary/meh" onmousedown="return>
                    <h3 class="LC20lb">
                        <div class="ellip">Meh | Definition of Meh by Merriam-Webster</div></h3>
                    <br>
                    <div class="TbwUpd">
                        <img class="xA33Gc" alt="https://www.merriam-webster.com/" height="16" src="data:image/png;base64,I=" width="16" data-atf="3"><cite class="iUh30 bc rpCHfe">Merriam-Webster › dictionary › meh</cite></div>
                </a>
            </div>
        </div>
    </div>
</div>

I tried to do that in this userscript but it didn't work.

// ==UserScript==
// @name        Rearrange Goggle Search result :Swap Title and URL
// @include     https://www.google.com/search*
// @version     1
// @grant       none
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==

$('.TbwUpd').appendTo('.LC20lb');

Original

After elements are swapped


Solution

  • 1- Google results are dynamically created so if that is the case, a whole lot of other code is needed to look for dynamic changes
    2- There is no need to add a 700+ function long JQuery just for a tiny code. It would be a lot more efficient as pure JavaScript. (Note: JQuery 1 & 2 are deprecated as they had issues and should be avoided)
    3- I am guessing there are more than 1 item to find and change so the fowling code will find all of them

    // ==UserScript==
    // @name        Rearrange Google Search result :Swap Title and URL
    // @include     https://www.google.com/search*
    // @version     1
    // @grant       none
    // ==/UserScript==
    
    document.querySelectorAll('a div.TbwUpd').forEach(item => {
      item.parentNode.appendChild(item.nextElementSibling); // put the <br> at the end
      item.parentNode.appendChild(item);                    // put the <div> at the end
    });
    

    4- If it is only one item then

    // ==UserScript==
    // @name        Rearrange Google Search result :Swap Title and URL
    // @include     https://www.google.com/search*
    // @version     1
    // @grant       none
    // ==/UserScript==
    
    
    const div = document.querySelector('a div.TbwUpd');
    div.parentNode.appendChild(div.nextElementSibling); // put the <br> at the end
    div.parentNode.appendChild(div);                    // put the div at the end