-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubstitute.js
123 lines (112 loc) · 3.66 KB
/
substitute.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
/*** Helper functions ***/
function onError(error) {
console.log("Error: ${error}");
}
/**
* Serializes a dictionary for use in a URL query string
*/
function urlSerialize(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
/**
* Determines whether a given complex object has some property given as list of arguments
*/
function hasProperty(jsonObject, path) {
var args = Array.prototype.slice.call(arguments, 1);
var obj = jsonObject;
for (var i=0; i<args.length; i++) {
var arg = args[i];
// Is the current object an array?
if (Number.isInteger(arg)) {
if (!Array.isArray(obj) || arg < 0 || arg >= obj.length) {
return false;
}
}
// Otherwise consider it as an object
else if (!obj || !obj.hasOwnProperty(args[i])) {
return false;
}
obj = obj[args[i]];
}
return true;
}
/*** Tag substitution ***/
function insertImageCaption(imageTag, subscriptionKey) {
var uriBase = "https://westeurope.api.cognitive.microsoft.com/vision/v1.0/analyze";
// Request parameters.
var params = {
"visualFeatures": "Categories,Description",
"language": "en",
};
// src attribute will always give the full url
var sourceImageUrl = imageTag.src;
// Set request headers and body
const requestHeaders = new Headers();
requestHeaders.append('Content-Type', 'application/json');
requestHeaders.append("Ocp-Apim-Subscription-Key", subscriptionKey);
const requestURL = uriBase + "?" + urlSerialize(params);
const driveRequest = new Request(requestURL, {
method: "POST",
headers: requestHeaders,
mode: 'cors',
body: '{"url": "' + sourceImageUrl + '"}'
});
// Perform API call
return fetch(driveRequest).then((response) => {
if (response.status === 200) {
promise = response.text();
promise.then((jsonResult) => {
const result = JSON.parse(jsonResult);
if (!hasProperty(result, "description", "captions", 0, "text")) {
console.log("Response does not have a caption");
return;
}
const text = result["description"]["captions"][0]["text"];
// Prepend with note that the caption is not necessarily correct
var newAlternativeText = "Image may show: " + text;
// Preserve old alternative attribute
if (imageTag.hasAttribute("alt")) {
newAlternativeText = imageTag.getAttribute("alt") + "; " + newAlternativeText;
}
imageTag.setAttribute("alt", newAlternativeText);
});
} else {
console.log("Failed to fetch request " + response.status);
}
}).catch(function(response) {
console.log(response);
});
}
/**
* Loads the Vision API subscription key from browser's storage
*/
function loadPreferences(onFinishedCallback, onErrorCallback) {
browser.storage.local.get("subscriptionKey").then((response) => {
if (!hasProperty(response, "subscriptionKey")) {
console.log("Please enter your substitution key for the Vision API in the addon's preferences menu");
return;
}
onFinishedCallback(response.subscriptionKey);
}, onErrorCallback);
}
/**
* Finds all img tags on the document and check their alt attributes
*/
function insertImageCaptions(subscriptionKey) {
// TODO update tags that are loaded on runtime
var imageTags = document.body.getElementsByTagName("img");
for (var i=0; i<imageTags.length; i++) {
// Ignore small images
var width = imageTags[i].naturalWidth;
var height = imageTags[i].naturalHeight;
if (width <= 50 || height <= 50) {
continue;
}
insertImageCaption(imageTags[i], subscriptionKey);
}
}
// Main method
loadPreferences(insertImageCaptions, onError);