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

Feature: chrome proxy #59

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

HairlessVillager
Copy link
Collaborator

这个 PR 提供了对 chrome 命名空间的代理的支持。具体来说这个 PR 做了以下修改:

  1. 新增了setChromeProxygetChromeProxy函数来设置/获取chrome命名空间的代理,具体用法参见注释;
  2. 将仓库中所有对chrome的引用都换成了getChromeProxy()

此外这个 PR 还做了以下修改:

  1. src/universal_tools目录移动到src/common/tools目录,并修改了对应的引用。

你可以通过以下方法来验证这个 PR:

  1. 在合适的位置使用以下代码设置 ChromeProxy
class MyChromeProxy {
  public static tabs_onUpdated_removeListener(callback: (tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => void): void {
    console.log(callback);
    return chrome.tabs.onUpdated.removeListener(callback);
  }
  public static tabs_get(tabId: number): Promise<chrome.tabs.Tab> {
    console.log(tabId);
    return chrome.tabs.get(tabId);
  }
  public static windows_create(createData: chrome.windows.CreateData): Promise<chrome.windows.Window> {
    console.log(createData);
    return chrome.windows.create(createData);
  }
}
setChromeProxy(MyChromeProxy);
  1. 编译并运行;
  2. 期望行为是应该能在 DevTools 中看到打印的日志。

这个 PR 有一个类型安全的问题:对 chrome 代理后 IDE 无法提供函数签名的提示(如src/extension/content/index.tsgetChromeProxy().runtime.onMessage.addListener(...)函数)。这个问题我暂时没有想到好的解决办法。


This PR provides support for proxying the chrome namespace. Specifically, the following changes have been made in this PR:

  1. Added the setChromeProxy and getChromeProxy functions to set/get the proxy for the chrome namespace. For specific usage, please refer to the comments;
  2. Replaced all references to chrome in the repository with getChromeProxy().

Additionally, the following changes have been made in this PR:

  1. Moved the src/universal_tools directory to src/common/tools and updated the corresponding references.

You can verify this PR using the following method:

  1. Set the ChromeProxy using the following code in an appropriate location:
class MyChromeProxy {
  public static tabs_onUpdated_removeListener(callback: (tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => void): void {
    console.log(callback);
    return chrome.tabs.onUpdated.removeListener(callback);
  }
  public static tabs_get(tabId: number): Promise<chrome.tabs.Tab> {
    console.log(tabId);
    return chrome.tabs.get(tabId);
  }
  public static windows_create(createData: chrome.windows.CreateData): Promise<chrome.windows.Window> {
    console.log(createData);
    return chrome.windows.create(createData);
  }
}
setChromeProxy(MyChromeProxy);
  1. Compile and run the code;
  2. The expected behavior is that logs should be printed in DevTools.

There is a type safety issue with this PR: After proxying chrome, the IDE cannot provide function signature hints (such as in the getChromeProxy().runtime.onMessage.addListener(...) function in src/extension/content/index.ts). I haven't come up with a good solution to this problem yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant