@5ime/likeme 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -0
- package/index.js +58 -0
- package/package.json +10 -0
- package/public/index.html +18 -0
- package/static/css/likeme.css +1 -0
- package/static/js/likeme.js +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# likeMe
|
|
2
|
+
|
|
3
|
+
> 🍰快速为你的网站添加 你喜欢我吗/Do you like me 小组件
|
|
4
|
+
|
|
5
|
+
## LeanCloud 设置
|
|
6
|
+
|
|
7
|
+
1. [登录](https://console.leancloud.app/login) 或 [注册](https://console.leancloud.app/register) `LeanCloud 国际版` 并进入 [控制台](https://console.leancloud.app/apps)
|
|
8
|
+
2. 点击左上角 [创建应用](https://console.leancloud.app/apps) 并起一个你喜欢的名字 (请选择免费的**开发版**):
|
|
9
|
+
3. 进入应用,选择左下角的 `设置` > `应用凭证`。你可以看到你的 `APP ID`,`APP Key` 和 `REST API 服务器地址`。请记录它们,以便后续使用。
|
|
10
|
+
4. 进入应用,选择左上角的 `数据存储` > `结构化数据` > `创建 Class`。`Class 名称` 填写为 `likeCount`,`Class 访问权限` 除 `update` 和 `find` 外均选择 `指定用户`,`默认 ACL 权限` 选定为 `限制读取`,点击 `创建` 。
|
|
11
|
+
5. 选择新建的 `Class` 然后 `添加行` -> `设置 ACL 权限` 均设置为 `所有用户` ,点击 `添加` ,你可以看到新建行的 `objectId,`。请记录它,以便后续使用。
|
|
12
|
+
|
|
13
|
+
此时你应该拥有了以下信息:`APP ID`,`APP Key`,`REST API 服务器地址` 和 `objectId`:tada:
|
|
14
|
+
|
|
15
|
+
## Vercel部署
|
|
16
|
+
|
|
17
|
+
[](https://vercel.com/new/clone?repository-url=https://github.com/5ime/likeme)
|
|
18
|
+
|
|
19
|
+
1. 点击上方按钮,跳转至 `Vercel` 进行部署。
|
|
20
|
+
|
|
21
|
+
2. 输入一个你喜欢的 `Vercel` 项目名称并点击 `Create` 继续:
|
|
22
|
+
|
|
23
|
+
3. 此时 `Vercel` 会基于 `likeMe` 模板帮助你新建并初始化仓库,仓库名为你之前输入的项目名。
|
|
24
|
+
|
|
25
|
+
一两分钟后,满屏的烟花会庆祝你部署成功。此时点击 `Go to Dashboard` 可以跳转到应用的控制台。
|
|
26
|
+
|
|
27
|
+
4. 点击顶部的 `Settings` - `Environment Variables` 进入环境变量配置页,并配置四个环境变量 `appId`, `appKey` , `serverURL` 和 `objectId` 。它们的值分别对应上一步在 `LeanCloud` 中获得的 `APP ID`, `APP KEY`,`REST API 服务器地址` 和 `objectId`。
|
|
28
|
+
|
|
29
|
+
1. 环境变量配置完成之后点击顶部的 `Deployments` 点击顶部最新的一次部署右侧的 `Redeploy` 按钮进行重新部署。该步骤是为了让刚才设置的环境变量生效。
|
|
30
|
+
|
|
31
|
+
1. 此时会跳转到 `Overview` 界面开始部署,等待片刻后 `STATUS` 会变成 `Ready`。此时请点击 `Visit` ,即可跳转到部署好的网站地址,此地址即为你的服务端地址。
|
|
32
|
+
|
|
33
|
+
## 绑定域名 (可选)
|
|
34
|
+
|
|
35
|
+
1. 点击顶部的 `Settings` - `Domains` 进入域名配置页
|
|
36
|
+
|
|
37
|
+
1. 输入需要绑定的域名并点击 `Add`
|
|
38
|
+
|
|
39
|
+
1. 在域名服务器商处添加新的 `CNAME` 解析记录
|
|
40
|
+
|
|
41
|
+
| Type | Name | Value |
|
|
42
|
+
| ----- | ------- | -------------------- |
|
|
43
|
+
| CNAME | example | cname.vercel-dns.com |
|
|
44
|
+
|
|
45
|
+
1. 等待生效,你可以通过自己的域名来访问了:tada:
|
|
46
|
+
|
|
47
|
+
## HTML 引入
|
|
48
|
+
|
|
49
|
+
在你的网页中进行如下设置:
|
|
50
|
+
|
|
51
|
+
1. 使用 `CDN` 引入 `likeMe`:
|
|
52
|
+
|
|
53
|
+
- `https://unpkg.com/ likeme@latest/static/css/likeme.css`。
|
|
54
|
+
- `https://unpkg.com/ likeme@latest/static/js/likeme.js`。
|
|
55
|
+
|
|
56
|
+
1. 创建 `<script>` 标签使用 `new limeMe()` 初始化,并传入必要的 `el` 与 `serverURL` 选项。
|
|
57
|
+
|
|
58
|
+
- `el` 选项是 `likeMe` 渲染使用的元素,你可以设置一个字符串形式的 `CSS 选择器` 或者一个 `HTMLElement` 对象。
|
|
59
|
+
- `serverURL` 是服务端的地址,即上一步获取到的值。
|
|
60
|
+
|
|
61
|
+
```html {3-7,12-18}:line-numbers
|
|
62
|
+
<head>
|
|
63
|
+
<!-- ... -->
|
|
64
|
+
<link rel="stylesheet" href='https://unpkg.com/likeme@latest/static/css/likeme.css'/>
|
|
65
|
+
<script src="https://unpkg.com/likeme@latest/static/js/likeme.js"></script>
|
|
66
|
+
<!-- ... -->
|
|
67
|
+
</head>
|
|
68
|
+
<body>
|
|
69
|
+
<!-- ... -->
|
|
70
|
+
<div id="likeme"></div>
|
|
71
|
+
<script>
|
|
72
|
+
new limeMe({
|
|
73
|
+
el: '#likeme',
|
|
74
|
+
serverURL: 'https://your-domain.vercel.app',
|
|
75
|
+
});
|
|
76
|
+
</script>
|
|
77
|
+
</body>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
2. 此时 `likeMe` 就会在你的网站上成功运行 :tada:
|
|
81
|
+
|
package/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const express = require('express')
|
|
2
|
+
const AV = require('leancloud-storage');
|
|
3
|
+
const { Query, User } = AV;
|
|
4
|
+
const app = express()
|
|
5
|
+
const port = 80
|
|
6
|
+
|
|
7
|
+
AV.init({
|
|
8
|
+
appId: process.env.appId,
|
|
9
|
+
appKey: process.env.appKey,
|
|
10
|
+
serverURL: process.env.serverURL
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const query = new AV.Query('likeCount');
|
|
14
|
+
const account = AV.Object.createWithoutData('likeCount', process.env.objectId);
|
|
15
|
+
|
|
16
|
+
app.use(function (req, res, next) {
|
|
17
|
+
res.header('Access-Control-Allow-Origin', '*');
|
|
18
|
+
if (req.method === 'OPTIONS') {
|
|
19
|
+
res.sendStatus(200);
|
|
20
|
+
} else {
|
|
21
|
+
next();
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
app.use('/static', express.static(__dirname + '/static'));
|
|
26
|
+
|
|
27
|
+
app.get('/', function getState(req,res){
|
|
28
|
+
res.setHeader('Content-Type', 'text/html');
|
|
29
|
+
res.sendFile(__dirname + '/public/index.html');
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
app.get('/info', (req,res) => {
|
|
33
|
+
query.find().then(function (results) {
|
|
34
|
+
const data = {code: '200', msg: 'success', data: {count: results[0].attributes.count}}
|
|
35
|
+
res.send(data);
|
|
36
|
+
}
|
|
37
|
+
).catch(function (error) {
|
|
38
|
+
const data = {code: '201', msg: 'error'}
|
|
39
|
+
res.send(data);
|
|
40
|
+
});
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
app.get('/like', (req, res) => {
|
|
44
|
+
account.increment('count', +1);
|
|
45
|
+
account.save(null, {
|
|
46
|
+
query: new AV.Query('likeCount').greaterThanOrEqualTo('count', +1),fetchWhenSave: true
|
|
47
|
+
}).then((account) => {
|
|
48
|
+
const data = {code: '200', msg: 'success', data: {count: account.attributes.count}}
|
|
49
|
+
res.send(data);
|
|
50
|
+
}, (error) => {
|
|
51
|
+
if (error.code === 305) {
|
|
52
|
+
const data = {code: '201', msg: 'error'}
|
|
53
|
+
res.send(data);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
app.listen(port, () => {})
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
+
<title>Do You Like Me ?</title>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<link rel="stylesheet" href='https://unpkg.com/likeme@latest/static/css/likeme.css'/>
|
|
11
|
+
<div class="likeCard">
|
|
12
|
+
<div class="likeCard-text">
|
|
13
|
+
少女祈祷中...
|
|
14
|
+
</div>
|
|
15
|
+
<h3>Do You Like Me</h3>
|
|
16
|
+
</div>
|
|
17
|
+
</body>
|
|
18
|
+
</html>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.likeCard{width:auto;user-select:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;cursor:pointer;border-radius:5px;margin:0 auto;padding:20px;background:#ff9797;transition:opacity .5s;color:#fff;text-align:center}.likeCard:hover{opacity:.9}.likeCard>h3{font-size:1.2em;margin-top:.4em;margin-bottom:0}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var limeMe=(function(){var instance;return function(likeme){if(!instance){instance=new CreateSingleton(likeme)}return instance}})();function CreateSingleton(likeme){this.el=likeme.el;this.url=likeme.serverURL;this.color=likeme.color;this.getData()}CreateSingleton.prototype.getData=function(){const url=this.url;const html=fetch(url).then((res)=>res.text()).then((html)=>{const parser=new DOMParser();const doc=parser.parseFromString(html,"text/html");const body=doc.querySelector("body");const likeme=document.querySelector(this.el);likeme.innerHTML=body.innerHTML;if(this.color){document.querySelector(".likeCard").style.backgroundColor=this.color}var xhr=new XMLHttpRequest();xhr.open("GET",url+"/info");xhr.send();document.querySelector(".likeCard").addEventListener("click",function(){xhr.open("GET",url+"/like");xhr.send()});xhr.onreadystatechange=function(){if(xhr.readyState==4&&xhr.status==200){var data=JSON.parse(xhr.responseText);document.querySelector(".likeCard-text").innerHTML="❤ "+data.data.count}}})};
|