{"id":13484605,"url":"https://github.com/SukkaW/DisqusJS","last_synced_at":"2025-03-27T16:30:59.895Z","repository":{"id":33108622,"uuid":"151406324","full_name":"SukkaW/DisqusJS","owner":"SukkaW","description":":speech_balloon: Render Disqus comments in Mainland China using Disqus API","archived":false,"fork":false,"pushed_at":"2024-12-15T15:12:43.000Z","size":568,"stargazers_count":658,"open_issues_count":12,"forks_count":55,"subscribers_count":9,"default_branch":"next","last_synced_at":"2025-03-19T22:18:44.236Z","etag":null,"topics":["disqus","disqus-api","disqusjs","disqusjs-disqus","hexo","hugo","javascript","react","react-component"],"latest_commit_sha":null,"homepage":"https://disqusjs.skk.moe","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SukkaW.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-03T12:06:06.000Z","updated_at":"2025-03-12T14:36:17.000Z","dependencies_parsed_at":"2023-11-16T02:02:32.303Z","dependency_job_id":"0d67f277-10cc-454a-bb10-75750bc54833","html_url":"https://github.com/SukkaW/DisqusJS","commit_stats":{"total_commits":68,"total_committers":4,"mean_commits":17.0,"dds":0.05882352941176472,"last_synced_commit":"8bd69c195fff5f067c31aeebc761e8fd8ac85e49"},"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SukkaW%2FDisqusJS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SukkaW%2FDisqusJS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SukkaW%2FDisqusJS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SukkaW%2FDisqusJS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SukkaW","download_url":"https://codeload.github.com/SukkaW/DisqusJS/tar.gz/refs/heads/next","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245615004,"owners_count":20644376,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["disqus","disqus-api","disqusjs","disqusjs-disqus","hexo","hugo","javascript","react","react-component"],"created_at":"2024-07-31T17:01:26.995Z","updated_at":"2025-03-27T16:30:59.445Z","avatar_url":"https://github.com/SukkaW.png","language":"TypeScript","readme":"# DisqusJS\n\nhttps://disqusjs.skk.moe\n\n\u003e 纯前端、超轻量级的「评论基础模式」实现：使用 Disqus API 渲染评论列表\n\n[![npm version](https://img.shields.io/npm/v/disqusjs.svg?style=flat-square)](https://www.npmjs.com/package/disqusjs) [![Author](https://img.shields.io/badge/Author-Sukka-b68469.svg?style=flat-square)](https://skk.moe) [![npm license](https://img.shields.io/npm/l/disqusjs.svg?style=flat-square)](https://github.com/SukkaW/DisqusJS/blob/master/LICENSE) [![jsDelivr Hits](https://data.jsdelivr.com/v1/package/npm/disqusjs/badge)](https://www.jsdelivr.com/package/npm/disqusjs)\n\n## 简介\n\nDisqusJS 是一个基于 Disqus API 和 React 开发的 Embed 插件。DisqusJS 通过 Disqus API 渲染只读的评论列表，搭配反向代理可以实现在网络审查地区加载 Disqus 评论列表；支持自动检测访客是否能够访问 Disqus、并自动选择加载原生 Disqus（评论完整模式）或 DisqusJS 提供的评论基础模式。\n\n## 功能\n\n- 判断访客能否访问 Disqus、自动选择「评论基础模式」或「Disqus 完整模式」\n- 展示评论列表、支持按照「最新」、「最早」、「最佳」排序\n- 可搭配 React（Gatsby、Next.js）使用\n\n## Demo\n\n- https://disqusjs.skk.moe\n- https://blog.skk.moe\n\n## 安装和使用\n\n### 在 HTML 中直接引入\n\n在你需要安装 DisqusJS 的页面的 `\u003c/head\u003e` 之前引入 DisqusJS 的 CSS：\n\n```html\n\u003clink rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/disqusjs@3.0/dist/browser/styles/disqusjs.css\"\u003e\n```\n\n在需要展示评论的地方插入 JS：\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/disqusjs@3.0/dist/browser/disqusjs.es2015.umd.min.js\"\u003e\u003c/script\u003e\n\n\u003c!-- 如果你只兼容现代浏览器，你也可以使用 ES Module --\u003e\n\u003cscript type=\"module\"\u003e\n  import DisqusJS from 'https://cdn.jsdelivr.net/npm/disqusjs@3.0/dist/browser/disqusjs.es2018.es.min.mjs'\n\u003c/script\u003e\n```\n\n接着创建一个 DisqusJS 容器：\n\n```html\n\u003cdiv id=\"disqusjs\"\u003e\u003c/div\u003e\n```\n\n### 通过 NPM 安装\n\n```sh\nnpm i disqusjs\n# Yarn\n# yarn add disqusjs\n# pnpm\n# pnpm add disqusjs\n```\n\n然后在项目中引入 DisqusJS：\n\n```js\n// CommonJS\nconst DisqusJS = require('disqusjs/dist/disqusjs.es2015.umd');\n// const DisqusJS = require('disqusjs/dist/disqusjs.es2017.umd');\n// const DisqusJS = require('disqusjs/dist/disqusjs.es2022.umd');\n\n// ES Module\nimport DisqusJS from 'disqusjs/dist/disqusjs.es2015.es.js';\n// import DisqusJS from 'disqusjs/dist/disqusjs.es2017.es.js';\n// import DisqusJS from 'disqusjs/dist/disqusjs.es2022.es.js';\n```\n\n注意，你仍然需要手动引入 DisqusJS 的 CSS：\n\n```js\nimport 'disqusjs/dist/styles/disqusjs.css';\n```\n\n---\n\n使用下述代码初始化一个 DisqusJS 实例。注意初始化需在 DisqusJS 加载完成后进行：\n\n```js\nconst disqusjs = new DisqusJS({\n    shortname: '',\n    siteName: '',\n    identifier: '',\n    url: '',\n    title: '',\n    api: '',\n    apikey: '',\n    admin: '',\n    adminLabel: ''\n});\n```\n\n接下来，我们需要让 DisqusJS 实例将评论组件渲染到页面上：\n\n```js\ndisqusjs.render(document.getElementById('disqusjs'));\n// 你也可以传入一个 CSS 选择器\n// disqusjs.render('#disqusjs');\n```\n\n### 作为 React 组件使用\n\n```jsx\nimport 'disqusjs/dist/react/styles/disqusjs.css';\nimport { DisqusJS } from 'disqusjs/dist/react/disqusjs.es2015.es.js';\n// import { DisqusJS } from 'disqusjs/dist/react/disqusjs.es2017.es.js';\n// import { DisqusJS } from 'disqusjs/dist/react/disqusjs.es2022.es.js';\n// const { DisqusJS } = require('disqusjs/dist/react/disqusjs.es2015.umd');\n// const { DisqusJS } = require('disqusjs/dist/react/disqusjs.es2017.umd');\n// const { DisqusJS } = require('disqusjs/dist/react/disqusjs.es2022.umd');\n\n\u003cDisqusJS\n  shortname=\"\"\n  siteName=\"\"\n  identifier=\"\"\n  url=\"\"\n  api=\"\"\n  apikey=\"\"\n  admin=\"\"\n  adminLabel=\"\"\n/\u003e\n```\n\n---\n\n完成上述步骤后，DisqusJS 就已经在您的站点安装好了，但是你现在还不能使用它。要使用 DisqusJS，你还需要进行一些配置。\n\n### 配置 Disqus Application\n\n在 [Disqus API Application](https://disqus.com/api/applications/) 处注册一个 Application。\n\n![](https://i.loli.net/2018/10/08/5bbb71f99369d.png)\n\n点击新创建的 Application，获取你的 API Key（公钥）。\n\n![](https://i.loli.net/2018/10/08/5bbb723acbe07.png)\n\n在 Application 的 Settings 页面设置你使用 DisqusJS 时的域名。Disqus API 会检查 API 请求的 Referrer 和 Origin。\n\n![](https://i.loli.net/2018/10/08/5bbb735b9d2a4.png)\n\n### 配置 DisqusJS 参数\n\n**shortname** `{string}`\n\n- 你的 Disqus Forum 的 shortname，你可以在 [Disqus Admin - Settings - General - Shortname](https://disqus.com/admin/settings/general/) 获取你的 shortname\n- **必须**，无默认值\n\n**siteName** `{string}`\n\n- 你站点的名称，将会显示在「评论基础模式」的 header 中；该配置应该和 [Disqus Admin - Settings - General - Website Name](https://disqus.com/admin/settings/general/) 一致\n- 非必须，无默认值\n\n**identifier** `{string}`\n\n- 当前页面的 identifier，用来区分不同页面\n- **建议**，默认值为 `document.location.origin + document.location.pathname + document.location.search`\n\n**url** `{string}`\n\n- 当前页面的 URL，Disqus 的爬虫会爬取该 URL 获取页面相关信息\n- **建议**，默认值为 `document.location.origin + document.location.pathname + document.location.search`\n\n**title** `{string}`\n\n- 当前页面的标题，如果没有设置默认为当前页面的标题。当页面标题中有其他信息（比如站点名称）而不想在 Disqus 中展示时，可以设置此项。\n- 非必须，默认值为 `document.title`\n\n**api** `{string}`\n\n- DisqusJS 请求的 API Endpoint，通常情况下你应该配置一个 Disqus API 的反代并填入反代的地址。你也可以直接使用 DISQUS 官方 API 的 Endpoint `https://disqus.com/api/`，或是使用我搭建的 Disqus API 反代 Endpoint `https://disqus.skk.moe/disqus/`。如有必要可以阅读关于搭建反代的 [相关内容](https://github.com/SukkaW/DisqusJS#%E8%B0%83%E8%AF%95%E8%BF%9B%E9%98%B6%E4%BD%BF%E7%94%A8--%E5%BC%80%E5%8F%91%E7%9B%B8%E5%85%B3)\n- **建议**，默认值为 `https://disqus.skk.moe/disqus/`\n\n**apikey** `{string | string[]}`\n\n- DisqusJS 向 API 发起请求时使用的 API Key，你应该在配置 Disqus Application 时获取了 API Key\n- DisqusJS 支持填入一个 包含多个 API Key 的数组，每次请求时会随机使用其中一个；如果你只填入一个 API Key，可以填入 string 或 Array。\n- **必填**，无默认值\n\n**nesting** `{number}`\n\n- 最大评论嵌套数；超过嵌套层数的评论，会不论从属关系显示在同一层级下\n- 非必须，默认值为 `4`\n\n**nocomment** `{string}`\n\n- 没有评论时的提示语（对应 Disqus Admin - Settings - Community - Comment Count Link - Zero comments）\n- 非必须，默认值为 `这里冷冷清清的，一条评论都没有`\n\n---\n\n以下配置和 Disqus Moderator Badge 相关，缺少一个都不会显示 Badge\n\n**admin** `{string}`\n\n- 你的站点的 Disqus Moderator 的用户名（也就是你的用户名）。你可以在 [Disqus - Settings - Account - Username](https://disqus.com/home/settings/account/) 获取你的 Username\n- 非必须，无默认值\n\n**adminLabel** `{string}`\n\n- 你想显示在 Disqus Moderator Badge 中的文字。该配置应和 [Disqus Admin - Settings - Community - Moderator Badge Text](https://disqus.com/admin/settings/community/) 相同\n- 非必须，无默认值\n\n### SPA 与 PJAX 站点注意事项\n\n如果你在 React SPA、Next.js、Gatsby 中以 React 组件的形式使用 DisqusJS，无需任何额外步骤，只需修改 `\u003cDisqusJS /\u003e` 组件的 prop，DisqusJS 会自动更新。\n\n如果你在 PJAX 站点中使用，需要在页面 unload 之前手动销毁 DisqusJS 实例，并新页面 load 后重新渲染一个 DisqusJS 实例：\n\n```js\nlet disqusjs = null;\n// 初始化 DisqusJS 实例\ndisqusjs = new DisqusJS({\n  // ...\n});\n// 将 DisqusJS 渲染到页面上\ndisqusjs.render(document.getElementById('disqusjs'));\n\ndocument.addEventListener('pjax:send', () =\u003e {\n  // 销毁 DisqusJS 实例\n  disqusjs.destroy();\n});\n\ndocument.addEventListener('pjax:complete', () =\u003e {\n  // 使用新的参数（如新的 identifier 和 url）创建全新的 DisqusJS 实例\n  disqusjs = new DisqusJS({\n    // ...\n  });\n  // 渲染新的 DisqusJS\n  disqusjs.render(document.getElementById('disqusjs'));\n});\n```\n\n## 如何搭建 Disqus API 反代\n\n使用 Caddy 或者 Nginx 都可以搭建一个反代服务器，需要反代的 Endpoint 是 `https://disqus.com/api/`。这里介绍的是针对不使用服务器和后端程序，使用 Serverless 平台搭建 Disqus API 反代的方法。\n\n### Vercel\n\n[Vercel](https://vercel.com) 是一个 Serverless 平台。免费的 Hobby Plan 提供每月 100 GiB 流量和无限的请求次数。\n[sukkaw/disqusjs-proxy-example](https://github.com/SukkaW/disqusjs-proxy-example) 提供了一个使用 Now Router 进行反代的样例配置文件。\n\n### Cloudflare Workers\n\n[Cloudflare Workers](https://www.cloudflare.com/products/cloudflare-workers/) 提供了一个在 Cloudflare 上运行 JavaScript 的平台。免费 Plan 可提供每天 `100000` 次免费请求次数额度。\n[idawnlight/disqusjs-proxy-cloudflare-workers](https://github.com/idawnlight/disqusjs-proxy-cloudflare-workers) 提供了一份使用 Cloudflare Workers 进行反代的样例代码。\n\n### Firebase\n\n[Firebase Cloud Functions](https://firebase.google.com/products/functions/) 提供了执行 Node.js 代码的 Serverless 平台。需绑定银行卡 (Visa 或 MasterCard) 才能启用互联网出站访问功能。\n[ysc3839/disqusjs-proxy 的 `firebase` 分支](https://github.com/ysc3839/disqusjs-proxy/tree/firebase) 提供了一个可以部署在 Firebase 上的反代样例。\n\n## 注意事项\n\n- Disqus API 不支持通过 AJAX 方式调用创建评论或者初始化页面，所以自动初始化页面和创建匿名评论在不搭配专门的后端程序的话不能实现。\n  - Disqus API 会检查请求是否包含 `Origin`、`X-Request-With` 等响应头、拦截 AJAX 请求。就算 Disqus API 不做检查，把你的私钥和公钥一起明文写在前端也是 **十分愚蠢** 的\n- 所以如果 DisqusJS 检测到当前页面没有初始化、会提示是否切换到 Disqus 完整模式进行初始化。\n- DisqusJS 仅在当前域名首次访问时检测 Disqus 可用性并选择模式，并把结果持久化在 localStorage 中，之后访问都会沿用之前的模式。\n- 一个 Disqus Application 的 Rate Limit 是每小时 1000 次；DisqusJS 一次正常加载会产生 2 次请求。DisqusJS 支持将多个 API Key 填入一个 Array 中，并在请求时随机选择（负载均衡）。你可以创建多个 Disqus API Application 并分别获取 API Key。\n\n## 从 DisqusJS 1.3.0 升级到 DisqusJS 3.0.0\n\n```html\n\u003c!-- 替换 DisqusJS 版本 --\u003e\n\u003c!--\u003clink rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/disqusjs@1.3/dist/disqusjs.css\"\u003e--\u003e\n\u003c!--\u003cscript src=\"https://cdn.jsdelivr.net/npm/disqusjs@1.3/dist/disqus.js\"\u003e\u003c/script\u003e--\u003e\n\u003clink rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/disqusjs@3.0/dist/browser/styles/disqusjs.css\"\u003e\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/disqusjs@3.0/dist/browser/disqusjs.es2015.umd.min.js\"\u003e\u003c/script\u003e\n\n\u003c!--\n  DisqusJS 1.3.0 容器的 id 属性必须是 `disqus_thread`，\n  DisqusJS 3.0.0 容器的 id 属性 **必须不是** `disqus_thread`。\n  建议使用 \"disqusjs\" 作为 DisqusJS 容器的 id 属性。\n--\u003e\n\u003c!--\u003cdiv id=\"disqus_thread\"\u003e\u003c/div\u003e--\u003e\n\u003cdiv id=\"disqusjs\"\u003e\u003c/div\u003e\n\u003cscript\u003e\n  const disqusjs = new DisqusJS({\n    // DisqusJS 1.3.0 和 DisqusJS 3.0.0 配置完全兼容，无需更改\n    // ...\n  });\n\n  // DisqusJS 1.3.0 在初始化实例后评论列表已经开始渲染到页面上，DisqusJS 3.0.0 还需要额外调用 render() 方法：\n  disqusjs.render(document.getElementById('disqusjs')); // render() 方法需要传入 DisqusJS 的容器\n\n  // DisqusJS 3.0.0 新增了销毁实例的 destroy() 方法，你可以在 PJAX 跳转时直接调用它：\n  disqusjs.destroy();\n  // 关于 PJAX 站点使用，请参考前文「SPA 与 PJAX 站点注意事项」\n\u003c/script\u003e\n```\n\n## 谁在使用 DisqusJS？\n\n- Sukka: [Sukka's Blog](https://blog.skk.moe)\n- Yrom Wang: [Yrom's](https://yrom.net/) (Still using DisqusJS v0.2.5)\n- chawyehsu: [The Art of Chawye Hsu](https://chawyehsu.com) ([source](https://github.com/chawyehsu/chawyehsu.com))\n- ysc3839: [YSC's blog](https://blog.ysc3839.com/)\n- JiPai: [JiPai Store](https://blog.jipai.moe/)\n- GodU: [U](https://godu.ink/)\n- bmyjacks: [Hey Bro, Wassup?](https://www.dreamsafari.info/)\n- 若风: [青菜芋子](https://loafing.cn/)\n- Ward Chen:[WardChan 的小站](https://blog.wardchan.com/)\n- jubyshu:[渚碧](https://jubeny.com/)\n- V2beach:[V2beach's Blog](https://blog.v2beach.cn/)\n\n\n如果你的站点或者个人博客在使用 DisqusJS，来 [把你的网站分享给其他人吧](https://github.com/SukkaW/DisqusJS/issues/12)！\n\n## Author 作者\n\n**DisqusJS** © [Sukka](https://github.com/SukkaW), Released under the [MIT](https://github.com/SukkaW/DisqusJS/blob/master/LICENSE) License.\u003cbr\u003e\nAuthored and maintained by Sukka with help from contributors ([list](https://github.com/SukkaW/DisqusJS/graphs/contributors)).\n\n\u003e [Personal Website](https://skk.moe) · [Blog](https://blog.skk.moe) · GitHub [@SukkaW](https://github.com/SukkaW) · Telegram Channel [@SukkaChannel](https://t.me/SukkaChannel) · Mastodon [@sukka@acg.mn](https://acg.mn/@sukka) · Twitter [@isukkaw](https://twitter.com/isukkaw) · Keybase [@sukka](https://keybase.io/sukka)\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/sponsors/SukkaW/\"\u003e\n    \u003cimg src=\"https://sponsor.cdn.skk.moe/sponsors.svg\"/\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n","funding_links":["https://github.com/sponsors/SukkaW/"],"categories":["TypeScript","JavaScript","JavaScript (485)"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSukkaW%2FDisqusJS","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSukkaW%2FDisqusJS","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSukkaW%2FDisqusJS/lists"}