- around: function around (obj, name, func) {
- let next = obj[name];
- let current = obj[name] = function () {
- let self = this, args = arguments;
- return func.call(self, function () next.apply(self, args), args);
+ around: (function () {
+ function getPluginPath () {
+ let pluginPath;
+ Error('hoge').stack.split(/\n/).some(
+ function (s)
+ let (m = s.match(/^\(\)@chrome:\/\/liberator\/content\/liberator\.js -> (.+):\d+$/))
+ (m && (pluginPath = m[1]))
+ );
+ return pluginPath;
+ }
+
+ let restores = {};
+
+ return function (obj, name, func, autoRestore) {
+ let original;
+ let restore = function () obj[name] = original;
+ if (autoRestore) {
+ let pluginPath = getPluginPath();
+ if (pluginPath) {
+ restores[pluginPath] =
+ (restores[pluginPath] || []).filter(
+ function (res) (
+ res.object != obj ||
+ res.name != name ||
+ (res.restore() && false)
+ )
+ );
+ restores[pluginPath].push({
+ object: obj,
+ name: name,
+ restore: restore
+ });
+ } else {
+ liberator.echoerr('getPluginPath failed');
+ }
+ }
+ original = obj[name];
+ let current = obj[name] = function () {
+ let self = this, args = arguments;
+ return func.call(self, function (_args) original.apply(self, _args || args), args);
+ };
+ libly.$U.extend(current, {original: original.original || original, restore: restore});
+ return libly.$U.extend({
+ original: original,
+ current: current,
+ restore: restore
+ }, [original, current]);