-
Notifications
You must be signed in to change notification settings - Fork 0
/
social-media.js
55 lines (50 loc) · 1.5 KB
/
social-media.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class SocialMedia {
link = null
socialLink = {
wa: {
selector: "a[wa-social]",
link: "https://api.whatsapp.com/send?text=1%",
attribute: {
"data-action": "share/whatsapp/share",
}
},
fb: {
selector: "a[fb-social]",
link: "https://www.facebook.com/sharer.php?u=1%",
},
twitter:{
selector: 'a[twitter-social]',
link: "https://twitter.com/intent/tweet?url=1%&text=2%",
args: [document.title]
},
linkin: {
selector: 'a[linkin-social]',
link: "https://www.linkedin.com/shareArticle?mini=true&url=1%",
}
}
constructor() {
this.link = window.location.href
Object.keys(this.socialLink).forEach(e => {
this.constructLink(this.socialLink[e])
})
}
constructLink = (arg) => {
let el = document.querySelectorAll(arg.selector)
let link = arg.link.replace("1%", this.link);
if(arg.args){
arg.args.forEach((e,i)=>{
link = link.replace((i+2)+"%", e)
})
}
//set link
Array.prototype.forEach.call(el, (e) => {
e.setAttribute("href", link)
//set attribute
if (arg.attribute)
Object.keys(arg.attribute).forEach(att => {
e.setAttribute(att, arg.attribute[att])
})
})
}
}
new SocialMedia()