forked from gokubi/Salesforce-Id-Clipper-Chrome-Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.html
99 lines (87 loc) · 3.15 KB
/
background.html
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
<!DOCTYPE html>
<!--
* Copyright (c) 2010 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.
-->
<html>
<head>
<script>
ID_RE = [
/https\:\/\/.*\.salesforce\.com\/(\w{18})/, // Matches id-18 for a standard salesforce page
/https\:\/\/.*\.salesforce\.com\/(\w{15})/, // Matches id-15 for a standard salesforce page
/https\:\/\/.*\.salesforce\.com\/apex\/.*id=(\w{15})/, // Matches id for an apex/visualforce page
];
LINK_RE = [
/(https\:\/\/.*\.salesforce\.com\/\w{18})/, // Matches link (id-18) for a standard salesforce page
/(https\:\/\/.*\.salesforce\.com\/\w{15})/, // Matches link (id-15) for a standard salesforce page
/(https\:\/\/.*\.salesforce\.com\/apex\/.*id=\w{15})/, // Matches link for an apex/visualforce page
];
// Called when the url of a tab changes.
function checkForValidUrl(tabId, changeInfo, tab) {
// If it's a salesforce database URL and contains an id
if ((tab.url.indexOf('salesforce') > -1) &&
((tab.url.match(ID_RE[1]) != null) || (tab.url.match(ID_RE[2]) != null))) {
// ... show the page action.
chrome.pageAction.show(tabId);
chrome.pageAction.setIcon({path: "clipper.png", tabId: tab.id});
}
};
// Extract the ID from a URL and copy to clipboard
function extractID(url, regex_set) {
if (!regex_set) regex_set = ID_RE;
for (var i in regex_set) {
var match = url.match(regex_set[i]);
if (match) {
return match[1];
break; // Even if 'return' is removed, processing should still stop;
}
}
return false;
}
// Extract a link from a URL (delegates to extractID)
function extractLink(url) {
return extractID(url, LINK_RE);
}
function copyToClipboard(copy_me) {
var bg = chrome.extension.getBackgroundPage();
var clipboardholder = bg.document.getElementById("clipboardholder");
clipboardholder.style.display = "block";
clipboardholder.value = copy_me;
clipboardholder.select();
bg.document.execCommand("Copy");
clipboardholder.style.display = "none";
return true;
}
// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);
// Called when the user clicks on the page action.
chrome.pageAction.onClicked.addListener(function(tab) {
var id = extractID(tab.url);
if (id) {
copyToClipboard(id);
chrome.pageAction.setIcon({path: "clippered.png", tabId: tab.id});
}
});
chrome.contextMenus.create(
{"title": "Copy Salesforce Id", "contexts" : ["link"],"onclick": onIdCopyClick}
)
chrome.contextMenus.create(
{"title": "Copy Clean Salesforce URL", "contexts" : ["link"],"onclick": onCleanCopyClick}
)
// copy Id only
function onIdCopyClick(info, tab) {
var id = extractID(info.linkUrl);
if (id) copyToClipboard(id);
}
// copy clean URL
function onCleanCopyClick(info, tab) {
var link = extractLink(info.linkUrl);
if (link) copyToClipboard(link);
}
</script>
</head>
<body>
<textarea id="clipboardholder" style="display:none;"></textarea>
</body>
</html>