Notion Netlify Blog 构建脚本

一键将 Notion 的 Page 通过 Netlify 构建

当前为 2020-05-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Notion Netlify Blog 构建脚本
  3. // @namespace notion_blog
  4. // @version 0.1
  5. // @description 一键将 Notion 的 Page 通过 Netlify 构建
  6. // @author superman66
  7. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js
  8. // @grant GM_xmlhttpRequest
  9. // @match https://www.notion.so/CUSTOM_KEY/*
  10. // ==/UserScript==
  11.  
  12. /****************** 使用前请按照下面的要求配置 ******************/
  13. /**
  14. * 1. @match 字段。CUSTOM_KEY 更换为你的。
  15. * 2. BUILD_HOOK_URL。将HOOK_KEY 更改为实际的KEY。具体配置见 https://docs.netlify.com/configure-builds/build-hooks/
  16. */
  17. /****************** 配置结束 ******************/
  18.  
  19. (function () {
  20. "use strict";
  21. const BUILD_HOOK_URL =
  22. "https://api.netlify.com/build_hooks/HOOK_KEY";
  23.  
  24. // Your code here...
  25. function triggerDeploy() {
  26. GM_xmlhttpRequest({
  27. method: "POST",
  28. url: BUILD_HOOK_URL,
  29. onload: function (responseDetail) {
  30. if (responseDetail.status === 200) {
  31. notifyMe();
  32. }
  33. },
  34. });
  35. }
  36.  
  37. function notifyMe() {
  38. // 先检查浏览器是否支持
  39. if (!("Notification" in window)) {
  40. alert("This browser does not support desktop notification");
  41. }
  42.  
  43. // 检查用户是否同意接受通知
  44. else if (Notification.permission === "granted") {
  45. // If it's okay let's create a notification
  46. var notification = new Notification("Netlify Trigger Deploy Successful.");
  47. }
  48.  
  49. // 否则我们需要向用户获取权限
  50. else if (Notification.permission !== "denied") {
  51. Notification.requestPermission(function (permission) {
  52. // 如果用户同意,就可以向他们发送通知
  53. if (permission === "granted") {
  54. var notification = new Notification(
  55. "Netlify Trigger Deploy Successful."
  56. );
  57. }
  58. });
  59. }
  60. }
  61.  
  62. function createNetlifyButton() {
  63. var button = document.createElement("div");
  64. button.setAttribute("role", "button");
  65. button.setAttribute("id", "deploy");
  66. button.setAttribute(
  67. "style",
  68. "position: fixed;top: 9px;right: 260px;z-index: 888;user-select: none;transition: background 120ms ease-in 0s;cursor: pointer;display: inline-flex;align-items: center;flex-shrink: 0;white-space: nowrap;height: 28px;border-radius: 3px;font-size: 14px;line-height: 1.2;min-width: 0px;padding-left: 8px;padding-right: 8px;color: rgb(55, 53, 47);"
  69. );
  70. button.innerHTML = "Deploy to Netlify";
  71. document.body.append(button);
  72. }
  73.  
  74. $(document).ready(function () {
  75. createNetlifyButton();
  76.  
  77. $("#deploy").on("click", function () {
  78. triggerDeploy();
  79. });
  80. });
  81. })();