Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to remove or overwrite a function on a page using injected javascript?
    text
    copied!<p>I want to delete a javascript function from a page using an injected javascript, which is running through a Google Chrome extension.</p> <p>For the purposes of the question, let's call the exampl,e function I want to remove testtest. In this case, the function looks like this on the page:</p> <pre><code>var testtest() { somecode bla bla bla; somecode bla bla bla; somecode bla bla bla; return false; } </code></pre> <p>Basically I want to remove or prevent the function testtest from ever running on the page.</p> <p>I was trying the javascript replace method to do it, but it isn't working. If this isn't possible I'd like an alternative solution to achieve my end goal (prevent the function from running on the page).</p> <p>I'm getting Hello World popup which means the script is running on the page, but the code is not being replaced.</p> <p>Here are my tries using javascript replace method:</p> <p><em><strong>TRY 1:</em></strong><br></p> <pre><code>alert("Hello World!"); window.location = loc.replace(testtest, "aaaaa"); </code></pre> <p>Conclusion:<br> "Hello World" popup: SUCCESS<br> <strong>Code Replaced: FAIL</strong></p> <p>--</p> <p><em><strong>TRY 2:</em></strong><br></p> <pre><code>alert("Hello World!"); loc.replace(testtest, "aaaaa"); </code></pre> <p>Conclusion:<br> "Hello World" popup: SUCCESS<br> <strong>Code Replaced: FAIL</strong></p> <p>--</p> <p><em><strong>TRY 3:</em></strong><br></p> <pre><code>alert("Hello World!"); testtest= "aaaaa"; </code></pre> <p>Conclusion:<br> "Hello World" popup: SUCCESS<br> <strong>Code Replaced: FAIL</strong></p> <p>--</p> <p><em><strong>TRY 4:</em></strong><br></p> <pre><code>alert("Hello World!"); var str="testtest"; var n=str.replace("testtest","aaaaa"); </code></pre> <p>Conclusion:<br> "Hello World" popup: SUCCESS<br> <strong>Code Replaced: FAIL</strong></p> <hr> <p>Perhaps javascript replace method isn't the right way to go about achieving my end objective. I don't really care exactly how I do it, as long as I achieve my goal. Please help me find a solution.</p> <p><strong>Update:</strong> I tried these other three methods but they also failed.</p> <p><em><strong>TRY 5:</em></strong><br></p> <pre><code>alert("Hello World!"); function pacifyGlobalFunction(testtest) { Object.defineProperty( window, testtest, { value: function () {}, configurable: true // permit future Object.defineProperty } ); } </code></pre> <p>Conclusion:<br> "Hello World" popup: SUCCESS<br> <strong>Code Replaced: FAIL</strong></p> <p>--</p> <p><em><strong>TRY 6:</em></strong><br></p> <pre><code>alert("Hello World!"); Object.defineProperty(window, 'testtest', { value: function(){/*This function cannot be overridden*/} }); </code></pre> <p>Conclusion:<br> "Hello World" popup: SUCCESS<br> <strong>Code Replaced: FAIL</strong></p> <p>--</p> <p><em><strong>TRY 7:</em></strong><br></p> <pre><code>alert("Hello World!"); var actualCode = '(' + function() { window.testtest = null; } + ')();'; var script = document.createElement('script'); script.textContent = actualCode; (document.head||document.documentElement).appendChild(script); script.parentNode.removeChild(script); </code></pre> <p>Conclusion:<br> "Hello World" popup: SUCCESS<br> <strong>Code Replaced: FAIL</strong></p> <hr> <p>So I still need a solution.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload