Skip to content

Commit

Permalink
3 states with images: active, disabled, inactive
Browse files Browse the repository at this point in the history
  • Loading branch information
ddesch committed Jan 8, 2019
1 parent b667658 commit 77b13cd
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 60 deletions.
161 changes: 105 additions & 56 deletions SPRESTJSON.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* SP REST JSON, v. 0.1.5
* SP REST JSON, v. 1.0.1
*
* by Daniel Desch <danieldesch@gmx.de>
*
Expand All @@ -14,22 +14,26 @@

'use strict';

var arrURLs = [];
arrURLs.push('https://*/*_api/web/*');
arrURLs.push('https://*/*_api/Web/*');
arrURLs.push('https://*/*_api/site/*');
arrURLs.push('https://*/*_api/Site/*');
arrURLs.push('https://*/*_api/sp./*');
arrURLs.push('https://*/*_api/SP./*');

// var arrURLs = [];
// arrURLs.push('https://*/*_api/web/*');
// arrURLs.push('https://*/*_api/Web/*');
// arrURLs.push('https://*/*_api/site/*');
// arrURLs.push('https://*/*_api/Site/*');
// arrURLs.push('https://*/*_api/sp./*');
// arrURLs.push('https://*/*_api/SP./*');


var RegExpRestURL = /\/_api\/web\/|\/_api\/site\/|\/_api\/sp./i; // '/_api/web/' or '/_api/site/' or /_api/sp./i --> caseinsesitive
var RegExpOData = /odata=|odata.metadata=/i; // 'odata=' OR 'odata.metadata=' /i --> caseinsesitive
var strAcceptValue = 'application/json; odata=verbose';
var strAcceptValue = '';
//var bJSON;
var iTabId;
var objTabSettings = {};
var arrODataOptions = ['application/json;odata=verbose', 'application/json;odata=minimalmetadata', 'application/json;odata=nometadata'];
var arrODataOptions = [
'application/json;odata=verbose',
'application/json;odata=minimalmetadata',
'application/json;odata=nometadata'
];
//var iODataChosen;
var portFromCS;

Expand Down Expand Up @@ -59,6 +63,7 @@ var publicToggleIcon;
if(objTabSettings[iTabId] == undefined) {
objTabSettings[iTabId] = {};
objTabSettings[iTabId].bJSON = true;
objTabSettings[iTabId].bAcceptChanged = false;
objTabSettings[iTabId].iODataChosen = 0;
objTabSettings[iTabId].bRefreshOnChange = true;
}
Expand All @@ -79,27 +84,68 @@ var publicToggleIcon;
}

function updateIcon() {
browser.browserAction.setIcon({
path: objTabSettings[iTabId].bJSON ? {
64: "icons/64/active.png"
} : {
64: "icons/64/inactive.png"
if(objTabSettings[iTabId].bAcceptChanged) {
browser.browserAction.enable();
var strTooltip = 'Click to set options for SP REST JSON in this tab.\n\nCurrent options: ';
if(objTabSettings[iTabId].bJSON) {
var objTab = objTabSettings[iTabId];
browser.browserAction.setIcon({
path: {
16: "icons/16/active.png",
32: "icons/32/active.png",
48: "icons/48/active.png",
64: "icons/64/active.png",
128: "icons/128/active.png"
}
});
var bRefreshOnChange = objTab.bRefreshOnChange;
var stroData = arrODataOptions[objTab.iODataChosen];
strTooltip += '\nAutomatic page reload on odata change: ' + bRefreshOnChange + '\noData option: ' + stroData;
}
});
browser.browserAction.setTitle({
title: 'Click to set options for SP REST JSON\nCurrent options: ' + (objTabSettings[iTabId].bJSON? arrODataOptions[objTabSettings[iTabId].iODataChosen] : 'disabled')
});
else {
browser.browserAction.setIcon({
path: {
16: "icons/16/disabled.png",
32: "icons/32/disabled.png",
48: "icons/48/disabled.png",
64: "icons/64/disabled.png",
128: "icons/128/disabled.png"
}
});
strTooltip += 'disabled';
}

browser.browserAction.setTitle({
title: strTooltip
});
}
else {
browser.browserAction.disable();
browser.browserAction.setIcon({
path: {
16: "icons/16/inactive.png",
32: "icons/32/inactive.png",
48: "icons/48/inactive.png",
64: "icons/64/inactive.png",
128: "icons/128/inactive.png"
}
});
browser.browserAction.setTitle({
title: 'Only active when a SharePoint REST API URL is loaded'
});
}


}

function connected(p) {
portFromCS = p;

// Set initial values for the pop up
portFromCS.postMessage({
bJSON: objTabSettings[iTabId].bJSON
,arrODataOptions: arrODataOptions
,iODataChosen: objTabSettings[iTabId].iODataChosen
,bRefreshOnChange: objTabSettings[iTabId].bRefreshOnChange
bJSON: objTabSettings[iTabId].bJSON,
arrODataOptions: arrODataOptions,
iODataChosen: objTabSettings[iTabId].iODataChosen,
bRefreshOnChange: objTabSettings[iTabId].bRefreshOnChange
});

portFromCS.onMessage.addListener(function(objMessage) {
Expand All @@ -126,53 +172,56 @@ var publicToggleIcon;
browser.runtime.onConnect.addListener(connected);

// Rewrite the Accept header if needed...
function rewriteAcceptHeader(ev) {
if(objTabSettings[iTabId] == undefined) {
strAcceptValue = arrODataOptions[0];
}
else {
strAcceptValue = arrODataOptions[objTabSettings[iTabId].iODataChosen];
}
if(objTabSettings[iTabId].bJSON) {
var bAccept = false;
for (var header of ev.requestHeaders) {
if (header.name.toLowerCase() == 'accept') {
bAccept = true;
console.log('Accept header exists');
var strValue = header.value.toLowerCase();
if(strValue.indexOf('application/json') > -1) {
console.log('Accept header is application/json');
if(RegExpOData.test(strValue) == false) {
console.log('but NO odata');
header.value = strAcceptValue;
function rewriteRequestAcceptHeader(ev) {
if(ev.type === 'main_frame') {
if(RegExpRestURL.test(ev.url)) {
if(objTabSettings[iTabId] == undefined) {
strAcceptValue = arrODataOptions[0];
}
else {
strAcceptValue = arrODataOptions[objTabSettings[iTabId].iODataChosen];
}
if(objTabSettings[iTabId].bJSON) {
var bAccept = false;
for (var header of ev.requestHeaders) {
if (header.name.toLowerCase() == 'accept') {
bAccept = true;
var strValue = header.value.toLowerCase();
if(strValue.indexOf('application/json') > -1) {
if(RegExpOData.test(strValue) == false) {
header.value = strAcceptValue;
}
}
else {
header.value = strAcceptValue;
}
}
}
else {
console.log('Accept is NOT application/json');
header.value = strAcceptValue;
if(!bAccept) {
ev.requestHeaders.push({name: 'Accept', value: strAcceptValue});
}
objTabSettings[iTabId].bAcceptChanged = true;
}
}
if(!bAccept) {
console.log('There wasn\'t any accept header');
ev.requestHeaders.push({name: 'Accept', value: strAcceptValue});
else {
objTabSettings[iTabId].bAcceptChanged = false;
}
console.log('return CHANGED requestHeader');
}
else console.log('return UNCHANGED requestHeader');
updateIcon();
return { requestHeaders: ev.requestHeaders };
}

// Add rewriteAcceptHeader as a listener to onBeforeSendHeaders
// Add rewriteRequestAcceptHeader as a listener to onBeforeSendHeaders
// Make it "blocking" so we can modify the headers.
browser.webRequest.onBeforeSendHeaders.addListener(
rewriteAcceptHeader,
rewriteRequestAcceptHeader,
{
urls: arrURLs
// urls: arrURLs
urls: ['<all_urls>']
},
["blocking", "requestHeaders"]
);

// listen to tab switching
browser.tabs.onActivated.addListener(updateActiveTab);

Expand Down
Binary file added icons/128/active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/128/disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/128/inactive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/16/active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/16/disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/16/inactive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/32/active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/32/disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/32/inactive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/48/active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/48/disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/48/inactive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/64/disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 14 additions & 4 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
"description": "Modifies request header for SharePoint REST API calls to get JSON instead of XML. For best results you'll need a JSON viewer Add-on."
,"manifest_version": 2
,"name": "SP REST JSON"
,"version": "0.1.5"
,"version": "1.0.1"
,"author": "Daniel Desch"
,"homepage_url": "https://github.com/ddesch/SP-REST-JSON/tree/master/"
,"icons": {
"64": "icons/64/active.png"
"16": "icons/16/active.png",
"32": "icons/32/active.png",
"48": "icons/48/active.png",
"64": "icons/64/active.png",
"128": "icons/128/active.png"

}
,"browser_action": {
"default_icon": "icons/64/active.png"
,"default_popup": "popup/options.html"
"default_icon": {
"16": "icons/16/inactive.png",
"32": "icons/32/inactive.png",
"48": "icons/48/inactive.png",
"64": "icons/64/inactive.png",
"128": "icons/64/inactive.png"
},
"default_popup": "popup/options.html"
,"browser_style": true

}
Expand Down

0 comments on commit 77b13cd

Please sign in to comment.