forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
68 lines (59 loc) · 2.36 KB
/
background.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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function customMailtoUrl() {
if (window.localStorage == null)
return "";
if (window.localStorage.customMailtoUrl == null)
return "";
return window.localStorage.customMailtoUrl;
}
function executeMailto(tab_id, subject, body, selection) {
var default_handler = customMailtoUrl().length == 0;
var action_url = "mailto:?"
if (subject.length > 0)
action_url += "subject=" + encodeURIComponent(subject) + "&";
if (body.length > 0) {
action_url += "body=" + encodeURIComponent(body);
// Append the current selection to the end of the text message.
if (selection.length > 0) {
action_url += encodeURIComponent("\n\n") +
encodeURIComponent(selection);
}
}
if (!default_handler) {
// Custom URL's (such as opening mailto in Gmail tab) should have a
// separate tab to avoid clobbering the page you are on.
var custom_url = customMailtoUrl();
action_url = custom_url.replace("%s", encodeURIComponent(action_url));
console.log('Custom url: ' + action_url);
chrome.tabs.create({ url: action_url });
} else {
// Plain vanilla mailto links open up in the same tab to prevent
// blank tabs being left behind.
console.log('Action url: ' + action_url);
chrome.tabs.update(tab_id, { url: action_url });
}
}
chrome.runtime.onConnect.addListener(function(port) {
var tab = port.sender.tab;
// This will get called by the content script we execute in
// the tab as a result of the user pressing the browser action.
port.onMessage.addListener(function(info) {
var max_length = 1024;
if (info.selection.length > max_length)
info.selection = info.selection.substring(0, max_length);
executeMailto(tab.id, info.title, tab.url, info.selection);
});
});
// Called when the user clicks on the browser action icon.
chrome.browserAction.onClicked.addListener(function(tab) {
// We can only inject scripts to find the title on pages loaded with http
// and https so for all other pages, we don't ask for the title.
if (tab.url.indexOf("http:") != 0 &&
tab.url.indexOf("https:") != 0) {
executeMailto(tab.id, "", tab.url, "");
} else {
chrome.tabs.executeScript(null, {file: "content_script.js"});
}
});