-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject.js
128 lines (104 loc) · 5.09 KB
/
inject.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
/*******
Until there is a message from the background asking if there should be an icon, nothing should be checked or reported (so if it changes, the changes get caught). At the moment this will just fire when the script is run, irrespective of whether it is even firing on the right tab.
And there is also no provision yet for talking to frames and finding out if they have longdescs. That's to be done soon.
*******/
console.log("started processing");
//if you need to report, then...
var longdescs = document.querySelectorAll('img[longdesc]');
if (longdescs.length) { //otherwise, fuggedaboudit
console.log((longdescs.length + " images found with longdesc attribute"));
//make a title message for the icon
numberOfImages = document.querySelectorAll('img').length;
iconTitle = longdescs.length + " images of " + numberOfImages + " have a long description";
if (window.frames.length > 0) {
iconTitle += ". There are also " + window.frames.length + " frames I haven't checked yet";
}
//ask the background to show the default_icon
chrome.extension.sendMessage({"type":"longdescPresent", "title": iconTitle}, function(response) {
console.log(response);
});
}
// Build an individual object to send across, for an image.
//
// parameter item is an element that has a longdesc (presumably img or frame, but can be anything)
// derive a title and max 100x100 thumbnail (or use defaults) and make an object representing it
// e.g. { "src": <a dataURL> ; title : <guess ;) > ; href: <the longdesc URL>}
function buildDesc(item) {
var tempObj = {};
//Get a thumbnail
try {
var imgData, canvas, ctx;
var img = new Image();
img.src = item.src;
canvas = document.createElement('canvas');
canvas.width = img.width; canvas.height = img.height;
ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0);
// Set the image size to 100px with the original aspect ratio.
if (canvas.width > canvas.height) {
canvas.style.width = "100px";
canvas.style.height = (img.height / img.width * 100) + "px";
} else {
canvas.style.height = "100px";
canvas.style.width = (img.width / img.height * 100) + "px";
}
tempObj.src = canvas.toDataURL();
console.log(tempObj.src);
} catch(error) {
//Otherwise use a dummy icon
console.log( error + "Using dummy image");
tempObj.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAAAAACPAi4CAAAAIXRFWHRTb2Z0d2FyZQBHcmFwaGljQ29udmVydGVyIChJbnRlbCl3h/oZAAABKUlEQVR4nOzX3Q2DIBAH8P8KruAKLOCDK7CCK7CCK7CCK/DiAF2BFVjBgk2qJHAekj40ck813P3Kx1VSrAMqYlhRVY+9fKuIHWkABTgzK6XM6yZgRkAI0fkUaW8AE+Tnq63ySVMxMMJ8Py9hEoXAhPPKpU9TRYBFr93xqEOeS+RlgTkMHDtnwuNSAoR9wxwDqTXQwFFRDuwV8TGULWFbpDzq/ZlEW8IBonDINQITCG3Qp06RCehsPQ8w2UbmAS7qiBuAJOo5gM3PnwfI3P5xgXND3gEWCJq/AiZyAgxA0MPXQDdWAulX4cOA/O+AB2iij1nARTwAcCP61H3CBvr4gioG9guOeqP9HNi6yiVsL4FOE+N/0AcNaEAD+MCAqhhQ+/f/DQAA//8DAOJ/aO3Iwa32AAAAAElFTkSuQmCC"
}
//Get the link
tempObj.href = item.longDesc;
//And here is your object
return tempObj;
}
chrome.extension.onMessage.addListener(function(msg,sender,sendResponse) {
console.log(msg);
/*** If we aren't, top, pass the message up instead of to the popup. or should background pass it to all frames? Should be a synch step of getting reply from children ***/
if (msg.msg == "Please send me the longdescs") {
var descriptionList = [], j=1; //note this covers the repetition below too… :S
if (top === window) { //make 'em and do it
if (longdescs.length) { //if there are any we deal with them...
for(var i=0;i<longdescs.length;i++) {
var gather = {}, regather;
console.log(('Processing ' + i));
//Get a title
var tempTitle = "unlabeled image " + j;
if (longdescs[i].alt) {
tempTitle = longdescs[i].alt;
} else if (longdescs[i].title) {
tempTitle = longdescs[i].title;
} else j++;
gather = buildDesc(longdescs[i]);
gather.title = tempTitle;
descriptionList.push(gather);
}
}
sendResponse(descriptionList);
return true;
} else { //we're a frame. Pass the message up the line.
//take this repeated code and make a clean function.
if (longdescs.length) { //if there are any we deal with them...
for(var i=0;i<longdescs.length;i++) {
var gather = {}, regather;
console.log(('Processing ' + i));
//Get a title
var tempTitle = "unlabeled image " + j;
if (longdescs[i].alt) {
tempTitle = longdescs[i].alt;
} else if (longdescs[i].title) {
tempTitle = longdescs[i].title;
} else j++;
gather = buildDesc(longdescs[i]);
gather.title = tempTitle;
descriptionList.push(gather);
parent.window.postMessage(gather,"*");
}
}
//send the message to the parent
}
} else { //if a frame sent the message, post the descriptions and say it's from a frame.
console.log(('some other message, from '+sender));
}
});