Skip to content

Commit

Permalink
build_plugin.py: some code can be placed outside of wrapper
Browse files Browse the repository at this point in the history
This may be needed to use GM_* functions
- https://wiki.greasespot.net/Greasemonkey_Manual:API
- https://www.tampermonkey.net/documentation.php

To leave some code out of wrapper use speciar marker (on separate line):

/*wrapped-from-here*/
  • Loading branch information
johndoe committed Mar 5, 2020
1 parent ea9cb98 commit dff4de3
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions build_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,22 @@ def process_file(source, out_dir, dist_path=None, deps_list=None):

path = source.parent # used as root for all (relative) paths
script = re.sub(r"'@bundle_code@';", partial(bundle_code, path=path), script)
try:
script_before_wrapper, script = script.split('\n/*wrapped-from-here*/\n', 1)
except ValueError:
script_before_wrapper = ''

wrapper = get_module(settings.plugin_wrapper)
template = r"'@(\w+)(?::([\w./-]+))?@'" # to find '@keyword[:path]@' patterns
repl = partial(expand_template, path=path)
data = [
meta,
script_before_wrapper,
re.sub(template, repl, wrapper.start),
re.sub(template, repl, script),
wrapper.setup,
wrapper.setup if not is_main else '', # it's for plugins only
wrapper.end,
]
if is_main:
data.pop(3) # remove wrapper.setup (it's for plugins only)

(out_dir / (plugin_name + '.user.js')).write_text(''.join(data), encoding='utf8')
if settings.url_dist_base and settings.update_file == '.meta.js':
Expand Down

0 comments on commit dff4de3

Please sign in to comment.