Skip to content

Commit

Permalink
Merge pull request #1 from cemkucuk/v1.1.0
Browse files Browse the repository at this point in the history
queried link for Opsgenie alerts page shortcuts and refactored popup …
  • Loading branch information
Cem Küçük authored Jun 15, 2020
2 parents ea09522 + 88637b3 commit c8c31b4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 40 deletions.
48 changes: 26 additions & 22 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var enabled = false
var requestUrl = ""
var query = ""
var timeInterval = 30000
var popupData = {}
var latestAlertDate
Expand Down Expand Up @@ -42,6 +43,7 @@ function startExecution() {
}
enabled = items.enabled
timeInterval = items.timeInterval * 1000
query = items.query
if (items.apiKey !== '') {
requestUrl = url + "v2/alerts?limit=100&sort=createdAt&" + "apiKey=" + items.apiKey + "&query=" + encodeURI(items.query)
}
Expand All @@ -59,7 +61,11 @@ function doExecute() {
setFailurePopupData("Alert API Poller is disabled, for enabling it use switch in <a href=\"options.html\" target=\"_blank\"> option page↗</a>.")
return
}
fetch(requestUrl)
fetch(requestUrl, {
headers: {
"User-Agent": "opsgenie-alert-notifier-extension"
}
})
.then(function (response) {
if (!response.ok) {
response.text().then(function (responseBody) {
Expand All @@ -82,21 +88,21 @@ function sendNotificationIfNewAlerts(data) {
console.log("notification")
if (latestAlertDate !== undefined) {
var newAlerts = []
for (let i=0; i<data.length; i++) {
if(latestAlertDate < data[i].createdAt)
newAlerts.push({
"title" : data[i].message,
"message": "Priority: " + data[i].priority
})
for (let i = 0; i < data.length; i++) {
if (latestAlertDate < data[i].createdAt)
newAlerts.push({
"title": data[i].message,
"message": "Priority: " + data[i].priority
})
}
if(newAlerts.length == 1) {
if (newAlerts.length == 1) {
chrome.notifications.create(data[0].id, {
type: 'basic',
iconUrl: 'images/128x128.png',
title: newAlerts[0].title,
message: newAlerts[0].message,
}, function () { });
} else if(newAlerts.length > 0) {
} else if (newAlerts.length > 0) {
chrome.notifications.create('alert-list', {
type: 'list',
iconUrl: 'images/128x128.png',
Expand All @@ -113,7 +119,7 @@ function sendNotificationIfNewAlerts(data) {

chrome.notifications.onClicked.addListener(function (notificationId) {
if (notificationId === 'alert-list') {
window.open('https://app.opsgenie.com/', '_blank')
window.open("https://app.opsgenie.com/alert/list?query=" + encodeURI(query), '_blank')
} else {
window.open('https://opsg.in/a/i/' + notificationId, '_blank')
}
Expand All @@ -136,20 +142,18 @@ function setBadge(count) {

function setSuccessPopupData(responseData) {
console.log("success popup data filled", responseData)
popupData = {
status: "success",
reason: "",
data: responseData.slice(0, 10),
time: new Date().toLocaleString()
}
popupData.status = "success"
popupData.reason = ""
popupData.data = responseData
popupData.time = new Date().toLocaleString()
popupData.ogUrl = "https://app.opsgenie.com/alert/list?query=" + encodeURI(query)
}

function setFailurePopupData(message) {
console.log("failure popup data filled", message)
popupData = {
status: "failure",
reason: message,
data: [],
time: new Date().toLocaleString()
}
popupData.status = "failure"
popupData.reason = message
popupData.data = []
popupData.time = new Date().toLocaleString()
popupData.ogUrl = "https://app.opsgenie.com/alert/list?query=" + encodeURI(query)
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Opsgenie Alert Notifier",
"version": "1.0.0",
"version": "1.1.0",
"description": "An extension for showing latest alerts and getting notifications for new alert(s) via polling Opsgenie’s REST API.",
"background": {
"scripts": ["background.js"]
Expand Down
14 changes: 9 additions & 5 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

body {
width: 600px;
height: 20px;
height: 300px;
}

th {
Expand Down Expand Up @@ -87,10 +87,14 @@

<body>
<p id="info-text"></p>
<table id="alert-list">
<tr>
</tr>
</table>
<div style="height:300px;overflow-y: scroll;">

<table id="alert-list">
<tr>
</tr>
</table>
</div>

<script src="jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
Expand Down
31 changes: 19 additions & 12 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
var rowText = '<td>x{count}</td><td><span class="ak-lozenge ak-lozenge__appearance-default-bold {priority}-bg">{priority}</span></td><td>{message}</td>'

chrome.runtime.sendMessage({}, function (response) {
console.log(response)
if (response.status !== "success") {
document.getElementById("info-text").innerHTML = '<p style="color:#BF2600">' + response.reason + "</p>"
} else {
console.log(response)
var infoText = document.getElementById("info-text")
infoText.style.removeProperty("padding")
infoText.innerHTML = "<i>Last updated <b>@ " + response.time + "</b></i>" + "<a style=\"float: right;\" href=\"https://app.opsgenie.com\" target=\"_blank\"> see all alerts↗ </a>"
var table = document.getElementById("alert-list");
response.data.forEach(function (alert, i) {
var row = table.insertRow(i + 1);
row.setAttribute("id", alert.id)
row.innerHTML = rowText
.replace("{message}", alert.message)
.replace("{count}", alert.count)
.replace(/{priority}/g, alert.priority)
});
if(response.data.length > 0) {
var infoText = document.getElementById("info-text")
infoText.style.removeProperty("padding")
infoText.innerHTML = "<i>Last updated @ " + response.time + "</i>" + "<a style=\"float: right;\" href=\"" + response.ogUrl + "\" target=\"_blank\"> see all alerts↗ </a>"
response.data.forEach(function (alert, i) {
var row = table.insertRow(i + 1);
row.setAttribute("id", alert.id)
row.innerHTML = rowText
.replace("{message}", alert.message)
.replace("{count}", alert.count)
.replace(/{priority}/g, alert.priority)
});
} else {
var infoText = document.getElementById("info-text")
infoText.style.removeProperty("padding")
infoText.innerHTML = "<i>Last updated @ " + response.time + "</i>" + "<a style=\"float: right;\" href=\"" + response.ogUrl + "\" target=\"_blank\"> see all alerts↗ </a>" +
"<p style=\"text-align:center\"> There are no alerts. 🎉</p>"
}
}
});

Expand Down

0 comments on commit c8c31b4

Please sign in to comment.