Override user defined function to use native function using chrome extension
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
add a comment |
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
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 ownArray#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 '18 at 5:53
@PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
– Chaudhary238
Nov 20 '18 at 5:55
add a comment |
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
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
javascript google-chrome-extension
asked Nov 20 '18 at 5:45
Chaudhary238Chaudhary238
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 ownArray#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 '18 at 5:53
@PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
– Chaudhary238
Nov 20 '18 at 5:55
add a comment |
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 ownArray#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 '18 at 5:53
@PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
– Chaudhary238
Nov 20 '18 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 '18 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 '18 at 5:53
@PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
– Chaudhary238
Nov 20 '18 at 5:55
@PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
– Chaudhary238
Nov 20 '18 at 5:55
add a comment |
2 Answers
2
active
oldest
votes
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 */";
although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
– Chaudhary238
Nov 22 '18 at 5:20
add a comment |
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();
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 '18 at 6:13
This is what OP wants AFAICT.
– wOxxOm
Nov 20 '18 at 6:14
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 */";
although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
– Chaudhary238
Nov 22 '18 at 5:20
add a comment |
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 */";
although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
– Chaudhary238
Nov 22 '18 at 5:20
add a comment |
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 */";
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 */";
edited Nov 20 '18 at 6:15
answered Nov 20 '18 at 6:09
Patrick EvansPatrick Evans
32.1k54771
32.1k54771
although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
– Chaudhary238
Nov 22 '18 at 5:20
add a comment |
although its not complete solution for my case, but it gave me initial point to start. thanks it help indeed.
– Chaudhary238
Nov 22 '18 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 '18 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 '18 at 5:20
add a comment |
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();
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 '18 at 6:13
This is what OP wants AFAICT.
– wOxxOm
Nov 20 '18 at 6:14
add a comment |
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();
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 '18 at 6:13
This is what OP wants AFAICT.
– wOxxOm
Nov 20 '18 at 6:14
add a comment |
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();
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();
answered Nov 20 '18 at 6:09
wOxxOmwOxxOm
26.6k34762
26.6k34762
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 '18 at 6:13
This is what OP wants AFAICT.
– wOxxOm
Nov 20 '18 at 6:14
add a comment |
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 '18 at 6:13
This is what OP wants AFAICT.
– wOxxOm
Nov 20 '18 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 '18 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 '18 at 6:13
This is what OP wants AFAICT.
– wOxxOm
Nov 20 '18 at 6:14
This is what OP wants AFAICT.
– wOxxOm
Nov 20 '18 at 6:14
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 '18 at 5:53
@PatrickEvans, I am injecting my JS to the DOM of page as its required to do so.
– Chaudhary238
Nov 20 '18 at 5:55