-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtitleAndDescriptionUtils.js
More file actions
49 lines (41 loc) · 1.67 KB
/
titleAndDescriptionUtils.js
File metadata and controls
49 lines (41 loc) · 1.67 KB
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
function xor(a,b){ return (a && !b) || (!a && b); };
function isNullOrEmpty(str){ return str == null || str.length == 0; };
function findMetaTagMatchingName(str){
return Array.prototype.filter.call(
document.querySelectorAll("meta[name]"),
item =>(item.name != null && item.name.includes(str))
);
};
function findMetaTagMatchingProperty(str){
return Array.prototype.filter.call(
document.querySelectorAll("meta[property]"),
item => {
let prop = item.getAttribute('property');
return prop != null && prop.includes(str);
/* equivalent to: (item.getAttribute('property') != null && item.getAttribute('property').includes(str)) */
}
);
};
function findMetaTagMatchingNameOrProperty(str){
return Array.prototype.filter.call(
document.querySelectorAll(["meta[name]","meta[property]"]),
function (item){
let maybePass = !xor( isNullOrEmpty(item.name), isNullOrEmpty(item.getAttribute('property')) );
if (maybePass){return false;} /* Not sure how I want to handle this scenario, so for now, dont handle it at all */
return (
(item.name != null && item.name.includes(str)) ||
(item.getAttribute('property') != null && item.getAttribute('property').includes(str))
);
}
);
};
function setTabTitle(titleStr){
document.head.querySelector('title').text = titleStr;
};
function setDescription(description){
document.getElementsByTagName("meta").description = description;
};
function setIndexedTitle_Desc(titleStr, descStr, appendTitleToDesc = false){
findMetaTagMatchingNameOrProperty('title').map(e=> e.content = titleStr)
findMetaTagMatchingNameOrProperty('description').map(e=> e.content = (appendTitleToDesc ? descStr + ' - ' + titleStr : descStr))
};