Skip to content

Instantly share code, notes, and snippets.

@markdon
Last active August 26, 2022 01:26
Show Gist options
  • Save markdon/0f9911078ad67c8bef09432651bd060a to your computer and use it in GitHub Desktop.
Save markdon/0f9911078ad67c8bef09432651bd060a to your computer and use it in GitHub Desktop.
ccm js redirector
// ==UserScript==
// @name CCM URL Redirector
// @namespace http://tampermonkey.net/
// @version 1.0.0
// @description Redirects matching CCM pages to the redirector application
// @author Mark Donnellon <mdonnellon@isw.net.au>
// @match https://devconn6.internal.isw.net.au/*
// @match https://dev.onefluor.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=hclconnections.net
// @grant none
// ==/UserScript==
// root library URL. This can't be identified as a CCM URL without access to a list of fullpageWidgetID. These are different for every Library. Maybe the same as the library id.
// https://devconn6.internal.isw.net.au/communities/service/html/communitystart?communityUuid=3c73e497-2c76-4612-9d92-8d325d6e5c33#fullpageWidgetId=Waab882e8da97_4193_9066_346c6caca265
// folder URL
// https://devconn6.internal.isw.net.au/communities/service/html/communitystart?communityUuid=3c73e497-2c76-4612-9d92-8d325d6e5c33#fullpageWidgetId=Waab882e8da97_4193_9066_346c6caca265&folder=%257BC0358B82-0000-C414-891E-38375BFB6F21%257D
// file URL
// https://devconn6.internal.isw.net.au/communities/service/html/communitystart?communityUuid=3c73e497-2c76-4612-9d92-8d325d6e5c33#fullpageWidgetId=Waab882e8da97_4193_9066_346c6caca265&file=%257BE0F6B668-0000-C817-AA2C-B43EDD5AD20F%257D
(async function () {
"use strict";
const TAG = 'ccm-redirector';
const {
pathname,
hash,
searchParams
} = new URL(window.location.href);
if (pathname.endsWith("communitystart")) {
const hashParams = new URLSearchParams(hash.substring(1));
const communityUuid = searchParams.get('communityUuid');
const fullpageWidgetId = hashParams.get('fullpageWidgetId');
const file = hashParams.get('file');
const folder = hashParams.get('folder');
console.log(`${TAG} | communityUuid`, communityUuid);
console.log(`${TAG} | fullpageWidgetId`, fullpageWidgetId);
console.log(`${TAG} | file`, file);
console.log(`${TAG} | folder`, folder);
const ccmIdExp = /%[0-9A-F-]{38}%7D/
console.log(`${TAG} | file?.match(ccmIdExp)`, file?.match(ccmIdExp));
console.log(`${TAG} | folder?.match(ccmIdExp)`, folder?.match(ccmIdExp));
if (communityUuid && fullpageWidgetId && (file?.match(ccmIdExp) || folder?.match(ccmIdExp))) {
// TODO: Test if file/folder is still in CCM.
// Method 1. Make a fetch to the file's XML entry. OK response means it exists.
// file %7B60FB4F68-0D00-C792-8D89-9E280ADCC624%7D
// communityUuid accbcb5c-3dc3-45c0-bce2-a3f6e36e6bfa
// fullpageWidgetId W1ecfef9aab53_40ae_81ee_2c858f815f28
// This would be the URL to check if CCM file exists
// https://lassus.internal.isw.net.au/dm/atom/library/D5A5ED06-47A8-4E65-87D6-6165C87A02E5%3B23D96280-11EE-4608-A966-132DFF45E73B/document/%7B60FB4F68-0D00-C792-8D89-9E280ADCC624%7D/entry
// I can't see how to convert communityUuid/fullpageWidgetId in to the library id in the above URL.
// Check if folder exists
// https://lassus.internal.isw.net.au/dm/atom/library/D5A5ED06-47A8-4E65-87D6-6165C87A02E5%3B23D96280-11EE-4608-A966-132DFF45E73B/folder/%7BC035B668-0000-C518-A2B7-A52B2D062E65%7D/entry
// Method 2. Use a mutation listener to watch for changes to the DOM until it can detect that either
// - The file details have rendered
// - An error has rendered
// - Do redirect on error
const redirect = new URL(`${window.location.origin}/ccm-redirect`);
// TODO: Remove strange characters from file/folder ID before redirect?
redirect.searchParams.set("communityUuid", communityUuid);
redirect.searchParams.set('library', fullpageWidgetId);
redirect.searchParams.set("file", file);
redirect.searchParams.set("folder", folder);
console.log(`${TAG} | redirect`, redirect.href);
// window.location.assign(redirect);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment