That is my code from my background.js:
chrome.webNavigation.onCompleted.addListener(function(o) {
chrome.tabs.executeScript(o.tabId, {
file: "test.js"
});
}, {
url: [
{hostContains: 'google.com'}
]
});
What I want now is an if statement right where the test.js gets called. Something like this:
If a == b then execute the line 'file: "test.js"' else do nothing
I tried this but it didn't work:
chrome.webNavigation.onCompleted.addListener(function(o) {
chrome.tabs.executeScript(o.tabId, {
if (a == b) {
file: "test.js"
}
});
}
Anyone know what I'm doing wrong ? Thanks !
You can't use an if
statement inside an object literal. That's simply not valid syntax. A possible solution would be to pass a different object based on the condition:
chrome.webNavigation.onCompleted.addListener(function(o) {
chrome.tabs.executeScript(o.tabId, (a == b) ? { file: "test.js"} : {});
}
The second parameter of the executeScript function takes an InjectDetails https://developer.chrome.com/extensions/tabs#type-InjectDetails object, you can't put an if statement in there.
You can have the if statement return an object literal, such as
chrome.webNavigation.onCompleted.addListener(function(o) {
chrome.tabs.executeScript(o.tabId,
if (a == b) return {
file: "test.js"
}
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.