PageSourceSearch

https://www.pangao.vip/js/leancloud.js

js pangao.vip collected 2026-09-28 06:50:15 UTC 5,768 bytes, 192 lines download raw bytes

1/* global CONFIG */
2// eslint-disable-next-line no-console
3
4(function(window, document) {
5  // 查询存储的记录
6  function getRecord(Counter, target) {
7    return new Promise(function(resolve, reject) {
8      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ target })))
9        .then(resp => resp.json())
10        .then(({ results, code, error }) => {
11          if (code === 401) {
12            throw error;
13          }
14          if (results && results.length > 0) {
15            var record = results[0];
16            resolve(record);
17          } else {
18            Counter('post', '/classes/Counter', { target, time: 0 })
19              .then(resp => resp.json())
20              .then((record, error) => {
21                if (error) {
22                  throw error;
23                }
24                resolve(record);
25              }).catch(error => {
26                console.error('Failed to create: ', error);
27                reject(error);
28              });
29          }
30        }).catch((error) => {
31          console.error('LeanCloud Counter Error: ', error);
32          reject(error);
33        });
34    });
35  }
36
37  // 发起自增请求
38  function increment(Counter, incrArr) {
39    return new Promise(function(resolve, reject) {
40      Counter('post', '/batch', {
41        'requests': incrArr
42      }).then((res) => {
43        res = res.json();
44        if (res.error) {
45          throw res.error;
46        }
47        resolve(res);
48      }).catch((error) => {
49        console.error('Failed to save visitor count: ', error);
50        reject(error);
51      });
52    });
53  }
54
55  // 构建自增请求体
56  function buildIncrement(objectId) {
57    return {
58      'method': 'PUT',
59      'path'  : `/1.1/classes/Counter/${objectId}`,
60      'body'  : {
61        'time': {
62          '__op'  : 'Increment',
63          'amount': 1
64        }
65      }
66    };
67  }
68
69  // 校验是否为有效的 Host
70  function validHost() {
71    if (CONFIG.web_analytics.leancloud.ignore_local) {
72      var hostname = window.location.hostname;
73      if (hostname === 'localhost' || hostname === '127.0.0.1') {
74        return false;
75      }
76    }
77    return true;
78  }
79
80  // 校验是否为有效的 UV
81  function validUV() {
82    var key = 'LeanCloud_UV_Flag';
83    var flag = localStorage.getItem(key);
84    if (flag) {
85      // 距离标记小于 24 小时则不计为 UV
86      if (new Date().getTime() - parseInt(flag, 10) <= 86400000) {
87        return false;
88      }
89    }
90    localStorage.setItem(key, new Date().getTime().toString());
91    return true;
92  }
93
94  function addCount(Counter) {
95    var enableIncr = CONFIG.web_analytics.enable && !Fluid.ctx.dnt && validHost();
96    var getterArr = [];
97    var incrArr = [];
98
99    // 请求 PV 并自增
100    var pvCtn = document.querySelector('#leancloud-site-pv-container');
101    if (pvCtn) {
102      var pvGetter = getRecord(Counter, 'site-pv').then((record) => {
103        enableIncr && incrArr.push(buildIncrement(record.objectId));
104        var ele = document.querySelector('#leancloud-site-pv');
105        if (ele) {
106          ele.innerText = (record.time || 0) + (enableIncr ? 1 : 0);
107          pvCtn.style.display = 'inline';
108        }
109      });
110      getterArr.push(pvGetter);
111    }
112
113    // 请求 UV 并自增
114    var uvCtn = document.querySelector('#leancloud-site-uv-container');
115    if (uvCtn) {
116      var uvGetter = getRecord(Counter, 'site-uv').then((record) => {
117        var incrUV = validUV() && enableIncr;
118        incrUV && incrArr.push(buildIncrement(record.objectId));
119        var ele = document.querySelector('#leancloud-site-uv');
120        if (ele) {
121          ele.innerText = (record.time || 0) + (incrUV ? 1 : 0);
122          uvCtn.style.display = 'inline';
123        }
124      });
125      getterArr.push(uvGetter);
126    }
127
128    // 如果有页面浏览数节点,则请求浏览数并自增
129    var viewCtn = document.querySelector('#leancloud-page-views-container');
130    if (viewCtn) {
131      var path = eval(CONFIG.web_analytics.leancloud.path || 'window.location.pathname');
132      var target = decodeURI(path.replace(/\/*(index.html)?$/, '/'));
133      var viewGetter = getRecord(Counter, target).then((record) => {
134        enableIncr && incrArr.push(buildIncrement(record.objectId));
135        var ele = document.querySelector('#leancloud-page-views');
136        if (ele) {
137          ele.innerText = (record.time || 0) + (enableIncr ? 1 : 0);
138          viewCtn.style.display = 'inline';
139        }
140      });
141      getterArr.push(viewGetter);
142    }
143
144    // 如果启动计数自增,批量发起自增请求
145    if (enableIncr) {
146      Promise.all(getterArr).then(() => {
147        incrArr.length > 0 && increment(Counter, incrArr);
148      });
149    }
150  }
151
152  var appId = CONFIG.web_analytics.leancloud.app_id;
153  var appKey = CONFIG.web_analytics.leancloud.app_key;
154  var serverUrl = CONFIG.web_analytics.leancloud.server_url;
155
156  if (!appId) {
157    throw new Error('LeanCloud appId is empty');
158  }
159  if (!appKey) {
160    throw new Error('LeanCloud appKey is empty');
161  }
162
163  function fetchData(api_server) {
164    var Counter = (method, url, data) => {
165      return fetch(`${api_server}/1.1${url}`, {
166        method,
167        headers: {
168          'X-LC-Id'     : appId,
169          'X-LC-Key'    : appKey,
170          'Content-Type': 'application/json'
171        },
172        body: JSON.stringify(data)
173      });
174    };
175
176    addCount(Counter);
177  }
178
179  var apiServer = serverUrl || `https://${appId.slice(0, 8).toLowerCase()}.api.lncldglobal.com`;
180
181  if (apiServer) {
182    fetchData(apiServer);
183  } else {
184    fetch('https://app-router.leancloud.cn/2/route?appId=' + appId)
185      .then(resp => resp.json())
186      .then((data) => {
187        if (data.api_server) {
188          fetchData('https://' + data.api_server);
189        }
190      });
191  }
192})(window, document);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.