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

Move wrapper template to separate file #238

Merged
merged 8 commits into from
Oct 17, 2019
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build
localbuildsettings.py
local*.py
*.pyc
*.swp
*~
Expand Down
72 changes: 20 additions & 52 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import json
import shelve
import hashlib
from importlib import import_module # Python >= 2.7

try:
import urllib2
Expand All @@ -20,19 +21,18 @@
# load settings file
from buildsettings import buildSettings

# load option local settings file
try:
defaultBuild = None
if os.path.isfile('./localbuildsettings.py'):
# load optional local settings file
from localbuildsettings import buildSettings as localBuildSettings

buildSettings.update(localBuildSettings)
except ImportError:
pass

# load default build
try:
from localbuildsettings import defaultBuild
except ImportError:
defaultBuild = None
# load default build
try:
from localbuildsettings import defaultBuild
except ImportError:
pass

buildName = defaultBuild

Expand Down Expand Up @@ -60,42 +60,8 @@
buildMobile = settings.get('buildMobile')
gradleOptions = settings.get('gradleOptions', '')
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
pluginWrapperStart = """
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 = '@@BUILDNAME@@';
plugin_info.dateTimeVersion = '@@DATETIMEVERSION@@';
plugin_info.pluginId = '@@PLUGINNAME@@';
//END PLUGIN AUTHORS NOTE

"""

pluginWrapperStartUseStrict = pluginWrapperStart.replace("{\n", "{\n\"use strict\";\n", 1)

pluginWrapperEnd = """
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();
} // 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);

"""

pluginWrapper = import_module(settings.get('pluginWrapper','pluginwrapper'))
pluginWrapper.startUseStrict = pluginWrapper.start.replace("{\n", "{\n\"use strict\";\n", 1)

pluginMetaBlock = """// @updateURL @@UPDATEURL@@
// @downloadURL @@DOWNLOADURL@@
Expand Down Expand Up @@ -156,9 +122,11 @@ def doReplacements(script, updateUrl, downloadUrl, pluginName=None):
script = re.sub('@@INJECTCODE@@', loadCode, script)

script = script.replace('@@METAINFO@@', pluginMetaBlock)
script = script.replace('@@PLUGINSTART@@', pluginWrapperStart)
script = script.replace('@@PLUGINSTART-USE-STRICT@@', pluginWrapperStartUseStrict)
script = script.replace('@@PLUGINEND@@', pluginWrapperEnd)
script = script.replace('@@PLUGINSTART@@', pluginWrapper.start)
script = script.replace('@@PLUGINSTART-USE-STRICT@@', pluginWrapper.startUseStrict)
script = script.replace('@@PLUGINEND@@',
pluginWrapper.end if pluginName == 'total-conversion-build'
else pluginWrapper.setup + pluginWrapper.end)

script = re.sub('@@INCLUDERAW:([0-9a-zA-Z_./-]+)@@', loaderRaw, script)
script = re.sub('@@INCLUDESTRING:([0-9a-zA-Z_./-]+)@@', loaderString, script)
Expand Down Expand Up @@ -231,7 +199,7 @@ def saveScriptAndMeta(script, ourDir, filename, oldDir=None):

downloadUrl = distUrlBase and distUrlBase + '/total-conversion-build.user.js' or 'none'
updateUrl = distUrlBase and distUrlBase + '/total-conversion-build.meta.js' or 'none'
main = doReplacements(main, downloadUrl=downloadUrl, updateUrl=updateUrl)
main = doReplacements(main, downloadUrl=downloadUrl, updateUrl=updateUrl, pluginName='total-conversion-build')

saveScriptAndMeta(main, outDir, 'total-conversion-build.user.js', oldDir)

Expand Down Expand Up @@ -278,11 +246,11 @@ def saveScriptAndMeta(script, ourDir, filename, oldDir=None):
shutil.rmtree("mobile/assets/plugins")
except:
pass
ignore_patterns = settings.get('ignore_patterns') or []
ignore_patterns.append('*.meta.js')
shutil.copytree(os.path.join(outDir, "plugins"), "mobile/assets/plugins",
# do not include desktop-only plugins to mobile assets
ignore=shutil.ignore_patterns('*.meta.js',
'force-https*', 'speech-search*', 'basemap-cloudmade*',
'scroll-wheel-zoom-disable*'))
ignore=shutil.ignore_patterns(*ignore_patterns))

if buildMobile != 'copyonly':
# now launch 'ant' to build the mobile project
Expand Down
2 changes: 2 additions & 0 deletions buildsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# buildMobile - optional - if set, mobile builds are built with 'gradle'. requires the Android SDK and appropriate mobile/local.properties file configured
# preBuild - optional - an array of strings to run as commands, via os.system, before building the scripts
# postBuild - optional - an array of string to run as commands, via os.system, after all builds are complete
# pluginWrapper - optional - name of module to import custom plugin wrapper from


buildSettings = {
Expand All @@ -33,6 +34,7 @@
'resourceUrlBase': None,
'distUrlBase': None,
'buildMobile': 'debug',
'ignore_patterns': ['speech-search*', 'scroll-wheel-zoom-disable*'],
},


Expand Down
33 changes: 5 additions & 28 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
// @id ingress-intel-total-conversion@jonatkins
// @name IITC: Ingress intel map total conversion
// @version 0.29.1.@@DATETIMEVERSION@@
// @namespace https://github.com/IITC-CE/ingress-intel-total-conversion
// @updateURL @@UPDATEURL@@
// @downloadURL @@DOWNLOADURL@@
// @description [@@BUILDNAME@@-@@BUILDDATE@@] Total conversion for the ingress intel map.
// @include https://intel.ingress.com/*
// @match https://intel.ingress.com/*
// @grant none
@@METAINFO@@
// @run-at document-end
// ==/UserScript==

@@PLUGINSTART@@
window.script_info = plugin_info;

// REPLACE ORIG SITE ///////////////////////////////////////////////////
if (document.documentElement.getAttribute('itemscope') !== null) {
Expand All @@ -26,8 +23,7 @@ document.body.onload = function() {};
//originally code here parsed the <Script> tags from the page to find the one that defined the PLAYER object
//however, that's already been executed, so we can just access PLAYER - no messing around needed!

var PLAYER = window.PLAYER || (typeof unsafeWindow !== 'undefined' && unsafeWindow.PLAYER);
if (!PLAYER || !PLAYER.nickname) {
if (!window.PLAYER || !PLAYER.nickname) {
// page doesn’t have a script tag with player information.
if (document.getElementById('header_email')) {
// however, we are logged in.
Expand Down Expand Up @@ -102,18 +98,6 @@ 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;




// CONFIG OPTIONS ////////////////////////////////////////////////////
window.REFRESH = 30; // refresh view every 30s (base time)
window.ZOOM_LEVEL_ADJ = 5; // add 5 seconds per zoom level
Expand Down Expand Up @@ -216,11 +200,4 @@ var ulog = (function (module) {
// fixed Addons
RegionScoreboard.setup();

} // 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);
@@PLUGINEND@@
13 changes: 1 addition & 12 deletions mobile/plugins/user-location.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,8 @@
// @name IITC plugin: User Location
// @category Tweaks
// @version 0.2.1.@@DATETIMEVERSION@@
// @namespace https://github.com/jonatkins/ingress-intel-total-conversion
// @updateURL @@UPDATEURL@@
// @downloadURL @@DOWNLOADURL@@
// @description [@@BUILDNAME@@-@@BUILDDATE@@] Show user location marker on map
// @include https://*.ingress.com/intel*
// @include http://*.ingress.com/intel*
// @match https://*.ingress.com/intel*
// @match http://*.ingress.com/intel*
// @include https://*.ingress.com/mission/*
// @include http://*.ingress.com/mission/*
// @match https://*.ingress.com/mission/*
// @match http://*.ingress.com/mission/*
// @grant none
@@METAINFO@@
// ==/UserScript==

@@PLUGINSTART@@
Expand Down
42 changes: 42 additions & 0 deletions pluginwrapper.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 = '@@BUILDNAME@@';
plugin_info.dateTimeVersion = '@@DATETIMEVERSION@@';
plugin_info.pluginId = '@@PLUGINNAME@@';
//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);

"""