Search code examples
javascriptgreasemonkey

How to alter/remove this inline javascript with Greasemonkey?


I found this script in the head of a website:

<script type="text/javascript" >

function Ext_Detect_NotInstalled(ExtName,ExtID) {
}

function Ext_Detect_Installed(ExtName,ExtID) {
    alert("We have found unwanted extension. Please contact support")
    window.location = "logout.php"
}

var Ext_Detect = function(ExtName,ExtID) {
    var s = document.createElement('script');
    s.onload = function(){Ext_Detect_Installed(ExtName,ExtID);};
    s.onerror = function(){Ext_Detect_NotInstalled(ExtName,ExtID);};
    s.src = 'chrome-extension://' + ExtID + '/captured.js';
    document.body.appendChild(s);
}

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

function displayErrorAndLogout() {
    alert("Please use chrome browser to view the content");
    window.location = "logout.php"
}

if (is_chrome==true)
{
    window.onload = function() { Ext_Detect('Chrome','ngpampappnmepgilojfohadhhmbhlaek');};
} else {
    is_chrome = navigator.userAgent.toLowerCase().indexOf('crios') > -1;
    if (is_chrome == false){

        if (detectIE()){
            displayErrorAndLogout()
        }

        if (navigator.userAgent.indexOf('UCBrowser') > -1) {
            displayErrorAndLogout()
        }

This script check internet download manager extension and popup a logout message. Is this possible to remove or alter this inline java script using Greasemonkey?


Solution

  • If you want to disable the check for the Chrome addon, it's easy: the script assigns a function to onload, so if you simply assign something else to onload, Ext_Detect will never run, and your extension will not be detected:

    window.onload = () => null;
    

    Unfortunately, the other part that checks for crios and UCBrowser and runs detectIE runs synchronously, presumably at the beginning of page load, and userscripts cannot reliably run at the very beginning of page load, so that behavior may not be possible to alter, though you could try it with @run-at document-start: displayErrorAndLogout calls alert before assigning to window.location, so if you make it so that alert throws an error, the location will not change:

    @run-at    document-start
    // ==/UserScript==
    window.alert = function() {
      throw new Error();
    };