在GNOME Shell扩展'这是猴子的补丁,这是出乎意料的

After GNOME Shell extension's monkey-patching, this.parent is unexpected

本文关键字:猴子 补丁 出乎意料 Shell GNOME 扩展      更新时间:2023-09-26

对于提升激活的GNOME Shell 3.16扩展,我正在尝试对AppSwitcherPopup._finish方法进行猴子补丁。与原始版本一样,修补版本调用this.parent:

function _modifiedFinish(timestamp) {
    // ...monkey-patched code...
    this.parent(timestamp);
}
function enable() {
    _originalFinish = AltTab.AppSwitcherPopup.prototype._finish;
    AltTab.AppSwitcherPopup.prototype._finish = _modifiedFinish;
}

(完整代码)

但我在控制台中得到了这个堆栈跟踪(来自运行gnome-shell --replace):

(gnome-shell:24452): Gjs-WARNING **: JS ERROR: TypeError: The method '_keyReleaseEvent' is not on the superclass
_parent@resource:///org/gnome/gjs/modules/lang.js:129
_modifiedFinish@/home/lastorset/.local/share/gnome-shell/extensions/Alt_Tab_Mod_Only_Raise_Activated_Window@dsboger.com.br/extension.js:34
SwitcherPopup<._keyReleaseEvent@resource:///org/gnome/shell/ui/switcherPopup.js:199
wrapper@resource:///org/gnome/gjs/modules/lang.js:169

在这种情况下,SwitcherPopup._keyReleaseEvent正在调用this,而this应该AppSwitcherPopup子类的实例。我相信this.parent在打补丁后应该是一样的——为什么它现在试图呼叫呼叫者?既然如此,为什么不成功呢?

我查找了生成this.parent的GJS代码,但我找不到缺少什么。

经过进一步挖掘,我找到了一种修复方法。在GJS类模型中,parent函数实际上是在寻找方法所有者的超类,以便调用同名方法。看起来每个GJS类都有一个设置_owner的wrapFunction辅助对象。我用它来修补函数:

AltTab.AppSwitcherPopup.prototype._finish = AltTab.AppSwitcherPopup.wrapFunction('_finish', _modifiedFinish);