Skip to content

[BUG] 祖先类别自定义成员未有继承在半沙盒 #1462

@cyfung1031

Description

@cyfung1031

Problem Description

测试需要准备两个脚本,请确保它们的次序 (有次序限制,难以e2e测试)
如果次序不一样,请重新停用&打开(让Chrome API重新注册,直至出现需要的顺序)

Image

01

// ==UserScript==
// @name         test_ancestor_method_override_unwrap
// @description  test_ancestor_method_override_unwrap
// @version      0.1.0
// @namespace    UserScript
// @match        https://example.com/*?test_ancestor_method_override
// @license      MIT
// @author       CY Fung
// @grant        none
// @run-at       document-start
// @unwrap
// @inject-into  page
// ==/UserScript==

(function() {
    'use strict';

    console.log("First Execution: test_ancestor_method_override_unwrap");

    if (!EventTarget.prototype.addEventListener656) {
        EventTarget.prototype.addEventListener656 = EventTarget.prototype.addEventListener;
        EventTarget.prototype.addEventListener = function (type, handler, option = void 0) {
            // console.log("test");
            return this.addEventListener656(type, handler, option);
        }
    }

    // Your code here...
})();

02

// ==UserScript==
// @name         test_ancestor_method_override_later
// @description  test_ancestor_method_override_later
// @version      0.1.0
// @namespace    UserScript
// @match        https://example.com/*?test_ancestor_method_override
// @license      MIT
// @author       CY Fung
// @grant        GM.getValue
// @run-at       document-idle
//
// ==/UserScript==

(function() {
    'use strict';

    console.log("Second Execution: test_ancestor_method_override_later");
    console.log(901, typeof window.addEventListener, typeof unsafeWindow.addEventListener);
    console.log(902, typeof window.addEventListener656, typeof unsafeWindow.addEventListener656);

    console.log(903, window.addEventListener);

    // Your code here...
})();

需要的话你可以在 scriptcat 加 debug

Image

Result

Image
  1. unwrap 先执行
  2. unwrap 定义了自定义成员在 window 的祖先 (EventTarget.prototype)
  3. content跟page的半沙盒环境初始化
  4. page 的半沙盒 window 没有这个 自定义成员 (因为为了符合 TM半沙盒设计,没有prototype 继承

Reproduction Steps

ScriptCat Version

Operating System and Browser Information

Additional Information (Optional)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions