/
tabs2text-utils.js
166 lines (152 loc) · 3.78 KB
/
tabs2text-utils.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/************************
** TABS2TEXT
** description: Export all your opened tabs to a CSV, HTML, JSON or Markdown file.
** homepage_url: https://github.com/deniszanin/tabs2text
** license: Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
**
** FILE: tabs2text-utils.js
************************
**/
"use strict";
/**
* ====================
* = EVENT LISTENERS =
* = LOCALES function =
**
** Default action on loading page.
**/
document.addEventListener("DOMContentLoaded", () => {
for (const element of document.querySelectorAll("[data-i18n")) {
element.innerText = chrome.i18n.getMessage(element.getAttribute("data-i18n")).replace(/"/g,"\"");
}
});
/**
* =======================
* = PROTOTYPE functions =
**
**
* Function to format Date object to a specific string.
* @source: https://stackoverflow.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object
* @param {*}
* @returns {string} strDate
**/
Date.prototype.yyyymmdd = function()
{
var mm = this.getMonth() + 1; // getMonth() is zero-based
var dd = this.getDate();
return [this.getFullYear(),
(mm>9 ? '' : '0') + mm,
(dd>9 ? '' : '0') + dd
].join('');
};
/**
* =============
* = FUNCTIONS =
**
* Function to send a message to background.js.
* @param {string} type
* @param {object} object
* @param {function} callback
* @returns {*}
**/
function sendBackgroundMessage(type, object, callback)
{
chrome.runtime.sendMessage({type, object}, callback);
}
/**
* Function to format a given date.
* @source: https://stackoverflow.com/questions/23593052/format-javascript-date-as-yyyy-mm-dd
* @param {string} argDate
* @returns {string} retDate
**/
function formatDate(argDate)
{
let tDate = new Date(argDate),
month = '' + (tDate.getMonth() + 1),
day = '' + tDate.getDate(),
year = tDate.getFullYear(),
hours = '' + tDate.getHours(),
minutes = '' + tDate.getMinutes();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
if (hours.length < 2) hours = '0' + hours;
if (minutes.length < 2) minutes = '0' + minutes;
return [month, day, year, hours, minutes].join('-');
}
/**
* Function to check if this browser is Firefox.
* @source: https://github.com/EFForg/https-everywhere/blob/bcaf7bdecf147c2a46d7ed73bce64389d828d865/chromium/background-scripts/background.js
* @param {*}
* @returns {boolean}
**/
function isFirefox()
{
if (typeof(browser) != "undefined")
{
// #TODO: get information about browser properly.
/*_getBrowserInfo().then(function(info) {
if (info == "Firefox") {
return true;
} else {
return false;
}
});*/
return true;
} else {
return false;
}
}
/**
* =========================
* = GET and SET functions =
**
**
* Function to get current date and format it.
* @param {*}
* @returns {date}
**/
function _getDate()
{
let tDate = new Date();
return tDate.yyyymmdd();
}
/**
* Function to get stored value in BrowserStorage.
* @param {string} argKey
* @param {object} argStorageArea
* @returns {promise} result
**/
function _getStoredValues(argKey, argStorageArea)
{
return new Promise(result => argStorageArea.get([argKey], data => result(data[argKey])));
}
/**
* Async function to get user preferences.
* @param {string} argKey
* @returns {promise} result
**/
async function _getUserSettings(argKey)
{
return await _getStoredValues(argKey, chrome.storage.local);
}
/**
* Function to get promise browser information.
* @param {*}
* @returns {promise} result
**/
function _getBrowserGenericInfo()
{
return new Promise( resolve => {
let result = browser.runtime.getBrowserInfo();
resolve(result);
});
}
/**
* Async function to get browser information.
* @param {*}
* @returns {object} result
**/
async function _getBrowserInfo()
{
return await _getBrowserGenericInfo();
}