Skip to content

Commit

Permalink
fixup! better plugin wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
johndoe committed Mar 10, 2020
1 parent 5b2eca4 commit 557d0e6
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions pluginwrapper.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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';
}
Expand All @@ -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();
"""

0 comments on commit 557d0e6

Please sign in to comment.