-
Notifications
You must be signed in to change notification settings - Fork 0
/
addInspScript.js
72 lines (58 loc) · 2.09 KB
/
addInspScript.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
// Logic to add new inspiration that the user submits
document.addEventListener('DOMContentLoaded', function () {
StorageArea = chrome.storage.sync;
// If there is content stored yet, initialize to []
StorageArea.getBytesInUse(null, (bytes) => {
if (bytes === 0) {
StorageArea.set({"contentArray": []});
}
});
// For overriding submitting the form
$("#addContent").submit(checkContent);
});
function checkContent(event) {
// stop form from submitting normally
event.preventDefault();
// get values from form
var $form = $(this),
content = $form.find('textarea[name="content"]').val(),
contentType = $form.find('input[name="content-type"]:checked').val();
if (isImage(content) && contentType === 'text') {
let r = confirm("You selected text, but that looks like an image url.\n" +
"Click OK if you want it to be text input.");
if (!r) {
return;
}
} else if (!isImage(content) && contentType === 'image') {
let r = confirm("You selected image, but that doesn't look like an image url.\n" +
"Click OK if you want it to be an image url.");
if (!r) {
return;
}
}
addContent(content, contentType, $form);
}
function addContent(content, contentType, $form) {
StorageArea = chrome.storage.sync;
// store the new content with a new, higher, key
StorageArea.get("contentArray", (obj) => {
var contentArray = obj["contentArray"];
contentArray.push({
content: content,
type: contentType,
});
StorageArea.set({"contentArray": contentArray});
});
// add confirmation text above the form
if (contentType === "image") {
$("#confirmation").html("Added new image <img src="+content+" />");
} else {
$("#confirmation").html("<span> Added new text \""+content+"\"</span>");
}
// clear the form
$form.find('textarea[name="content"]').val('');
};
function isImage(text) {
let re = /^\S+\.(jpg|jpeg|png|gif|bmp)\S*$/i;
return !!text.match(re);
}