how do we check if this is a content script?
We develop extensions for Firefox. The script needs to know if it is a content script or a background script (sometimes the same script can be both a content script and a background script). We tried to do it with the following code:
if (typeof exports === 'undefined') {
// we're in a content script.
} else {
// we're in a background script.
}
But the problem is that typeof exports
is not 'undefined'
in some content scripts. Is there a better way to know when we are in a content script?
Edit : I has a mistake and actually typeof exports
is 'undefined'
in the content script. So maybe checking typeof exports
is a reliable way to identify content scripts.
The first thing that came up into my mind - include some separate file to extension as content script, that will have global variable. Eg file content_script_definition.js:
isContentScript = true;
And then use next code:
if (typeof isContentScript !== 'undefined' && isContentScript) {
// we're in a content script.
} else {
// we're in a background script.
}
Maybe there is some documented functionality, not sure about this...
其他方式
if(String(this).indexOf("Sandbox") >= 0){
//main.js
}
else{
//content script
}
链接地址: http://www.djcxy.com/p/45754.html
上一篇: Firefox插件在运行Windows时不会将jquery作为内容脚本加载
下一篇: 我们如何检查这是否为内容脚本?