I want to delete a javascript function from a page using an injected javascript, which is running through a Google Chrome extension.
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:
var testtest() {
somecode bla bla bla;
somecode bla bla bla;
somecode bla bla bla;
return false;
}
Basically I want to remove or prevent the function testtest from ever running on the page.
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).
I'm getting Hello World popup which means the script is running on the page, but the code is not being replaced.
Here are my tries using javascript replace method:
TRY 1:
alert("Hello World!");
window.location = loc.replace(testtest, "aaaaa");
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
--
TRY 2:
alert("Hello World!");
loc.replace(testtest, "aaaaa");
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
--
TRY 3:
alert("Hello World!");
testtest= "aaaaa";
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
--
TRY 4:
alert("Hello World!");
var str="testtest";
var n=str.replace("testtest","aaaaa");
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
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.
Update: I tried these other three methods but they also failed.
TRY 5:
alert("Hello World!");
function pacifyGlobalFunction(testtest) {
Object.defineProperty(
window,
testtest,
{
value: function () {},
configurable: true // permit future Object.defineProperty
}
);
}
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
--
TRY 6:
alert("Hello World!");
Object.defineProperty(window, 'testtest', {
value: function(){/*This function cannot be overridden*/}
});
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
--
TRY 7:
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);
Conclusion:
"Hello World" popup: SUCCESS
Code Replaced: FAIL
So I still need a solution.