# 一些用户抱怨“类似主题”窗口

**URL:** https://meta.discourse.org/t/some-users-complaining-about-topic-similar-to-window/216505
**Category:** Feature
**Created:** [2022年一月30日 18:50 UTC](https://meta.discourse.org/t/some-users-complaining-about-topic-similar-to-window/216505 "2022-01-30T18:50:30Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2022年二月3日 10:29 UTC](https://meta.discourse.org/t/some-users-complaining-about-topic-similar-to-window/216505/5 "2022-02-03T10:29:30Z")

</div>

这可以很容易地完成。

> [@Optional Mobile Title Header](https://meta.discourse.org/t/optional-mobile-title-switch-component/104238):
>
> discourse2Summary Optional Mobile Title Header makes the feature where the header switches to mobile title optional for both users and sites.eyeglassesPreview [Preview on Discourse Theme Creator](https://discourse.theme-creator.io/theme/Discourse/optional-mobile-title-header-switch)hammer_and_wrenchRepository Link [https://github.com/discourse/discourse-optional-title-mobile-header](https://github.com/discourse/discourse-optional-title-mobile-header)open_bookNew to Discourse Themes? [Beginner’s guide to using Discourse Themes](https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966) Install this theme component Features When disabled per user prefs → interface: …

你可以查看此组件的代码，并使用 CSS 来隐藏用户偏好设置中的元素（目标为 `.similar-topics`）。这正是我曾在 [https://unicyclist.com/](https://unicyclist.com/) 上的做法，用户可以在其中设置隐藏标题右上角的捐赠图标。

![image](https://global.discourse-cdn.com/meta/original/3X/a/d/adf382a5b7d7b8ea35506d44f6b2dc15a46a8738.png)

![image](https://global.discourse-cdn.com/meta/original/3X/1/7/172f9356c6b17d3d34f52f11ba9b2aec9d922e40.png)

* * *

基本上，我对原始插件所做的唯一更改是，我将设置存储在 cookie 中（我想每年提醒我的用户支付服务器费用）。

这是我自己的主题组件代码：

```js
    // 设置 cookie 有效期为 1 年
    function setHideDonationCookie(value=null) {
        var now = new Date();
        var time = now.getTime();
        var expireTime = time + 1000*60*60*24*365;
        now.setTime(expireTime);
        document.cookie = 'donationButton=' + value + ';expires='+now.toUTCString()+';path=/';
    }
    // 返回 cookie 值
    function getCookie(name) {
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
        }
        else
        {
            begin += 2;
            var end = document.cookie.indexOf(";", begin);
            if (end == -1) {
            end = dc.length;
            }
        }
        return decodeURI(dc.substring(begin + prefix.length, end));
    } 

function getToggleDonationIcon() {
  let pref = getCookie('donationButton');

  let result = settings.default_enabled;
  if (pref !== null) {
    result = pref === "true";
  }
  return result;
}

// 我们将 CSS 规则添加到 HTML 代码中
if (getToggleDonationIcon()) {
  let style = document.createElement('style');
  style.innerHTML = ".header-icon-help-fund-servers-fees{ display: none; }";
  document.head.appendChild(style);
}

// 技术上我们只想修改当前用户
api.modifyClass("model:user", {
  toggleDonationIcon: function() {
    return getToggleDonationIcon();
  }.property()
});

api.modifyClass("controller:preferences/interface", {
  actions: {
    save() {
      this._super();
      // 将 cookie 值设置为设置值
      if(this.get("model.toggleDonationIcon") != getToggleDonationIcon()) {
        setHideDonationCookie(this.get("model.toggleDonationIcon").toString());
        if(this.get("model.toggleDonationIcon") == false) {
            document.getElementsByClassName("header-icon-help-fund-servers-fees")[0].style.display = "";
        }
        else {
            document.getElementsByClassName("header-icon-help-fund-servers-fees")[0].style.display = "none";
        }
      }
    }
  }
});

    // 在用户偏好设置界面添加选择器
  {{preference-checkbox labelKey=(theme-prefix 'donation_icon_toggle') checked=model.toggleDonationIcon}} 

```

---

_[View the full topic](https://meta.discourse.org/t/some-users-complaining-about-topic-similar-to-window/216505)._
