Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for URNs from additional systems #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions extension/config/systems.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"mu": {
"urnTypeRegex": "urn:dr:mu:([^:]+):",
"typePaths": {
"programcard": "programcard/{urn}",
"bundle": "bundle/{urn}",
"manifest": "internalmanifest/{urn}"
},
"environmentBaseUrls": {
"dev": "http://mudev/api/",
"test": "http://mutst/api/",
"preprod": "http://mupre/api/",
"prod": "http://mu/api/"
}
},
"mu-external": {
"urnTypeRegex": "urn:dr:mu:([^:]+):",
"typePaths": {
"programcard": "programcard/{urn}",
"bundle": "bundle/{urn}",
"manifest": "manifest/{urn}"
},
"environmentBaseUrls": {
"dev": "http://mudev/api-external/",
"test": "http://mutst/api-external/",
"preprod": "http://mupre/api-external/",
"prod": "https://www.dr.dk/mu/"
}
},
"mu-online": {
"urnTypeRegex": "urn:dr:mu:([^:]+):",
"typePaths": {
"programcard": "api/1.4/programcard/{urn}",
"bundle": "api/1.4/bundle/{urn}",
"manifest": "api/1.4/manifest/{urn}"
},
"environmentBaseUrls": {
"dev": "http://mudev/mu-online/",
"test": "https://www.dr.dk/mu-online-beta/",
"preprod": "https://www.dr.dk/mu-online-stage/",
"prod": "https://www.dr.dk/mu-online/"
}
},
"radio": {
"urnTypeRegex": "urn:dr:radio:([^:]+):",
"typePaths": {
"category": "v2/categories/{urn}",
"channel": "v2/channels/{urn}",
"episode": "v2/episodes/{urn}",
"series": "v2/series/{urn}",
"image": "v2/images/{urn}?w=720&ratio=16:9",
"list": "v2/lists/{urn}"
},
"environmentBaseUrls": {
"dev": "https://api-dev.dr.dk/radio/",
"test": "https://api-test.dr.dk/radio/",
"preprod": "https://api-preprod.dr.dk/radio/",
"prod": "https://api.dr.dk/radio/"
}
},
"ocs": {
"urnTypeRegex": "urn:dr:ocs:(?:[^:]+):([^:]+):",
"typePaths": {
"content": "contents/{urn}",
"publication": "publications/{urn}"
},
"environmentBaseUrls": {
"dev": "https://api-dev.dr.dk/ocs/",
"test": "https://api-test.dr.dk/ocs/",
"preprod": "https://api-preprod.dr.dk/ocs/",
"prod": "https://api.dr.dk/ocs/"
}
},
"music": {
"urnTypeRegex": "urn:dr:music:([^:]+):",
"typePaths": {
"artist": "api/artists/{urn}",
"composition": "api/compositions/{urn}",
"release": "api/releases/{urn}",
"track": "api/tracks/{urn}"
},
"environmentBaseUrls": {
"dev": "https://api-test.dr.dk/music-api/",
"test": "https://api-test.dr.dk/music-api/",
"preprod": "https://api.dr.dk/music-api/",
"prod": "https://api.dr.dk/music-api/"
}
},
"odacache": {
"urnTypeRegex": "urn:dr:odacache:(?:[^:]+):([^:]+):",
"typePaths": {
"videoassetmessage": "api/assets/{urn}",
"audioassetmessage": "api/assets/{urn}",
"videoclipassetmessage": "api/assets/{urn}",
"audioclipassetmessage": "api/assets/{urn}"
},
"environmentBaseUrls": {
"dev": "https://api-test.dr.dk/odacache/",
"test": "https://api-test.dr.dk/odacache/",
"preprod": "https://api.dr.dk/odacache/",
"prod": "https://api.dr.dk/odacache/"
}
}
}
93 changes: 80 additions & 13 deletions extension/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,68 @@
TYPE_OBJECT = 3,
TYPE_ARRAY = 4,
TYPE_BOOL = 5,
TYPE_NULL = 6,
drUrnRegExp = /(.+\/(mu|muonline|api)\/|http:\/\/localhost(:\d+|)\/)/,
drUrnTypeRegExp = /urn:dr:mu:(.+):/
TYPE_NULL = 6
;

const drUrlRegex = /^http(?:s)?:\/\/(?:[^/]*?([^.-]+)(?:\.gcp)?\.dr\.dk|www\.dr\.dk\/mu-online-([^/]+)|(?:mu(dev|tst|pre)?)|(localhost)(?::\d+)?)\//;
const muSystemRegex = /^http(?:s)?:\/\/[^/]+\/([^/]+?)(?:-(?:beta|stage))?\//
const drUrnSystemRegex = /urn:dr:([^:]+):/;

const systemsConfigUrl = chrome.runtime.getURL('/config/systems.json');
let drSystems = null;

fetch(systemsConfigUrl)
.then(
function(response) {
response.json().then(function(data) {
drSystems = data;

for (const systemName in drSystems) {
drSystems[systemName].urnTypeRegex = new RegExp(drSystems[systemName].urnTypeRegex);
}
});
}
);

var senderUrl = "";

// Utility functions
function getEnvironmentName(url) {
let result = drUrlRegex.exec(url);
let environment = result[1] || result[2] || result[3] || result[4];

switch (environment) {
case "localhost":
return "localhost";
case "dev":
return "dev";
case "beta":
case "tst":
case "test":
return "test";
case "stage":
case "pre":
case "preprod":
return "preprod";
default:
return "prod";
}
}

function getMuSystemName(url) {
let result = muSystemRegex.exec(url);

switch (result[1]) {
case "mu-online":
return "mu-online";
case "mu":
case "api-external":
return "mu-external";
default:
return "mu";
}
}

function removeComments (str) {
str = ('__' + str + '__').split('');
var mode = {
Expand Down Expand Up @@ -273,16 +327,29 @@
innerStringA.innerText = escapedString ;
innerStringEl.appendChild(innerStringA) ;
}
else if (value[0] === 'u' && value.substring(0, 10) === 'urn:dr:mu:' && drUrnRegExp.test(senderUrl)) {
var innerStringA = document.createElement('A') ;
var urnType = drUrnTypeRegExp.exec(value)[1];
if (urnType == "manifest") {
urnType = "internalmanifest";
}
innerStringA.href = drUrnRegExp.exec(senderUrl)[0]+ urnType + "/" + value ;
innerStringA.innerText = escapedString ;
innerStringEl.appendChild(innerStringA) ;
}
else if (value[0] === 'u' && value.substring(0, 7) === 'urn:dr:' && drUrlRegex.test(senderUrl)) {
var hasLinkCreationSucceeded = false;
var systemName = drUrnSystemRegex.exec(value)[1];
if (systemName == "mu") {
systemName = getMuSystemName(senderUrl);
}
var system = drSystems[systemName];
if (system !== undefined) {
var envName = getEnvironmentName(senderUrl);
var urnType = system.urnTypeRegex.exec(value)[1];
if ((system.environmentBaseUrls?.[envName] !== undefined || envName == "localhost") && system.typePaths?.[urnType] !== undefined) {
var innerStringA = document.createElement('A') ;
let baseUrl = envName == "localhost" ? drUrlRegex.exec(senderUrl)[0] : system.environmentBaseUrls[envName];
innerStringA.href = baseUrl + system.typePaths[urnType].replace("{urn}", value);
innerStringA.innerText = escapedString ;
innerStringEl.appendChild(innerStringA) ;
hasLinkCreationSucceeded = true;
}
}
if (!hasLinkCreationSucceeded) {
innerStringEl.innerText = escapedString;
}
}
else {
innerStringEl.innerText = escapedString;
}
Expand Down
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "JSON Formatter w. DR urn support",
"version": "0.4",
"version": "0.5",
"manifest_version": 2,
"description": "Makes JSON easy to read. Open source.",
"homepage_url": "https://github.com/drdk/json-formatter",
Expand Down