Search code examples
javascriptgoogle-chromerefresh

Is there a way to inject JavaScript automatically from chrome dev tool after refreshing the page?


I write some JavaScripts into chrome console. Meta tag which I show you below allow to browser refresh in 5 seconds. I want to inject the same code automatically after my page is reloaded. Because dev console injected JavaScripts are gone when you refresh the page. Could I do that right in the chrome dev tool?

Thank you

var meta = document.createElement('meta');
meta.httpEquiv = "refresh";
meta.content = "5";
document.getElementsByTagName('head')[0].appendChild(meta);

Solution

  • You cannot directly write code in the chrome console and make it persist between page reloads, however you can use the Local Overrides in DevTools introduced in Chrome 65.

    First, you will need to enable local overrides in the Sources tab of DevTools. Then select a folder to save the overrides and give the permission to access that folder. After that, you can search for a file and add your code. See image below.

    enter image description here