-
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
Draft
johnd0e
wants to merge
2
commits into
IITC-CE:master
Choose a base branch
from
johnd0e:better-wrapper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,5 @@ | ||||||
# 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 | ||||||
# 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 | ||||||
|
@@ -10,33 +9,38 @@ | |||||
# (not the full GM_info - it contains the ENTIRE script source!) | ||||||
|
||||||
start = """ | ||||||
function wrapper(plugin_info) { | ||||||
var wrapper = function (plugin_info) { | ||||||
// ensure plugin framework is there, even if iitc is not yet loaded | ||||||
if(typeof window.plugin !== 'function') window.plugin = function() {}; | ||||||
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) | ||||||
// 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 | ||||||
|
||||||
""" | ||||||
|
||||||
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();""" | ||||||
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 = """ | ||||||
} // 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'); | ||||||
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); | ||||||
script.append('('+ wrapper +')('+JSON.stringify(plugin_info)+');'); | ||||||
document.body.appendChild(script).remove(); | ||||||
|
||||||
""" | ||||||
""" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
""" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We can avoid this, if we add this info to meta block, see #242.