1

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.

user2651403
  • 478
  • 5
  • 17
  • Potential Duplicate: http://stackoverflow.com/questions/9347733/stop-a-function-from-execute-with-chrome-extension – TylerLubeck Aug 05 '13 at 15:44
  • Why are you trying to modify the _URL_ in your code, when you say you want to modify a _function_? – Paul S. Aug 05 '13 at 15:51
  • Paul you're right about not needing `loc = window.location.href;` no matter still none of the tries remove the script. – user2651403 Aug 05 '13 at 18:28

3 Answers3

0
function testtest() {}

Is similar to:

window.testtest = function () {};

So testtest is a property of the window object.

You can use delete to delete properties from objects. So:

delete window.testtest;

Should remove the function. The function will still be called though .. so you might want to just redefine it.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

Your mention of Google Chrome means that you should have Object.defineProperty; make a non-writable property for window to prevent it being set to something else

function pacifyGlobalFunction(functionName) {
    Object.defineProperty(
        window,
        functionName,
        {
            value: function () {},
            configurable: true // permit future Object.defineProperty
        }
    );
}
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • I tried this verbatim but it doesn't do anything, script still runs. Hello World popup in the same file ran fine, so the script is running on the page, just the code isn't working. – user2651403 Aug 05 '13 at 18:19
  • @user2651403 can you make an actual testcase on jsfiddle or jsbin for us? – Paul S. Aug 05 '13 at 18:43
  • not really possible to demonstrate. Since we're talking about changing or removing a function on a remote url using a chrome extension. – user2651403 Aug 05 '13 at 19:12
  • What is the expected output of your fiddle if the function is removed? The only other function call I see is `alert`.. you want to remove `alert`? – Paul S. Aug 05 '13 at 19:36
  • no. i put alert to make sure it's running. You can't use jsfiddle to see anything because I want to remove a function on a remote domain. The only way to demo is to set up a whole site just to make a function and then make and install the extension and visit that domain and see if the function is removed. – user2651403 Aug 06 '13 at 00:41
  • http://jsbin.com/ijiruv/1/edit .. but in Chrome, turns out that doing `function foo() {}` will overwrite property `window.foo` even if defined as non-writable... may be a webkit bug, so you'll have to re-define after it was defined, not before as I'd hoped using my solution. It should work anything defined using `=` though e.g. `foo = function () {};` – Paul S. Aug 06 '13 at 02:28
  • I have no idea what you're talking about. How about update the answer to working code since you admit your answer is wrong? – user2651403 Aug 09 '13 at 22:36
0

You can overwrite the function. For example, your old one would look like

function testMe() { 
   alert("Hi"); 
}

just overwrite it by empty content like this

function testMe() {}

So, when calling testMe(); on page no alerts is popped up :)