It is absolutely a legitimate concern.
The easy way would be to use a mechanism similar to #ifndef
include guards in C.
Define a flag that gets set once the content script gets executed, and check if it's defined before doing anything. All scripts injected from the same extension share the JS execution context and therefore global scope.
Your content script might look like this:
if (window.contentScriptInjected !== true) {
window.contentScriptInjected = true; // global scope
/* Do your initialization and work */
}
/* global function definitions */
The check should use an explicit true
value to avoid false positives on pages that happen to have an element with id
attribute that accidentally equals the variable name - browsers create an implicit global variable that points to that DOM element so for an if
check it'll be truthy.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…