-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
executable file
·64 lines (55 loc) · 1.47 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
var sitesByTab = {};
chrome.tabs.onUpdated.addListener( showPageActionIfJetpackSite );
function showPageActionIfJetpackSite( tabId, changeInfo, tab ) {
var url, xhr;
if ( changeInfo.status !== 'loading' ) {
return;
}
// Remove Protocol
url = tab.url.replace( /^.*?:\/\//, '' );
// unTrailingSlashIt
url = url.replace( /\/$/, '' );
// replace / with ::
url = url.replace( /::/g, '/' );
xhr = new XMLHttpRequest();
xhr.open( 'GET', 'https://public-api.wordpress.com/rest/v1.1/sites/' + url );
xhr.onreadystatechange = function() {
var site;
if ( xhr.readyState == 4 && xhr.status !== 404 ) {
site = JSON.parse( xhr.responseText );
if ( site.jetpack ) {
showPageAction( tabId, site );
}
}
}
xhr.send();
}
function showPageAction( tabId, site ) {
sitesByTab[ tabId ] = site;
chrome.pageAction.setPopup( {
tabId: tabId,
popup: 'popup.html'
} );
chrome.pageAction.show( tabId );
}
chrome.runtime.onMessage.addListener( function( request, sender, sendResponse ) {
if ( request.greeting === "GetSite" ) {
chrome.tabs.getSelected( null, function( tab ) {
sendResponse( {
site: sitesByTab[ tab.id ]
} );
} );
return true;
}
} );
chrome.runtime.onMessage.addListener( function( message ) {
if ( message && message.type == 'copy' ) {
var input = document.createElement( 'textarea' );
document.body.appendChild( input );
input.value = message.text;
input.focus();
input.select();
document.execCommand( 'Copy' );
input.remove();
}
} );