Override user defined function to use native function using chrome extension












1














I am fairly new to JS and creating a chrome extension where i have use Array.filter function, But for some website, website owner has created their own Array.filter function whose behavior is not same as built in function. Is there any way to override this user defined function and get the native behavior of this function. Any Help will be appreciated.










share|improve this question






















  • Content scripts have their own isolated environment, meaning your content script cant use variables / functions that were made in the injected page. So even if the site implemented their own Array#filter method your script wouldn't be able to use it. Unless you bypassed the isolation by injecting a script into the DOM of the page.
    – Patrick Evans
    Nov 20 at 5:53












  • @PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
    – Chaudhary238
    Nov 20 at 5:55
















1














I am fairly new to JS and creating a chrome extension where i have use Array.filter function, But for some website, website owner has created their own Array.filter function whose behavior is not same as built in function. Is there any way to override this user defined function and get the native behavior of this function. Any Help will be appreciated.










share|improve this question






















  • Content scripts have their own isolated environment, meaning your content script cant use variables / functions that were made in the injected page. So even if the site implemented their own Array#filter method your script wouldn't be able to use it. Unless you bypassed the isolation by injecting a script into the DOM of the page.
    – Patrick Evans
    Nov 20 at 5:53












  • @PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
    – Chaudhary238
    Nov 20 at 5:55














1












1








1







I am fairly new to JS and creating a chrome extension where i have use Array.filter function, But for some website, website owner has created their own Array.filter function whose behavior is not same as built in function. Is there any way to override this user defined function and get the native behavior of this function. Any Help will be appreciated.










share|improve this question













I am fairly new to JS and creating a chrome extension where i have use Array.filter function, But for some website, website owner has created their own Array.filter function whose behavior is not same as built in function. Is there any way to override this user defined function and get the native behavior of this function. Any Help will be appreciated.







javascript google-chrome-extension






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 5:45









Chaudhary238

83




83












  • Content scripts have their own isolated environment, meaning your content script cant use variables / functions that were made in the injected page. So even if the site implemented their own Array#filter method your script wouldn't be able to use it. Unless you bypassed the isolation by injecting a script into the DOM of the page.
    – Patrick Evans
    Nov 20 at 5:53












  • @PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
    – Chaudhary238
    Nov 20 at 5:55


















  • Content scripts have their own isolated environment, meaning your content script cant use variables / functions that were made in the injected page. So even if the site implemented their own Array#filter method your script wouldn't be able to use it. Unless you bypassed the isolation by injecting a script into the DOM of the page.
    – Patrick Evans
    Nov 20 at 5:53












  • @PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
    – Chaudhary238
    Nov 20 at 5:55
















Content scripts have their own isolated environment, meaning your content script cant use variables / functions that were made in the injected page. So even if the site implemented their own Array#filter method your script wouldn't be able to use it. Unless you bypassed the isolation by injecting a script into the DOM of the page.
– Patrick Evans
Nov 20 at 5:53






Content scripts have their own isolated environment, meaning your content script cant use variables / functions that were made in the injected page. So even if the site implemented their own Array#filter method your script wouldn't be able to use it. Unless you bypassed the isolation by injecting a script into the DOM of the page.
– Patrick Evans
Nov 20 at 5:53














@PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
– Chaudhary238
Nov 20 at 5:55




@PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
– Chaudhary238
Nov 20 at 5:55












2 Answers
2






active

oldest

votes


















1














To save the original Array#filter method you just save it to a variable and then use it when needed by using call():



//Saving the original method
var ArrayFilter = Array.prototype.filter;
//Then whenever needing to use it, call it by using call()
var someArray = [1,2,3];
var filteredArray = ArrayFilter.call(someArray,function(){ /* your filter callback */ });


Now you need to make this run before the script that creates the modified filter() method. You are going to have to do this by changing at which point your content script is loaded so it can load the other code. This is done by setting the run_at setting in the manifest:



manifest:



"content_scripts": [
{
"matches": ["http://*.example.com/*"],
"run_at": "document_start",
"js": ["contentScript.js"]
}
],


contentScript.js



//injecting the script into the page
//or however you are currently doing it
var yourScript = document.createElement('script');
document.head.appendChild(yourScript);
yourScript.textContent = "/* your js code */";





share|improve this answer























  • although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
    – Chaudhary238
    Nov 22 at 5:20



















0














Run your code before the page scripts and use Object.defineProperty to redefine the method and forbid subsequent changes. You need to put that code in a DOM script element so that it runs in the page context (more info), using a literal string, not src property, to ensure it precedes any other page scripts (more info).



manifest.json:



"content_scripts": [{
"matches": ["https://foo.bar/*"],
"js": ["content.js"],
"run_at": "document_start",
"all_frames": true
}]


content.js:



const script = document.createElement("script");
script.textContent = `
Object.defineProperty(Array.prototype, 'filter', {
value: Array.prototype.filter,
configurable: false,
writable: false,
});
`;
document.documentElement.appendChild(script);
script.remove();





share|improve this answer

















  • 1




    Forcing the filter method to not be writable by the page could have unexpected results, like causing whatever logic is using it to error out. The page might have a legitimate reason for having a custom one
    – Patrick Evans
    Nov 20 at 6:13












  • This is what OP wants AFAICT.
    – wOxxOm
    Nov 20 at 6:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386921%2foverride-user-defined-function-to-use-native-function-using-chrome-extension%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














To save the original Array#filter method you just save it to a variable and then use it when needed by using call():



//Saving the original method
var ArrayFilter = Array.prototype.filter;
//Then whenever needing to use it, call it by using call()
var someArray = [1,2,3];
var filteredArray = ArrayFilter.call(someArray,function(){ /* your filter callback */ });


Now you need to make this run before the script that creates the modified filter() method. You are going to have to do this by changing at which point your content script is loaded so it can load the other code. This is done by setting the run_at setting in the manifest:



manifest:



"content_scripts": [
{
"matches": ["http://*.example.com/*"],
"run_at": "document_start",
"js": ["contentScript.js"]
}
],


contentScript.js



//injecting the script into the page
//or however you are currently doing it
var yourScript = document.createElement('script');
document.head.appendChild(yourScript);
yourScript.textContent = "/* your js code */";





share|improve this answer























  • although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
    – Chaudhary238
    Nov 22 at 5:20
















1














To save the original Array#filter method you just save it to a variable and then use it when needed by using call():



//Saving the original method
var ArrayFilter = Array.prototype.filter;
//Then whenever needing to use it, call it by using call()
var someArray = [1,2,3];
var filteredArray = ArrayFilter.call(someArray,function(){ /* your filter callback */ });


Now you need to make this run before the script that creates the modified filter() method. You are going to have to do this by changing at which point your content script is loaded so it can load the other code. This is done by setting the run_at setting in the manifest:



manifest:



"content_scripts": [
{
"matches": ["http://*.example.com/*"],
"run_at": "document_start",
"js": ["contentScript.js"]
}
],


contentScript.js



//injecting the script into the page
//or however you are currently doing it
var yourScript = document.createElement('script');
document.head.appendChild(yourScript);
yourScript.textContent = "/* your js code */";





share|improve this answer























  • although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
    – Chaudhary238
    Nov 22 at 5:20














1












1








1






To save the original Array#filter method you just save it to a variable and then use it when needed by using call():



//Saving the original method
var ArrayFilter = Array.prototype.filter;
//Then whenever needing to use it, call it by using call()
var someArray = [1,2,3];
var filteredArray = ArrayFilter.call(someArray,function(){ /* your filter callback */ });


Now you need to make this run before the script that creates the modified filter() method. You are going to have to do this by changing at which point your content script is loaded so it can load the other code. This is done by setting the run_at setting in the manifest:



manifest:



"content_scripts": [
{
"matches": ["http://*.example.com/*"],
"run_at": "document_start",
"js": ["contentScript.js"]
}
],


contentScript.js



//injecting the script into the page
//or however you are currently doing it
var yourScript = document.createElement('script');
document.head.appendChild(yourScript);
yourScript.textContent = "/* your js code */";





share|improve this answer














To save the original Array#filter method you just save it to a variable and then use it when needed by using call():



//Saving the original method
var ArrayFilter = Array.prototype.filter;
//Then whenever needing to use it, call it by using call()
var someArray = [1,2,3];
var filteredArray = ArrayFilter.call(someArray,function(){ /* your filter callback */ });


Now you need to make this run before the script that creates the modified filter() method. You are going to have to do this by changing at which point your content script is loaded so it can load the other code. This is done by setting the run_at setting in the manifest:



manifest:



"content_scripts": [
{
"matches": ["http://*.example.com/*"],
"run_at": "document_start",
"js": ["contentScript.js"]
}
],


contentScript.js



//injecting the script into the page
//or however you are currently doing it
var yourScript = document.createElement('script');
document.head.appendChild(yourScript);
yourScript.textContent = "/* your js code */";






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 6:15

























answered Nov 20 at 6:09









Patrick Evans

31.8k54470




31.8k54470












  • although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
    – Chaudhary238
    Nov 22 at 5:20


















  • although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
    – Chaudhary238
    Nov 22 at 5:20
















although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
– Chaudhary238
Nov 22 at 5:20




although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
– Chaudhary238
Nov 22 at 5:20













0














Run your code before the page scripts and use Object.defineProperty to redefine the method and forbid subsequent changes. You need to put that code in a DOM script element so that it runs in the page context (more info), using a literal string, not src property, to ensure it precedes any other page scripts (more info).



manifest.json:



"content_scripts": [{
"matches": ["https://foo.bar/*"],
"js": ["content.js"],
"run_at": "document_start",
"all_frames": true
}]


content.js:



const script = document.createElement("script");
script.textContent = `
Object.defineProperty(Array.prototype, 'filter', {
value: Array.prototype.filter,
configurable: false,
writable: false,
});
`;
document.documentElement.appendChild(script);
script.remove();





share|improve this answer

















  • 1




    Forcing the filter method to not be writable by the page could have unexpected results, like causing whatever logic is using it to error out. The page might have a legitimate reason for having a custom one
    – Patrick Evans
    Nov 20 at 6:13












  • This is what OP wants AFAICT.
    – wOxxOm
    Nov 20 at 6:14
















0














Run your code before the page scripts and use Object.defineProperty to redefine the method and forbid subsequent changes. You need to put that code in a DOM script element so that it runs in the page context (more info), using a literal string, not src property, to ensure it precedes any other page scripts (more info).



manifest.json:



"content_scripts": [{
"matches": ["https://foo.bar/*"],
"js": ["content.js"],
"run_at": "document_start",
"all_frames": true
}]


content.js:



const script = document.createElement("script");
script.textContent = `
Object.defineProperty(Array.prototype, 'filter', {
value: Array.prototype.filter,
configurable: false,
writable: false,
});
`;
document.documentElement.appendChild(script);
script.remove();





share|improve this answer

















  • 1




    Forcing the filter method to not be writable by the page could have unexpected results, like causing whatever logic is using it to error out. The page might have a legitimate reason for having a custom one
    – Patrick Evans
    Nov 20 at 6:13












  • This is what OP wants AFAICT.
    – wOxxOm
    Nov 20 at 6:14














0












0








0






Run your code before the page scripts and use Object.defineProperty to redefine the method and forbid subsequent changes. You need to put that code in a DOM script element so that it runs in the page context (more info), using a literal string, not src property, to ensure it precedes any other page scripts (more info).



manifest.json:



"content_scripts": [{
"matches": ["https://foo.bar/*"],
"js": ["content.js"],
"run_at": "document_start",
"all_frames": true
}]


content.js:



const script = document.createElement("script");
script.textContent = `
Object.defineProperty(Array.prototype, 'filter', {
value: Array.prototype.filter,
configurable: false,
writable: false,
});
`;
document.documentElement.appendChild(script);
script.remove();





share|improve this answer












Run your code before the page scripts and use Object.defineProperty to redefine the method and forbid subsequent changes. You need to put that code in a DOM script element so that it runs in the page context (more info), using a literal string, not src property, to ensure it precedes any other page scripts (more info).



manifest.json:



"content_scripts": [{
"matches": ["https://foo.bar/*"],
"js": ["content.js"],
"run_at": "document_start",
"all_frames": true
}]


content.js:



const script = document.createElement("script");
script.textContent = `
Object.defineProperty(Array.prototype, 'filter', {
value: Array.prototype.filter,
configurable: false,
writable: false,
});
`;
document.documentElement.appendChild(script);
script.remove();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 6:09









wOxxOm

26.2k34461




26.2k34461








  • 1




    Forcing the filter method to not be writable by the page could have unexpected results, like causing whatever logic is using it to error out. The page might have a legitimate reason for having a custom one
    – Patrick Evans
    Nov 20 at 6:13












  • This is what OP wants AFAICT.
    – wOxxOm
    Nov 20 at 6:14














  • 1




    Forcing the filter method to not be writable by the page could have unexpected results, like causing whatever logic is using it to error out. The page might have a legitimate reason for having a custom one
    – Patrick Evans
    Nov 20 at 6:13












  • This is what OP wants AFAICT.
    – wOxxOm
    Nov 20 at 6:14








1




1




Forcing the filter method to not be writable by the page could have unexpected results, like causing whatever logic is using it to error out. The page might have a legitimate reason for having a custom one
– Patrick Evans
Nov 20 at 6:13






Forcing the filter method to not be writable by the page could have unexpected results, like causing whatever logic is using it to error out. The page might have a legitimate reason for having a custom one
– Patrick Evans
Nov 20 at 6:13














This is what OP wants AFAICT.
– wOxxOm
Nov 20 at 6:14




This is what OP wants AFAICT.
– wOxxOm
Nov 20 at 6:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386921%2foverride-user-defined-function-to-use-native-function-using-chrome-extension%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy