如何使用 Capacitor Browser 插件操作/清除 cookie

How to manipulate / clear cookies with Capacitor Browser plugin

我需要清除使用 Capacitor 浏览器插件打开的网站的 cookie。 我找不到方法。

在 Capacitor Browser 插件中有一个“browserPageLoaded”侦听器,这是放置我的代码的理想位置,但我仍然找不到任何访问 cookie 的方法。 您如何从那里访问 cookie?

不幸的是,这是不可能的,浏览器插件在 iOS 上使用 SFSafariViewController,在 Android 上使用 Chrome 自定义选项卡,出于安全原因不共享 cookie,请参阅答案这里 https://github.com/ionic-team/capacitor-plugins/issues/7. To solve this problem you could use cordova's inAppBrowser which also works with Capacitor https://ionicframework.com/docs/native/in-app-browser. Among the options there is "clearcache" and "clearsessioncache". The list of options and examples are available at the following link https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/.

示例:

控制台:

npm install cordova-plugin-inappbrowser
npm install @ionic-native/in-app-browser
ionic cap sync

代码:

import {
  InAppBrowser,
  InAppBrowserObject,
  InAppBrowserOptions,
} from "@ionic-native/in-app-browser/ngx";

constructor(private iab: InAppBrowser) {}

createNewPageBrowserInApp(url, target) {
    if (this.iab) {
      this.currentPageInAppBrowser = this.iab.create(
        url,
        target,
        this.options(inAppBrowserType)
      );
    }
  }

options(inAppBrowserType: InAppBrowserType): InAppBrowserOptions {
    switch (inAppBrowserType) {
      case InAppBrowserType.NavigationPage: {
        console.log("InAppBrowserType.NavigationPage");
        return {
          toolbarcolor: "#FFFFFF",
          location: "yes",
          hidden: "no",
          clearcache: "yes",
          clearsessioncache: "yes",
          zoom: "yes", //Android only
          hardwareback: "yes",
          mediaPlaybackRequiresUserAction: "no",
          hidenavigationbuttons: "no",
          navigationbuttoncolor: "#428bca",
          hideurlbar: "yes",
          shouldPauseOnSuspend: "no", //Android only
          disallowoverscroll: "no", //iOS only
          usewkwebview: "yes", //iOS only
          toolbar: "yes", //iOS only
          enableViewportScale: "no", //iOS only
          allowInlineMediaPlayback: "yes", //iOS only
          presentationstyle: "pagesheet", //iOS only
          fullscreen: "no", //Windows only
          useWideViewPort: "yes",
        };
      }

InAppBrowserType 是供内部使用的枚举类型