diff --git a/pluginwrapper.py b/pluginwrapper.py index 2e99b995c..55960b7a8 100644 --- a/pluginwrapper.py +++ b/pluginwrapper.py @@ -1,13 +1,17 @@ -# plugin wrapper code snippets +# plugin wrapper code snippets. handled as macros, to ensure that +# indentation caused by the wrapper function doesn't apply to the plugin code body + +# putting everything in a wrapper function that in turn is placed in a +# script tag on the website allows us to execute in the site's context +# instead of in the Greasemonkey/Extension/etc. context. # a cut-down version of GM_info is passed as a parameter to the script # (not the full GM_info - it contains the ENTIRE script source!) start = """ -var plugin_info = (typeof GM_info === 'undefined') ? {} : (function (s) { - ['version','name','description'].forEach(function (k) { s[k] = GM_info.script[k]; }); - return {scriptMetaStr:GM_info.scriptMetaStr, script:s}; -}({})); +var wrapper = function (plugin_info) { +// ensure plugin framework is there, even if iitc is not yet loaded +window.plugin = window.plugin || function () {}; // PLUGIN AUTHORS: writing a plugin outside of the IITC build environment? if so, delete these lines!! // (leaving them in place might break the 'About IITC' page or break update checks) @@ -16,14 +20,9 @@ plugin_info.pluginId = '@plugin_id@'; // END PLUGIN AUTHORS NOTE -window = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window; -// ensure plugin framework is there, even if iitc is not yet loaded -window.plugin = window.plugin || function () {}; - """ setup = """ - if (typeof setup !== 'function') { var setup = {}; plugin_info.error = 'setup is not a function'; } @@ -32,4 +31,16 @@ if (window.iitcLoaded) { setup(); } """ -end = '' +end = """ +} // wrapper end +var plugin_info = (typeof GM_info === 'undefined') ? {} : (function (s) { + ['version','name','description'].forEach(function (k) { s[k] = GM_info.script[k]; }); + return {scriptMetaStr:GM_info.scriptMetaStr, script:s}; +}({})); +if (typeof unsafeWindow === 'undefined' || unsafeWindow === window) { return wrapper(plugin_info); } +// inject code into site context +var script = document.createElement('script'); +script.append('('+ wrapper +')('+JSON.stringify(plugin_info)+');'); +document.body.appendChild(script).remove(); + +""" \ No newline at end of file