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

revise wrapper #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 9 additions & 14 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
gradleBuildFile = settings.get('gradleBuildFile', 'mobile/build.gradle')

# 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 IIFE doesn't apply to the plugin code body
pluginWrapperStart = """
function wrapper(plugin_info) {
if (typeof window.plugin !== 'function') window.plugin = function() {};
(function wrapper (plugin_info){
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};
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)
Expand All @@ -82,17 +82,12 @@

pluginWrapperEnd = """
setup.info = plugin_info; //add the script info data to the function as a property
if(!window.bootPlugins) window.bootPlugins = [];
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();
} // 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 (window.iitcLoaded && typeof setup === 'function') setup();
})({ script: typeof GM_info !== 'undefined' && GM_info.script && {
version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description
}})// wrapper end

"""

Expand Down
32 changes: 16 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,9 @@ document.body.innerHTML = ''
// avoid error by stock JS
+ '<div id="play_button"></div>';

// 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.
function wrapper(info) {
// 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!)
window.script_info = info;

// anonymous function wrapper for the code - any variables/functions not placed into 'window' will be private
(function(info){



Expand Down Expand Up @@ -202,17 +198,21 @@ window.overlayStatus = {};

// plugin framework. Plugins may load earlier than iitc, so don’t
// overwrite data
if(typeof window.plugin !== 'function') window.plugin = function() {};
if (typeof window.plugin !== 'function') window.plugin = function() {};

// (not saving the full GM_info - it contains the ENTIRE script source!)
window.script_info = info;
if (typeof GM_info !== 'undefined') {
info.script = {
version: GM_info.script.version,
name: GM_info.script.name,
description: GM_info.script.description,
};
}


@@INJECTCODE@@


} // end of wrapper

// inject code into site context
var script = document.createElement('script');
var info = { buildName: '@@BUILDNAME@@', dateTimeVersion: '@@DATETIMEVERSION@@' };
if (this.GM_info && this.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);
})({ buildName: '@@BUILDNAME@@', dateTimeVersion: '@@DATETIMEVERSION@@' });
// end of wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@
public class IITC_FileManager {
private static final WebResourceResponse EMPTY =
new WebResourceResponse("text/plain", "UTF-8", new ByteArrayInputStream("".getBytes()));
private static final String WRAPPER_NEW = "wrapper(info);";
private static final String WRAPPER_OLD =
"script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));\n"
+ "(document.body || document.head || document.documentElement).appendChild(script);";

// update interval is 2 days by default
private long mUpdateInterval = 1000 * 60 * 60 * 24 * 7;
Expand Down Expand Up @@ -213,9 +209,7 @@ private InputStream prepareUserScript(final InputStream stream) {
final HashMap<String, String> info = getScriptInfo(content);

final JSONObject jObject = new JSONObject(info);
final String gmInfo = "var GM_info={\"script\":" + jObject.toString() + "}";

content = content.replace(WRAPPER_OLD, WRAPPER_NEW);
final String gmInfo = "var GM_info = { \"script\":" + jObject.toString() + " };";

return new ByteArrayInputStream((gmInfo + content).getBytes());
}
Expand Down