0

I have some 3rd parties JavaScript files in my code which includes some another JavaScript files which are loaded after some event. I don`t have control to change code inside 3rd party JavaScript file.

My website is hosted on https and included files are loading over http since browser does not allow this to load and says "mix content blocked".

Can I write interceptor that will intercept this call and will change http to https.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    This is usually taken care of by a forward on your web hosting. – Scott Marcus Dec 26 '19 at 15:16
  • I dont believe you will be able to do so in case you don't have access to those javascript libraries that are making a request for other libraries. However, if your site is hosted in a web server i.e IIS than you can use url re-writing module to achieve this but this will not be part of your front end. – Umer Hayyat Dec 26 '19 at 15:49

2 Answers2

0

When you are including a 3rd party library in your code, the loading browser will make the request to the server where the code is hosted. You can download the code, if it's available and there is no legal issues to do so, and host it on your server. You have to understand that not all data needs to go through https (to increase performance for example).

Take a look at this link to have a better understanding of the issue and how the prevent it.

tiborK
  • 385
  • 1
  • 6
0

Thanks for your comments but I don`t believe I should change 3rd party libraries as there might be some legal issues with it.

But I found something which can solve my issue by intercepting all the http calls and forward it to https by adding following code in your JavaScript at global.

Object.defineProperty(HTMLScriptElement.prototype, 'src', {
    get: function() {
         return this.getAttribute('src')
    },
    set: function(url) {
        var prefix = "http://";

        if (url.startsWith(prefix))
            url = "https://" + url.substr(prefix.length);

        console.log('being set: ' + url);
        this.setAttribute('src', url);
    }
});

Originally posted here : Force JavaScript to load additional resources over https