Compatibility/System Addon/Override Policies and Workflows: Difference between revisions

Jump to navigation Jump to search
Update technical details (get rid of window.eval and fix location for content script registration)
(→‎Types of Overrides: Add TODO to remove window.eval and provide an exportFunction example)
(Update technical details (get rid of window.eval and fix location for content script registration))
Line 90: Line 90:
As with user agent overrides, JS injections should be designed to use as little code as possible. Due to the nature of these overrides, it is hard to provide exact guidelines.
As with user agent overrides, JS injections should be designed to use as little code as possible. Due to the nature of these overrides, it is hard to provide exact guidelines.


The injected JS will be executed in its own context per default, check [https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts#Content_script_environment the MDN web docs] for details. If it is required to run JS in the websites context, JS code can be wrapped inside an ''eval'' call:
The injected JS will be executed in its own context per default, check [https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts#Content_script_environment the MDN web docs] for details. If it is required to run JS in the websites context, functions can be exported via [https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.exportFunction exportFunction]:


<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
window.eval(`(function() { // used to require window.wrappedJSObject.eval
Object.defineProperty(window.wrappedJSObject, "isTestFeatureSupported", {
   /* your code here */
  get: exportFunction(function() {
}());`);
    return true;
  }, window),
 
   set: exportFunction(function() {}, window)
});
</syntaxhighlight>
</syntaxhighlight>
TODO: we don't use window.eval, but [https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.exportFunction exportFunction]. Dennis needs to update this code example.


==== Technical details ====
==== Technical details ====
Line 104: Line 106:
JS injections are defined within individual files within the [https://github.com/mozilla/webcompat-addon/blob/master/src/webextension/injections/js/ src/webextension/injections/js/] directory of the addon sources. In this folder, you will find a collection of files, one file per override or injection. For easier cross-reference, please stick to the ''bugNNNNN-short-description.js'' file name schema.
JS injections are defined within individual files within the [https://github.com/mozilla/webcompat-addon/blob/master/src/webextension/injections/js/ src/webextension/injections/js/] directory of the addon sources. In this folder, you will find a collection of files, one file per override or injection. For easier cross-reference, please stick to the ''bugNNNNN-short-description.js'' file name schema.


In addition, the injection file has to be registered in the manifest file located at [https://github.com/mozilla/webcompat-addon/blob/master/src/webextension/manifest.json src/webextension/manifest.json]. Details on available parameters can be found [https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/content_scripts in the MDN web docs].
In addition, the injection file has to be registered in the constant array located at the top of [https://github.com/mozilla/webcompat-addon/blob/master/src/webextension/background.js#L7 src/webextension/background.js]. Details on available parameters can be found [https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/content_scripts in the MDN web docs].


== Open points for further discussion ==
== Open points for further discussion ==
202

edits

Navigation menu