Skip to content

Commit

Permalink
revise wrapper
Browse files Browse the repository at this point in the history
- Do not try to 'inject script in site context' anymore,
  as it has no observable effect (if I haven't missed something).
  Thus wrapper is simpler now.
- It's now real IIFE, and does not pollute global namespace with
  `wrapper`, `script`, `info` and so on.
- No need to cut that 'inject' part of wrapper for mobile app.
  • Loading branch information
johndoe committed Jan 13, 2019
1 parent c791bb1 commit 2bea635
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
14 changes: 4 additions & 10 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@
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) {
;(function(plugin_info){
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};
Expand All @@ -86,13 +85,8 @@
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);
})({script: GM_info && GM_info.script && {version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description}})
// wrapper end
"""

Expand Down
24 changes: 12 additions & 12 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +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) {

// anonymous function wrapper for the code - any variables/functions not placed into 'window' will be private
(function(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;
Expand Down Expand Up @@ -208,11 +207,12 @@ if(typeof window.plugin !== 'function') window.plugin = function() {};
@@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@@',
script: this.GM_info && this.GM_info.script && {
version: GM_info.script.version,
name: GM_info.script.name,
description: GM_info.script.description,
}
}); // 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

0 comments on commit 2bea635

Please sign in to comment.