Skip to content

Commit

Permalink
Implemented /issues/43
Browse files Browse the repository at this point in the history
  • Loading branch information
CamielBouchier committed Apr 9, 2022
1 parent 0a51a79 commit 498d8da
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Release_1_4_0
=============

* Implemented https://github.com/CamielBouchier/cb_thunderlink/issues/43

Release_1_3_0
=============

Expand Down
103 changes: 94 additions & 9 deletions mail_extension/cb_background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,84 @@

console.log("cb_background started")

//
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////

/* Port of strftime() by T. H. Doan (https://thdoan.github.io/strftime/)
*
* Day of year (%j) code based on Joe Orost's answer:
* http://stackoverflow.com/questions/8619879/javascript-calculate-the-day-of-the-year-1-366
*
* Week number (%V) code based on Taco van den Broek's prototype:
* http://techblog.procurios.nl/k/news/view/33796/14863/calculate-iso-8601-week-and-year-in-javascript.html
*/

function strftime(sFormat, date) {
if (!(date instanceof Date)) date = new Date();
var nDay = date.getDay(),
nDate = date.getDate(),
nMonth = date.getMonth(),
nYear = date.getFullYear(),
nHour = date.getHours(),
aDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
aMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
aDayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
isLeapYear = function() {
return (nYear%4===0 && nYear%100!==0) || nYear%400===0;
},
getThursday = function() {
var target = new Date(date);
target.setDate(nDate - ((nDay+6)%7) + 3);
return target;
},
zeroPad = function(nNum, nPad) {
return ((Math.pow(10, nPad) + nNum) + '').slice(1);
};
return sFormat.replace(/%[a-z]/gi, function(sMatch) {
return (({
'%a': aDays[nDay].slice(0,3),
'%A': aDays[nDay],
'%b': aMonths[nMonth].slice(0,3),
'%B': aMonths[nMonth],
'%c': date.toUTCString(),
'%C': Math.floor(nYear/100),
'%d': zeroPad(nDate, 2),
'%e': nDate,
'%F': date.toISOString().slice(0,10),
'%G': getThursday().getFullYear(),
'%g': (getThursday().getFullYear() + '').slice(2),
'%H': zeroPad(nHour, 2),
'%I': zeroPad((nHour+11)%12 + 1, 2),
'%j': zeroPad(aDayCount[nMonth] + nDate + ((nMonth>1 && isLeapYear()) ? 1 : 0), 3),
'%k': nHour,
'%l': (nHour+11)%12 + 1,
'%m': zeroPad(nMonth + 1, 2),
'%n': nMonth + 1,
'%M': zeroPad(date.getMinutes(), 2),
'%p': (nHour<12) ? 'AM' : 'PM',
'%P': (nHour<12) ? 'am' : 'pm',
'%s': Math.round(date.getTime()/1000),
'%S': zeroPad(date.getSeconds(), 2),
'%u': nDay || 7,
'%V': (function() {
var target = getThursday(),
n1stThu = target.valueOf();
target.setMonth(0, 1);
var nJan1 = target.getDay();
if (nJan1!==4) target.setMonth(0, 1 + ((4-nJan1)+7)%7);
return zeroPad(1 + Math.ceil((n1stThu-target)/604800000), 2);
})(),
'%w': nDay,
'%x': date.toLocaleDateString(),
'%X': date.toLocaleTimeString(),
'%y': (nYear + '').slice(2),
'%Y': nYear,
'%z': date.toTimeString().replace(/.+GMT([+-]\d+).+/, '$1'),
'%Z': date.toTimeString().replace(/.+\((.+?)\)$/, '$1')
}[sMatch] || '') + '') || sMatch;
});
}

////////////////////////////////////////////////////////////////////////////////////////////////////

const default_settings = {
open_mode : "three_pane",
Expand All @@ -24,7 +99,8 @@ const default_settings = {
conf_links : {
0 : {enable: true, name: "cbthunderlink", value: "cbthunderlink://$cblink$"},
1 : {enable: true, name: "thunderlink", value: "thunderlink://messageid=$msgid$"},
}
},
now_strftime_format : "%d-%m-%Y %H:%M:%S"
}

var settings = default_settings
Expand All @@ -41,10 +117,11 @@ async function get_settings() {
await browser.storage.local.set({cb_thunderlink: settings})
} else {
settings = {
open_mode : config.cb_thunderlink.open_mode,
avoid_folders : config.cb_thunderlink.avoid_folders,
prefer_folders : config.cb_thunderlink.prefer_folders,
conf_links : config.cb_thunderlink.conf_links
open_mode : config.cb_thunderlink.open_mode,
avoid_folders : config.cb_thunderlink.avoid_folders,
prefer_folders : config.cb_thunderlink.prefer_folders,
conf_links : config.cb_thunderlink.conf_links,
now_strftime_format : config.cb_thunderlink.now_strftime_format
}
if (!settings.open_mode) {
settings.open_mode = default_settings.open_mode
Expand All @@ -62,6 +139,10 @@ async function get_settings() {
settings.conf_links = default_settings.conf_links
await browser.storage.local.set({cb_thunderlink: settings})
}
if (!settings.now_strftime_format) {
settings.now_strftime_format = default_settings.now_strftime_format
await browser.storage.local.set({cb_thunderlink: settings})
}
}
// A change could require a new context menu setting.
create_context_menu()
Expand Down Expand Up @@ -140,6 +221,9 @@ async function link_to_clipboard(idx, the_message, selected_text) {
let date_locale = date_time.toLocaleDateString()
let time_locale = date_time.toLocaleTimeString()

// https://github.com/CamielBouchier/cb_thunderlink/issues/43 : Adding now time
let now_date_time = strftime(settings.now_strftime_format)

// Following few lines are +/- from original thunderlink.
// replace a few characters that frequently cause trouble
// with a focus on org-mode, provided as filtered_subject
Expand All @@ -159,7 +243,8 @@ async function link_to_clipboard(idx, the_message, selected_text) {
link = link.replace(/\$time_locale\$/ig, time_locale)
link = link.replace(/\$subject\$/ig, subject)
link = link.replace(/\$subject_filtered\$/ig, subject_filtered)
link = link.replace(/\$selected_text\$/ig, selected_text ?? "");
link = link.replace(/\$selected_text\$/ig, selected_text ?? "")
link = link.replace(/\$now\$/ig, now_date_time)

console.debug(`Storing in clipboard: ${link}`);

Expand Down
10 changes: 10 additions & 0 deletions mail_extension/cb_options.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ <h4><b>Folders to avoid</b></h4>
<input id="avoid_folders" type="text"
placeholder="list of folders to be avoided"/>

<h4><b>Time format for '$now$'</b></h4>

<p> See <a href ="https://github.com/thdoan/strftime"> supported specifiers</a></p>

<input id="now_strftime_format" type="text" placeholder="%d-%m-%Y %H:%M:%S"/>

<h4><b>Configurable links</b></h4>

<p>Here you can configure the links to be generated.</p>
Expand Down Expand Up @@ -117,6 +123,10 @@ <h4><b>Configurable links</b></h4>
$selected_text$ =&gt; Currently selected text
(only available when calling cb_thunderlink by right-clicking in a message tab, <strong>not</strong> from the message list).
</li>
<li>
$now$ =&gt; the time the link was generated, in a format defined by the
setting 'now_strftime_format'.
</li>
</ul>
<p>Typical default entries are:</p>
<ul>
Expand Down
19 changes: 11 additions & 8 deletions mail_extension/cb_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ function store_settings() {
prefer_folders[i] = prefer_folders[i].trim()
}
let settings = {
open_mode : document.querySelector('input[name="open_mode"]:checked').value,
conf_links : conf_links,
avoid_folders : avoid_folders,
prefer_folders : prefer_folders,
open_mode : document.querySelector('input[name="open_mode"]:checked').value,
now_strftime_format : document.getElementById('now_strftime_format').value.trim(),
conf_links : conf_links,
avoid_folders : avoid_folders,
prefer_folders : prefer_folders,
}
browser.storage.local.set({cb_thunderlink: settings})
}
Expand All @@ -66,11 +67,13 @@ for (input of inputs) {

browser.storage.local.get('cb_thunderlink').then((settings) => {
if (settings.cb_thunderlink) {
let open_mode = settings.cb_thunderlink.open_mode
let avoid_folders = settings.cb_thunderlink.avoid_folders
let prefer_folders = settings.cb_thunderlink.prefer_folders
let conf_links = settings.cb_thunderlink.conf_links
let open_mode = settings.cb_thunderlink.open_mode
let now_strftime_format = settings.cb_thunderlink.now_strftime_format
let avoid_folders = settings.cb_thunderlink.avoid_folders
let prefer_folders = settings.cb_thunderlink.prefer_folders
let conf_links = settings.cb_thunderlink.conf_links
document.getElementById(open_mode).checked = true
document.getElementById('now_strftime_format').value = now_strftime_format
document.getElementById('avoid_folders').value = avoid_folders.join(', ')
document.getElementById('prefer_folders').value = prefer_folders.join(', ')
for (const key in conf_links) {
Expand Down
2 changes: 1 addition & 1 deletion mail_extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"manifest_version": 2,
"version": "1.3.0",
"version": "1.4.0",
"name": "cb_thunderlink",
"description": "cb_thunderlink is a thunderlink replacement.",
"author": "camiel@bouchier.be",
Expand Down

0 comments on commit 498d8da

Please sign in to comment.