admin管理员组文章数量:1290099
I'm working on a chrome extension (manifest v3) and would like to pass arguments to a file that I mentioned in the chrome.scripting.executeScripts
. But, the documentation mentions that args
is only valid if the func
parameter is specified.
I found a similar question for Manifest v2 using chrome.tabs.executeScript
which has a solution, but i'm not able to use a similar approach in manifest v3.
script with func
(working)
// popup.js
chrome.scripting.executeScript({
target: { tabId: tabId, allFrames: false },
args: [eleID, type, offsetHeight + 10],
func: scrollToTarget
});
function scrollToTarget(eleID, type, headerHeight = 40) {
console.log({eleID, type, headerHeight);
}
NOT working
script with files
this is a similar approach to manifest v2
chrome.tabs.executeScript
// popup.js
chrome.scripting.executeScript({
target: { tabId: tabId, allFrames: false },
code: `var eleID = '${eleID}'; var type = '${type}'; var headerHeight = ${offsetHeight};`
}, function () {
chrome.scripting.executeScript({
target: { tabId: tabId, allFrames: false },
files: ['./executeScript.js'],
});
});
executeScrupt.js
scrollToTarget(eleID, type, headerHeight);
function scrollToTarget(eleID, type, headerHeight = 40) {
console.log({eleID, type, headerHeight);
}
I'm working on a chrome extension (manifest v3) and would like to pass arguments to a file that I mentioned in the chrome.scripting.executeScripts
. But, the documentation mentions that args
is only valid if the func
parameter is specified.
I found a similar question for Manifest v2 using chrome.tabs.executeScript
which has a solution, but i'm not able to use a similar approach in manifest v3.
script with func
(working)
// popup.js
chrome.scripting.executeScript({
target: { tabId: tabId, allFrames: false },
args: [eleID, type, offsetHeight + 10],
func: scrollToTarget
});
function scrollToTarget(eleID, type, headerHeight = 40) {
console.log({eleID, type, headerHeight);
}
NOT working
script with files
this is a similar approach to manifest v2
chrome.tabs.executeScript
// popup.js
chrome.scripting.executeScript({
target: { tabId: tabId, allFrames: false },
code: `var eleID = '${eleID}'; var type = '${type}'; var headerHeight = ${offsetHeight};`
}, function () {
chrome.scripting.executeScript({
target: { tabId: tabId, allFrames: false },
files: ['./executeScript.js'],
});
});
executeScrupt.js
scrollToTarget(eleID, type, headerHeight);
function scrollToTarget(eleID, type, headerHeight = 40) {
console.log({eleID, type, headerHeight);
}
Share
Improve this question
edited Sep 3, 2022 at 7:53
Gangula
asked Sep 2, 2022 at 17:31
GangulaGangula
7,3525 gold badges50 silver badges86 bronze badges
1 Answer
Reset to default 14Option 1
executeScript in MV3 doesn't have code
parameter so you need to rewrite it using func
and set the arguments as properties of window
(or its alias self
), which is the same as a globally declared var
for all practical purposes (assuming you don't use world: 'MAIN'
).
chrome.scripting.executeScript({
target: {tabId},
args: [{eleID, type, headerHeight: offsetHeight + 10}],
func: vars => Object.assign(self, vars),
}, () => {
chrome.scripting.executeScript({target: {tabId}, files: ['./executeScript.js']});
});
Option 2
Alternatively, you can make your injected file declare the functions without calling them:
// executeScript.js
function scrollToTarget(eleID, type, headerHeight = 40) {
console.log(eleID, type, headerHeight);
}
Then you'll call the function after injecting the file:
chrome.scripting.executeScript({target: {tabId}, files: ['./executeScript.js']}, () => {
chrome.scripting.executeScript({
target: {tabId},
args: [eleID, type, offsetHeight + 10],
func: (...args) => scrollToTarget(...args),
});
});
本文标签:
版权声明:本文标题:javascript - Pass arguments to a file script executed by chrome.scripting.executeScripts? (manifest v3) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741482855a2381263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论