-
Notifications
You must be signed in to change notification settings - Fork 109
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||||||
['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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps better expose this:
Otherwise we would need special handling to see such plugins in About:
|
||||||
""" | ||||||
|
||||||
end = '' |
This file was deleted.
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); | ||
|
||
""" |
There was a problem hiding this comment.
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