Can I put if condition in javascript to detect any running script

133 views Asked by At

I want to develop chrome extension to put a check on the script say this website runs http://whatsmyscreenresolution.com/

e.g.

if (his_script==my_script) 
then 
   block it or return "123". 

I want to do something like this.Is it possible or can I even block websites to detect my screen resolution, font, etc other than disabling javascript at my end?

3

There are 3 answers

2
wolfhammer On

It doesn't think it's possible. Tried setting window.screen and creating a var screen but no matter what is written to screen.width and screen.height it always returns the correct resolution. It doesn't seem spoofable at least from a javascript console. You might try a hidden frame with the desired screen resolution for privacy and when the page is loaded adjust the resolution to actual browser resolution and display the frame.

3
Paul S. On

can I even block websites to detect my screen resolution

You could define a new window.screen object

(function (screen) {
    function clone(e) {
        var o = {}, k;
        for (k in e) o[k] = e[k];
        return o;
    }
    Object.defineProperty(window, 'screen', {get: function () {
        var o = clone(screen);
        o.availHeight = o.height = Math.random() * (o.height - 600) + 600;
        o.availWidth = o.width = Math.random() * (o.width - 600) + 600;
        return o;
    }});
}(window.screen));

After this, trying to access screen or window.screen will give you randomised (but not entirely unreasonable for styling purposes) values

DEMO

1
Brian On

Take a look at the chrome.webRequest api: https://developer.chrome.com/extensions/webRequest

Theoretically, you could do this with the onBeforeRequest listener.