-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent.js
executable file
·66 lines (58 loc) · 1.9 KB
/
content.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
// Utility for writing to the clipboard
const writeClipboard = (str) => {
var copyFrom = document.createElement("textarea");
copyFrom.textContent = str;
document.body.appendChild(copyFrom);
copyFrom.select();
document.execCommand('copy');
document.body.removeChild(copyFrom);
}
// Initialize the InboxSDK
InboxSDK.load('1', 'sdk_gmail-id-copy_457e9be754').then(function(sdk){
var barMessage;
// Find the index of the currently logged in user
var userNumber = 0;
var matches = window.location.pathname.match(/^\/mail\/u\/(\d+)\/.*/);
if (matches) {
userNumber = matches[1];
}
// Whenever a message is loaded, add a menu button to the More menu
sdk.Conversations.registerMessageViewHandler(function(messageView) {
messageView.addToolbarButton({
section: 'MORE',
title: 'Copy message ID',
onClick: function () {
// When it's clicked, send a message to page.js
barMessage = sdk.ButterBar.showSaving({text: "Loading...", confirmationText: "Message ID copied to clipboard!"});
window.postMessage({type: 'FETCH_ATTACHMENT', id: messageView.getMessageID(), user: userNumber}, "*");
},
orderHint: 0
});
});
// Handlers for message id success and failure
window.addEventListener("message", function(event) {
if (event.source != window || event.data.type != 'FETCH_RESULT') {
return;
}
writeClipboard('rfc822msgid:' + event.data.result);
barMessage.resolve();
});
window.addEventListener("message", function(event) {
if (event.source != window || event.data.type != 'FETCH_ERROR') {
return;
}
barMessage.reject();
sdk.ButterBar.showError({text: "Unable to find message ID"});
});
});
/**
* Inject the page script.
*
* See page.js for why we need to do this
*/
var s = document.createElement('script');
s.src = chrome.extension.getURL('page.js');
s.onload = function () {
this.parentNode.removeChild(this);
};
(document.head || document.documentElement).appendChild(s);