-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveToGmail.js
executable file
·147 lines (122 loc) · 3.77 KB
/
saveToGmail.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/// LABELING_SECTION_START
// https://github.com/puutaro/saveToGmail
/// LABELING_SECTION_END
/// SETTING_SECTION_START
editExecute="NO"
onUpdateLastModify="ON"
disableSettingValsEdit="ON"
/// SETTING_SECTION_END
/// CMD_VARIABLE_SECTION_START
/// CMD_VARIABLE_SECTION_END
/// Please write bellow with javascript
const LEAST_STRING_NUM = 300;
launchOrPaste();
function launchOrPaste(){
var doc = document;
var list = doc.querySelectorAll("h1,h2,h3");
let tocArr = makeTocArr(list);
var summary = makeSummary(tocArr);
if(summary.length < LEAST_STRING_NUM) {
summary = summaryComp(summary);
};
const url = location.href;
const body = [url, summary].join("\n\n");
jsIntent.sendGmail(
doc.title,
body,
`url=${url}`,
);
};
function makeTocArr(list){
if(list.length <= 0) return [];
var tocArr = [], curH2 = [], curH3 = [];
for (var i = 0; i < list.length; i++) {
var e = list[i];
var tagName = e.tagName ;
if (tagName == "H1") {
tocArr.push({text:e.textContent, children:(curH2=[])});
} else if(tagName == "H2"){
curH2.push({text:e.textContent, children:(curH3=[])});
} else {
curH3.push(e.textContent);
}
};
return tocArr;
}
function makePtagSummaryTotal(summaryPList, summaryEntry, prefix="-"){
var summaryEntryPTagTextTotal = "";
var summaryEntryPreTagText = "";
if(summaryPList.length <= 0) return summaryEntryPTagTextTotal;
for(var summaryPreTag of summaryPList){
summaryEntryPreTagText = summaryPreTag.textContent.substring(
0, LEAST_STRING_NUM
);
if(!summaryEntryPreTagText.trim()) continue;
summaryEntryPTagTextTotal = summaryEntryPTagTextTotal.concat(
"\n\t\t\t\t",
prefix + " ",
summaryEntryPreTagText
);
if(
summaryEntry.length + summaryEntryPTagTextTotal.length > LEAST_STRING_NUM
) break;
};
return summaryEntryPTagTextTotal;
};
function summaryComp(summary){
if(
summary.length > LEAST_STRING_NUM
) return summary.replace(/\n\n*/g, "\n");
let summaryPList = document.querySelectorAll("p");
var summaryEntry = summary;
var summaryEntryPtagTextTotal = "";
summaryEntryPtagTextTotal = makePtagSummaryTotal(
summaryPList,
summaryEntry
);
summaryEntry = summaryEntry.concat(
summaryEntryPtagTextTotal
);
if(
summaryEntry.length > LEAST_STRING_NUM
) return summaryEntry.replace(/\n\n*/g, "\n");
let summaryPreList = document.querySelectorAll("pre");
var summaryEntryPreTagTextTotal = "";
summaryEntryPreTagTextTotal = makePtagSummaryTotal(
summaryPreList,
summaryEntry,
"--",
);
summaryEntry = summaryEntry.concat(
summaryEntryPreTagTextTotal
);
if(summaryEntry) return summaryEntry.replace(/\n\n*/g, "\n");
return "no summary";
};
function makeSummary(tocArr){
var summary = "";
if(
tocArr.length <= 0
) return summary;
for (var i in tocArr) {
summary = summary.concat(tocArr[i].text, '\n');
var ch = tocArr[i].children;
if (ch.length <= 0) continue;
for (var i2 in ch) {
h2Con = ch[i2].text.trim().replaceAll('\n', ' ');
if(!h2Con) break;
summary = summary.concat("\t\t", h2Con, '\n');
h3ch = ch[i2].children;
if (h3ch.length <= 0) continue;
for (var i3 in h3ch){
h3Con = h3ch[i3].trim().replaceAll('\n', ' ');
if(!h3Con) break;
summary = summary.concat("\t\t\t\t", h3Con, '\n');
};
};
if(
summary.endsWith("\n\n")
) continue;
};
return summary.replace(/\n\n*/g, "\n");
};