'Javascript working in console, not working (propertly) as userscript

This is my first post on this forum. On various forums there are links to t.me/, by default they look like this https://t.me/netflix/3724 , a preview of one post opens, when you click on CONTEXT you go straight to the feed, and this does a little code do - adds "s" between two slashes, reload the page, and redirect straight to the feed, and adress looks like: https://t.me/s/netflix/3724 Code work fine only in browser console, but not as a userscript- then the page reloads and redirects to telegram.org. I want the code to work as userscript. Here's what it userscript looks like:

// ==UserScript==
// @name            Telegram Direct 
// @namespace       Violentmonkey Scripts
// @match           https://t.me/*
// @version         1.0
// @run-at          document-start
// @noframes
// ==/UserScript==

window.location.href = "https://t.me/s" + window.location.pathname.substring(window.location.pathname.indexOf('s', 1));

I'm not a programmer, I've tried many solutions from this site, but since I don't really know what to look for, I ask for help.

Greets.



Solution 1:[1]

I solved the problem in a different way :) The solution below:

// ==UserScript==
// @name            Telegram Direct 
// @namespace       Violentmonkey Scripts
// @match           https://t.me/*
// @version         Over 9000
// @author          Ukash
// @run-at          document-start
// @noframes
// @icon            https://telegram.org/img/favicon.ico
// @description     Telegram Direct
// ==/UserScript==


document.querySelector('.tgme_page_context_link').click();   

  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ukash