How to block a website with a subdomain?

I am trying to block myself from accessing Google Search Console (such an addictive tool, like alcohol :slight_smile:)

These two userscripts didn’t block it:

// ==UserScript==
// @name        blocksite
// @run-at      document-start
// @match       *
// ==/UserScript==

let href = window.location.href;
if (
  window.location.href.includes('https://search.google.com')
) {
  window.open("https://google.com/", "_self");
}

and:

// ==UserScript==
// @name        blocksite
// @run-at      document-start
// @match       *
// ==/UserScript==

let href = window.location.href;
let anythingToBlock = [
    'https://search.google.com'
];

for (let i = 0; i < anythingToBlock.length; i++) {
    if (href.includes(anythingToBlock[i])) {
            window.open("https://google.com/", "_self");
    }
}

Please share with us why both codes don’t work?

search.google.com already redirects to google.com…

What’s it say if you console.log the href variable?

On https://search.google.com/search-console/about I ran and got this:

let href = window.location.href;
console.log(href);

VM139:2 https://search.google.com/search-console/about

undefined

Use an asterisk (*) if you want to prohibit all subdomains for a certain website, such as *. domain.com. All subdomains of domain.com will match this.

1 Like

Replacing

let anythingToBlock = [
    'https://search.google.com'
];

With

let anythingToBlock = [
    '*://*.google.com'
];

Didn’t solve the problem sadly. :frowning_face:
I can still access Google Search Console.

Replacing

window.open("https://google.com/", "_self");

With

window.open('https://algeriatimes.net//", "_self');

Doesn’t make the code to work – both on Google Chrome and Microsoft Edge.

Strangely, the following code works only from console, but not from a user script manager (Tampermonkey).

// ==UserScript==
// @name         New Userscript
// @run-at      document-start
// @match        *
// ==/UserScript==

window.setInterval( () => {
    const urlPatternToBlock = [
        'https://search.google.com'
    ];

    for (const element of urlPatternToBlock) {
        if (window.location.href.includes(urlPatternToBlock)) {
        window.open("https://google.com/", "_self");
        }
    }
}, 1000);

I assume what’s happening is that your script starts to load, the browser gets a Redirect from Google (because as i said, search.google.com redirects to google.com automatically), and interrupts the script; it then starts to load again on google.com, but now the search pattern misses, and so the script does nothing.

Now I understand it :slight_smile:

What is the kind or type or name of this redirect which doesn’t change the URL in the browser? I never came across such an elusive redirect ever in history…

What can be done against this specific type of redirects?
I ask this because I know that JavaScript shouldn’t be disallowed to a user ever :slight_smile:

It does change the url in my browser. If i type in:
image

hit Enter,

I end up
image

And my Network tab tells me it was a 301 Redirect message that sent me there:

Odd.
In my browser, Google Chrome version 114, It doesn’t. It stays only as follows in the URL field of the browser.

https://search.google.com/search-console/about

I even tried to work around this with this code targeting the HTML source of the original page, but it didn’t work:

const firstLinkElement = document.querySelector('link');

if (firstLinkElement.href.includes('https://search.google.com/search-console/about')) {
 window.open("https://google.com/", "_self");   
}

Is there any workaround?

@m_hutley

Oddly enough, the following code works. The @match command here is different than above.

// ==UserScript==
// @name        google_single_value
// @run-at      document-start
// @match       https://search.google.com/search-console/*
// ==/UserScript==

if (window.location.href.includes('https://search.google.com')) {
    window.open("https://google.com/", "_self");
};

That doesn’t really solve my problem because I want to match any webpage whatsoever (this code segment is actually part of a larger script which should match any webpage, with or without any redirect.

The problem was found out to be caused by an incorrect match command.

Instead

// @match       *

It should have been

// @match *://*/*

Credit for user:Destroy666 in Stack Exchange to note that in his answer.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.