-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWL.js
138 lines (97 loc) · 4.01 KB
/
WL.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
(function (WL) {
if (window.top !== window.top) {
return;
}
// common config
var config = {
'host': 'https://www.wunderlist.com'
};
// modules can be imported individually for different extensions, but all will export onto window.WL
if (!WL) {
window.WL = {};
WL = window.WL;
}
// trims whitespace, reduces inner newlines and spaces
// and keeps string below 500 chars
function trim (string, length) {
// default length to trim by is 500 chars
length = length || 500;
// get rid of stacked newlines
string = (string || '').replace(/\n{3,}/g, '\n\n');
// get rid of redonk spaces
string = string.replace(/\s{3,}/g, ' ');
string = $.trim(string);
// only trim string length if it's
if (string.length > length) {
string = string.substring(0, length) + '...';
}
return string;
}
function fetchOpenGraph () {
var data = {};
data.url = $('meta[name="og:url"]').attr('content') || $('meta[property="og:url"]').attr('content');
data.title = $('meta[name="og:title"]').attr('content') || $('meta[property="og:title"]').attr('content');
data.description = $('meta[name="og:description"]').attr('content') || $('meta[property="og:description"]').attr('content');
return data;
}
function fetchTwitterCard () {
var data = {};
data.url = $('meta[name="twitter:url"]').attr('content');
data.title = $('meta[name="twitter:title"]').attr('content');
data.description = $('meta[name="twitter:description"]').attr('content');
return data;
}
function buildUrl (data) {
data = data || {};
// Builds url for passing data to wunderlist.com extension frame.
// Takes passes in data, or defaults to the tabs title and url or text selection in the frame
// Scrape predefined data
var scrapeData = WL.scrape(data);
// fetch open graph and twitter card meta data
var openGraph = fetchOpenGraph();
var twitterCard = fetchTwitterCard();
// build main meta datas - left priority
var title = scrapeData.title || data.title || openGraph.title || twitterCard.title || document.title || '';
var description = trim(openGraph.description || twitterCard.description || $('meta[name="description"]').attr('content') || '');
var url = scrapeData.url || data.url || openGraph.url || twitterCard.url || $('link[rel="canonical"]').attr('href') || window.location.href;
// start building note from passed in data
// and make sure it doesn't exceed the max length
var note = trim(data.note || scrapeData.note, 1000);
// grab user selection and trim to max 5000 characters
var selection = trim(window.getSelection().toString() || '', 5000);
// if not passed in note data use a default note constructor
if (!data.note && !scrapeData.note) {
// use selection over description if present
// append url after description in note
note = (selection ? selection : description) + " \n" + url;
}
else {
// use selection over note if present and allow scraper exclude url
note = (selection ? selection : note) + " \n" + url;
}
// prepare specialList data if present
if (scrapeData.specialList) {
note = 'specialList:' + scrapeData.specialList + '\u2603' + note;
}
// save scraper type/name for in-app analytics
if (scrapeData.scraper) {
note = 'scraper:' + scrapeData.scraper + '\u2603' + note;
}
// encode
title = encodeURIComponent(title);
note = encodeURIComponent(note);
return config.host + '/#/extension/add/' + title + '/' + note;
}
function buildCss (options) {
// Create styles for overlay
var transitionSpeed = options && options.transitionSpeed || 500;
var opacity = options && options.opacity || 0;
return 'opacity:' + opacity + ';-webkit-transition:opacity ' + transitionSpeed + 'ms linear;';
}
// exports
WL.fetchOpenGraph = fetchOpenGraph;
WL.fetchTwitterCard = fetchTwitterCard;
WL.buildUrl = buildUrl;
WL.buildCss = buildCss;
WL.config = config;
})(window.WL);