Skip to content
This repository has been archived by the owner on Sep 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2 from nero2009/feature/firefox-version
Browse files Browse the repository at this point in the history
Feature/firefox version
  • Loading branch information
swyxio authored Nov 21, 2018
2 parents 48f7e95 + 83f83ae commit 579bb71
Show file tree
Hide file tree
Showing 6 changed files with 4,939 additions and 74 deletions.
26 changes: 14 additions & 12 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
var urlHost;
var getLocation = function(href) {
var l = document.createElement('a');
l.href = href;
return l;
};

function onMessage(request, sender, sendResponse) {
// console.log('backgroundjs', {
// request,
// sender,
// sendResponse
// });
if (request.netlifyPage) {
var url = getLocation(sender.url);
var slug = url.hostname;
chrome.pageAction.show(sender.tab.id);
chrome.pageAction.setIcon({
browser.pageAction.show(sender.tab.id);
browser.pageAction.setIcon({
path: 'logo16.png',
tabId: sender.tab.id
});
chrome.pageAction.setTitle({
browser.pageAction.setTitle({
title: 'Netlify Site!',
tabId: sender.tab.id
});
chrome.pageAction.setPopup({
browser.pageAction.setPopup({
tabId: sender.tab.id,
popup: 'popup.html'
});
Expand All @@ -31,14 +28,19 @@ function onMessage(request, sender, sendResponse) {
slug
});
}
if (request.method === 'setHost') {
urlHost = request.url
}else if(request.method === 'getHost'){
sendResponse(urlHost)
}
if (request.get_version) {
chrome.tabs.query(
browser.tabs.query(
{
active: true,
currentWindow: true
},
function(tabs) {
chrome.tabs.sendMessage(
browser.tabs.sendMessage(
tabs[0].id,
{
check: 'version'
Expand All @@ -51,7 +53,7 @@ function onMessage(request, sender, sendResponse) {
);
}
}
chrome.extension.onMessage.addListener(onMessage);
browser.runtime.onMessage.addListener(onMessage);

//Checks if version in use is lower than the current version
function lowerVersion(in_use_version, current_version) {
Expand Down
14 changes: 4 additions & 10 deletions content-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@ req.send(null);
req.onreadystatechange = function() {
var header = req.getResponseHeader('server');
if (header === 'Netlify') {
chrome.extension.sendMessage({
browser.runtime.sendMessage({
netlifyPage: true
});
}
};

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
chrome.extension.sendMessage(
{
hiFrom: 'contentscript',
url: document.location
},
function(response) {}
);
});
var host = document.location.host

browser.runtime.sendMessage({method:'setHost', url: host})
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"manifest_version": 2,

"name": "netlify-chrome-extension",
"description": "netlify-chrome-extension",
"name": "netlify-browser-extension",
"description": "netlify-browser-extension",
"version": "1.2.2",
"icons": {
"16": "logo16.png",
Expand Down
5 changes: 4 additions & 1 deletion popup.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<!DOCTYPE html>
<html>
<head> </head>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>

<body>
<div style="white-space: nowrap;">
Expand Down
108 changes: 59 additions & 49 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,70 @@
//Send version request to background page
chrome.extension.sendMessage({ get_version: 'true' }, function(response) {
browser.runtime.sendMessage({ get_version: 'true' }, function(response) {
// console.log('popupjs sendMessage received', { response });
});

//Update panel with version information
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
browser.runtime.sendMessage({ method:'getHost'}, function(response) {
var host = response;
const ghbutton = document.getElementById('ghbutton');
const ghspan = document.getElementById('ghspan');
const dpbutton = document.getElementById('dpbutton');
const slug = request.url.host.split('.')[0];
if (request.hiFrom === 'contentscript') {
console.log(
'[netlify chrome extension] checking if netlify site is open source...'
);
fetch('https://api.netlify.com/api/v1/sites/' + request.url.host)
.then(res => res.json())
.then(res => {
console.log('[netlify chrome extension] Yes!', { res });
document
.getElementById('label')
.appendChild(document.createTextNode('by ' + res.account_name));
document
.getElementById('version')
.appendChild(
document.createTextNode(
'Last published ' +
new Date(res.published_deploy.published_at).toLocaleDateString()
)
); // may also want commit_url and deploy_ssl_url and title
const slug = host.split('.')[0];
console.log(
'[netlify firefox extension] checking if netlify site is open source...'
);
fetch('https://api.netlify.com/api/v1/sites/' + host)
.then(res => res.json())
.then(res => {
console.log('[netlify firefox extension] Yes!', { res });
document
.getElementById('label')
.appendChild(document.createTextNode('by ' + res.account_name));
document
.getElementById('version')
.appendChild(
document.createTextNode(
'Last published ' +
new Date(res.published_deploy.published_at).toLocaleDateString()
)
); // may also want commit_url and deploy_ssl_url and title

// const img = document.getElementById('img');
// img.src = res.screenshot_url;
ghbutton.href = res.repo_url;
dpbutton.href = res.admin_url;
dpbutton.innerText = 'Deploy Logs';
ghspan.hidden = false;
// const img = document.getElementById('img');
// img.src = res.screenshot_url;
ghbutton.href = res.repo_url;
dpbutton.href = res.admin_url;
dpbutton.innerText = 'Deploy Logs';
ghspan.hidden = false;
dpspan.hidden = false;
dpbutton.hidden = false;
})
.catch(err => {
console.log('[netlify firefox extension] Nope :(', { err });
document
.getElementById('label')
.appendChild(document.createTextNode('but it is not open source'));
if (host.includes('netlify.com')) {
// attempt to show deploys
ghspan.hidden = true;
dpspan.hidden = false;
dpbutton.hidden = false;
})
.catch(err => {
console.log('[netlify chrome extension] Nope :(', { err });
document
.getElementById('label')
.appendChild(document.createTextNode('but it is not open source'));
if (request.url.host.includes('netlify.com')) {
// attempt to show deploys
ghspan.hidden = true;
dpspan.hidden = false;
dpbutton.hidden = false;
dpbutton.innerText = 'If its yours, you can still see deploy log';
dpbutton.href = 'https://app.netlify.com/sites/' + slug;
} else {
// nothing at all
ghspan.hidden = true;
dpspan.hidden = true;
}
});
}
dpbutton.innerText = 'If its yours, you can still see deploy log';
dpbutton.href = 'https://app.netlify.com/sites/' + slug;
} else {
// nothing at all
ghspan.hidden = true;
dpspan.hidden = true;
}
});


});


// Honestly its probably more complicated than needs to be but i based it off of other extensions that do the same thing.

// inject content-script into every page
// script pings background.js that there is a new page
// background.js activates the "browser action" (the little logo on the browser bar) if its a Netlify site by sniffing the Server field in the response header.
// if it is a Netlify site and you click the "browser action":
// if it is on .netlify.com host, popup.js checks if it is open source and manipulates popup.html accordingly.

Loading

0 comments on commit 579bb71

Please sign in to comment.