Guest User

Widget

a guest
Sep 29th, 2021
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const namespace = "plugins.run"
  2. const displayName = "Plugins.run"
  3. const accentColor = new Color("#F1F5F9");
  4.  
  5. // LineChart by https://kevinkub.de/
  6. // Used as the widget background
  7. class LineChart {
  8.   constructor(width, height, values) {
  9.     this.ctx = new DrawContext();
  10.     this.ctx.size = new Size(width, height);
  11.     this.values = values;
  12.   }
  13.  
  14.   _calculatePath() {
  15.     let maxValue = Math.max(...this.values);
  16.     let minValue = Math.min(...this.values);
  17.     let difference = maxValue - minValue;
  18.     let count = this.values.length;
  19.     let step = this.ctx.size.width / (count - 1);
  20.     let points = this.values.map((current, index, all) => {
  21.         let x = step*index;
  22.         let y = this.ctx.size.height - (current - minValue) / difference * this.ctx.size.height;
  23.         return new Point(x, y);
  24.     });
  25.     return this._getSmoothPath(points);
  26.   }
  27.      
  28.   _getSmoothPath(points) {
  29.     let path = new Path();
  30.     path.move(new Point(0, this.ctx.size.height));
  31.     path.addLine(points[0]);
  32.     for(let i = 0; i < points.length-1; i++) {
  33.       let xAvg = (points[i].x + points[i+1].x) / 2;
  34.       let yAvg = (points[i].y + points[i+1].y) / 2;
  35.       let avg = new Point(xAvg, yAvg);
  36.       let cp1 = new Point((xAvg + points[i].x) / 2, points[i].y);
  37.       let next = new Point(points[i+1].x, points[i+1].y);
  38.       let cp2 = new Point((xAvg + points[i+1].x) / 2, points[i+1].y);
  39.       path.addQuadCurve(avg, cp1);
  40.       path.addQuadCurve(next, cp2);
  41.     }
  42.     path.addLine(new Point(this.ctx.size.width, this.ctx.size.height));
  43.     path.closeSubpath();
  44.     return path;
  45.   }
  46.  
  47.   configure(fn) {
  48.     let path = this._calculatePath();
  49.     if(fn) {
  50.       fn(this.ctx, path);
  51.     } else {
  52.       this.ctx.addPath(path);
  53.       this.ctx.fillPath(path);
  54.     }
  55.     return this.ctx;
  56.   }
  57.  
  58. }
  59.  
  60. // Requests
  61.  
  62. function plausibleEndpoint(key) {
  63.   return "https://plausible.io/api/stats/" + namespace + key
  64. }
  65.  
  66. // Today
  67. const today = new Date()
  68. const fmt = new DateFormatter()
  69. fmt.dateFormat = "yyyy-MM-dd"
  70. const formatted_date = fmt.string(today)
  71. const req = new Request(plausibleEndpoint("/main-graph?period=day&date=" + formatted_date + "&filters=%7B%7D"))
  72. const graph = await req.loadJSON()
  73. const todayVisitors = graph["top_stats"][1]["value"]
  74. const chartData = graph["plot"]
  75.  
  76. // Live visits
  77. const liveRequest = new Request(plausibleEndpoint("/current-visitors"))
  78. const liveVisitors = await liveRequest.loadString()
  79.  
  80. // Format visitors above 1k
  81.  
  82. const formattedTodayVisitors = kFormatter(todayVisitors);
  83. const formattedLiveVisitors = kFormatter(liveVisitors);
  84.  
  85. // Create widget
  86. let w = new ListWidget()
  87. w.backgroundColor = new Color("#12141D");
  88.  
  89. // Use iA Writer Quattro font
  90. // Download: https://github.com/iaolo/iA-Fonts
  91. const iA = new Font("iA Writer Duo S Regular",20);
  92. const iABold = new Font("iA Writer Duo S Bold",24);
  93.  
  94. // Show domain
  95. t1 = w.addText(displayName)
  96. t1.textColor = accentColor;
  97. t1.font = iABold;
  98. t1.minimumScaleFactor = 0.1;
  99. t1.lineLimit = 1;
  100.  
  101. const spacer = w.addSpacer(null);
  102.  
  103.  
  104. // Show live visitors
  105. const copy1 = formattedLiveVisitors + " now"
  106.  
  107. t2 = w.addText(copy1)
  108. t2.textColor = Color.white();
  109. t2.font = iA;
  110. t2.minimumScaleFactor = 0.1;
  111. t2.lineLimit = 1;
  112.  
  113. // Show today visitors
  114. const copy2 = formattedTodayVisitors + " today"
  115.  
  116. t3 = w.addText(copy2);
  117. t3.textColor = Color.gray();
  118. t3.font = iA;
  119. t3.minimumScaleFactor = 0.1;
  120. t3.lineLimit = 1;
  121.  
  122. // Line chart
  123. let chart = new LineChart(400, 200, chartData).configure((ctx, path) => {
  124.   ctx.opaque = false;
  125.   ctx.setFillColor(new Color("FFFFFF", .1));
  126.   ctx.addPath(path);
  127.   ctx.fillPath(path);
  128. }).getImage();
  129. w.backgroundImage = chart
  130.  
  131. if (config.runsInWidget) {
  132.   Script.setWidget(w)
  133. } else {
  134.   w.presentMedium()
  135. }
  136. Script.complete()
  137.  
  138. function kFormatter(num) {  
  139.   if (num > 999) {
  140.     return (num / 1000).toFixed(1) + "k"
  141.   } else {
  142.     return num
  143.  }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment