Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better plugin wrapper #358

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions buildsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@
'version_timestamp': True,
},

# use no-inject wrapper for easy breakpoints (Tampermonkey only!)
'tmdev': {
'url_dist_base': 'http://localhost:8000',
'update_file': '.user.js',
'version_timestamp': True,
'plugin_wrapper': 'pluginwrapper_noinject',
},

# default entry that also builds the mobile .apk
# requires: Java JDK, android-sdk
'mobile': {
Expand Down
47 changes: 20 additions & 27 deletions pluginwrapper.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
# plugin wrapper code snippets. handled as macros, to ensure that
# 1. indentation caused by the "function wrapper()" doesn't apply to the plugin code body
# 2. the wrapper is formatted correctly for removal by the IITC Mobile android app

# 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.
# plugin wrapper code snippets

# 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 = """
function wrapper(plugin_info) {
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};
var plugin_info = (typeof GM_info === 'undefined') ? {} : (function (s) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we should rely on GM_info definition, but iOS is not ready: HubertZhang/IITC-Mobile#1

['version','name','description'].forEach(function (k) { s[k] = GM_info.script[k]; });
return {scriptMetaStr:GM_info.scriptMetaStr, script:s};
}({}));

//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)
// 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)
plugin_info.buildName = '@build_name@';
plugin_info.dateTimeVersion = '@build_date@';
plugin_info.pluginId = '@plugin_id@';
//END PLUGIN AUTHORS NOTE
// END PLUGIN AUTHORS NOTE
Comment on lines +16 to +21
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid this, if we add this info to meta block, see #242.


window = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window;
// ensure plugin framework is there, even if iitc is not yet loaded
window.plugin = window.plugin || function () {};

"""

setup = """
setup.info = plugin_info; //add the script info data to the function as a property
if(!window.bootPlugins) window.bootPlugins = [];
window.bootPlugins.push(setup);
// if IITC has already booted, immediately run the 'setup' function
if(window.iitcLoaded && typeof setup === 'function') setup();"""

end = """
} // wrapper end
// inject code into site context
var script = document.createElement('script');
var info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description };
script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));
(document.body || document.head || document.documentElement).appendChild(script);

if (typeof setup !== 'function') {
var setup = {}; plugin_info.error = 'setup is not a function';
}
setup.info = plugin_info;
(window.bootPlugins = window.bootPlugins || []).push(setup);
if (window.iitcLoaded) { setup(); }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps better expose this:

function safeSetup (setup) {

Otherwise we would need special handling to see such plugins in About:

window.aboutIITC = function () {

"""

end = ''
14 changes: 0 additions & 14 deletions pluginwrapper_noinject.py

This file was deleted.

42 changes: 42 additions & 0 deletions pluginwrapper_old.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# plugin wrapper code snippets. handled as macros, to ensure that
# 1. indentation caused by the "function wrapper()" doesn't apply to the plugin code body
# 2. the wrapper is formatted correctly for removal by the IITC Mobile android app

# 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 = """
function wrapper(plugin_info) {
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') 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)
plugin_info.buildName = '@build_name@';
plugin_info.dateTimeVersion = '@build_date@';
plugin_info.pluginId = '@plugin_id@';
//END PLUGIN AUTHORS NOTE

"""

setup = """
setup.info = plugin_info; //add the script info data to the function as a property
if(!window.bootPlugins) window.bootPlugins = [];
window.bootPlugins.push(setup);
// if IITC has already booted, immediately run the 'setup' function
if(window.iitcLoaded && typeof setup === 'function') setup();"""

end = """
} // wrapper end
// inject code into site context
var script = document.createElement('script');
var info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description };
script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));
(document.body || document.head || document.documentElement).appendChild(script);

"""