woofCookies.ets 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import linysText from '../../components/texts/linysText';
  2. import linysTextTitle from '../../components/texts/linysTextTitle';
  3. import { animation_default, click_effect_default } from '../../hosts/bunch_of_defaults';
  4. import { webview } from '@kit.ArkWeb';
  5. import { match_domain } from '../../utils/url_tools';
  6. import woofControlFrame from '../woofControlFrame';
  7. import linysTimeoutButtonWithText from '../../components/buttons/linysTimeoutButtonWithText';
  8. @CustomDialog
  9. struct woofCookies {
  10. controller: CustomDialogController;
  11. // Environment
  12. @StorageProp('tablet_mode') tablet_mode: boolean = false;
  13. @StorageProp('current_url') current_url: string = "= ̄ω ̄=";
  14. @State domain_url: string = this.get_domain_url(this.current_url);
  15. // Interfaces
  16. @State cookies_site: string[] = this.get_cookies_site();
  17. // alignRules
  18. alignRules_head_non_tablet: AlignRuleOption = {
  19. top: { anchor: "__container__", align: VerticalAlign.Top },
  20. left: { anchor: "__container__", align: HorizontalAlign.Start },
  21. };
  22. alignRules_head_tablet: AlignRuleOption = {
  23. top: { anchor: "__container__", align: VerticalAlign.Top },
  24. bottom: { anchor: "controls_cookies", align: VerticalAlign.Top },
  25. left: { anchor: "__container__", align: HorizontalAlign.Start },
  26. };
  27. alignRules_foot: AlignRuleOption = {
  28. bottom: { anchor: "__container__", align: VerticalAlign.Bottom },
  29. left: { anchor: "__container__", align: HorizontalAlign.Start }
  30. };
  31. alignRules_body_tablet: AlignRuleOption = {
  32. top: { anchor: "__container__", align: VerticalAlign.Top },
  33. right: { anchor: "__container__", align: HorizontalAlign.End }
  34. };
  35. alignRules_body_non_tablet: AlignRuleOption = {
  36. top: { anchor: "title_cookies_manager", align: VerticalAlign.Bottom },
  37. bottom: { anchor: "controls_cookies", align: VerticalAlign.Top },
  38. right: { anchor: "__container__", align: HorizontalAlign.End }
  39. };
  40. // Colors
  41. @StorageProp('color_current_primary') color_current_primary: ResourceColor = $r('app.color.start_window_background');
  42. @StorageProp('color_current_secondary') color_current_secondary: ResourceColor = $r('app.color.block_color');
  43. @StorageProp('color_current_font') color_current_font: ResourceColor = $r('app.color.font_color_title');
  44. build() {
  45. woofControlFrame({
  46. title: $r('app.string.Settings_manage_cookies'),
  47. controller: this.controller
  48. }) {
  49. RelativeContainer() {
  50. Scroll() {
  51. Column({ space: 10 }) {
  52. linysText({
  53. text: $r('app.string.Settings_manage_cookies_desc_1'),
  54. max_lines: 10
  55. })
  56. .width("100%")
  57. linysText({
  58. text: $r('app.string.Settings_manage_cookies_desc_2'),
  59. max_lines: 10
  60. })
  61. .width("100%")
  62. linysText({
  63. text: $r('app.string.Settings_manage_cookies_desc_3'),
  64. max_lines: 10
  65. })
  66. .width("100%")
  67. }
  68. .width("100%")
  69. } // Title
  70. .align(Alignment.TopStart)
  71. .edgeEffect(EdgeEffect.Spring)
  72. .padding({ bottom: 10, right: 10 })
  73. .width(this.tablet_mode ? "40%" : "100%")
  74. .alignRules(this.tablet_mode ? this.alignRules_head_tablet : this.alignRules_head_non_tablet)
  75. .animation(animation_default())
  76. .constraintSize(this.tablet_mode ? {} : { maxHeight: '35%' })
  77. .id("title_cookies_manager")
  78. Column() {
  79. linysText({ text: $r('app.string.Settings_manage_cookies_this_site'), max_lines: 3 })// Cookies on
  80. .width("100%")
  81. .padding({ top: this.tablet_mode ? 0 : 10, bottom: 5 })
  82. linysText({ text: this.domain_url })// domain
  83. .width("100%")
  84. .padding({ bottom: 10 })
  85. Scroll() {
  86. Column() {
  87. ForEach(this.cookies_site, (item: string, index: number) => {
  88. Cookie({
  89. text: item,
  90. index: index,
  91. })
  92. .width("100%")
  93. })
  94. if (this.cookies_site.length == 0) {
  95. linysTextTitle({
  96. text: "当前数量为空"
  97. })
  98. .margin(30)
  99. .opacity(0.7)
  100. .animation(animation_default())
  101. }
  102. }
  103. .width("100%")
  104. } // cookies display list
  105. .scrollable(ScrollDirection.Vertical)
  106. .edgeEffect(EdgeEffect.Spring)
  107. .align(Alignment.Top)
  108. .borderRadius(10)
  109. .backgroundColor(this.color_current_secondary)
  110. .width("100%")
  111. .layoutWeight(1)
  112. } // Cookies
  113. .width(this.tablet_mode ? "60%" : "100%")
  114. .height(this.tablet_mode ? "100%" : undefined)
  115. .alignRules(this.tablet_mode ? this.alignRules_body_tablet : this.alignRules_body_non_tablet)
  116. .animation(animation_default())
  117. Column({ space: 10 }) {
  118. linysText({ text: $r('app.string.Settings_manage_cookies_clear_all'), max_lines: 5 })
  119. .width('100%')
  120. linysTimeoutButtonWithText({
  121. desc_text: '谨慎操作',
  122. button_text: ' 󰀁 ',
  123. onExecution: () => {
  124. this.clear_all_cookies();
  125. }
  126. }) // Delete
  127. } // Controls
  128. .margin({ top: 15 })
  129. .padding(this.tablet_mode ? { right: 10 } : undefined)
  130. .alignItems(HorizontalAlign.End)
  131. .alignRules(this.alignRules_foot)
  132. .animation(animation_default())
  133. .width(this.tablet_mode ? "40%" : "100%")
  134. .id("controls_cookies")
  135. }.layoutWeight(1)
  136. }
  137. }
  138. clear_all_cookies() {
  139. webview.WebCookieManager.clearAllCookiesSync();
  140. webview.WebCookieManager.saveCookieAsync();
  141. this.refresh_site_cookies_list();
  142. }
  143. refresh_site_cookies_list() {
  144. this.cookies_site = this.get_cookies_site();
  145. }
  146. get_cookies_site() {
  147. let list = webview.WebCookieManager.fetchCookieSync(this.domain_url).split('; ');
  148. if (list[list.length-1] == '') {
  149. list = list.slice(0, list.length - 1);
  150. }
  151. return list;
  152. }
  153. get_domain_url(url: string) {
  154. let match_domain_result = match_domain(url);
  155. return match_domain_result[0] + '://' + match_domain_result[1];
  156. }
  157. }
  158. export default woofCookies;
  159. @Component
  160. struct Cookie {
  161. @State text: string = "qwq";
  162. @Prop index: number;
  163. // Color
  164. @StorageProp('color_current_secondary') color_current_secondary: ResourceColor = $r('app.color.block_color');
  165. @StorageProp('color_current_font') color_current_font: ResourceColor = $r('app.color.font_color_title');
  166. build() {
  167. Column() {
  168. linysText({
  169. text: this.text,
  170. max_lines: 25
  171. })
  172. }
  173. .padding({
  174. left: 10,
  175. right: 10,
  176. top: 5,
  177. bottom: 5
  178. })
  179. .alignItems(HorizontalAlign.Start)
  180. .backgroundColor(this.color_current_secondary)
  181. .animation(animation_default())
  182. .clickEffect(click_effect_default())
  183. .width("100%")
  184. }
  185. }