Skip to content

Commit

Permalink
Merge pull request #218 from facets-io/bug-fixes-preview-and-misc
Browse files Browse the repository at this point in the history
Bug fixes preview and misc
  • Loading branch information
mkotsollaris authored Feb 4, 2021
2 parents 74006ab + f9a1536 commit ea39613
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 89 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
tag_name: 0.4.0
release_name: 0.4.0
tag_name: 0.4.1
release_name: 0.4.1
body: |
- Enhanced Preview feature
- Added cookie management
- Added css for disabled buttons
- Added better UX for transition-management of the popup
- Embedded mutation observer-script through codebase, adhering to Manifest v3 guidelines
- Minor UX updates and bug fixes
draft: false
prerelease: false
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@
"webpack-cli": "^4.4.0",
"zip-folder": "^1.0.0"
}
}
}
17 changes: 15 additions & 2 deletions public/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ chrome.runtime.onMessage.addListener(
});
return;
} else if (request.data === 'OPEN_PREVIEW_PAGE') {
await chrome.tabs.create({ url: request.config.url }, function (tab) {
console.log('[FACET][Background] SETTING COOKIES FOR ', request.config.url);
await chrome.tabs.create({ url: request.config.url }, async function (tab) {
console.log('[FACET][Background] SETTING COOKIES FOR ', request.config, ":", request.config);

chrome.cookies.set({
url: request.config.url,
name: `FACET_EXTENSION_DISABLE_MO`,
Expand All @@ -34,6 +35,18 @@ chrome.runtime.onMessage.addListener(
value: request.config.injectingScriptTag
});

chrome.cookies.set({
url: request.config.url,
name: `DURING_PREVIEW`,
value: "true"
});

chrome.cookies.set({
url: request.config.url,
name: `FACET_MAP_PREVIEW`,
value: JSON.stringify(request.config.facetMapPreview)
});

chrome.tabs.executeScript(tab.id, {
file: 'preview-click-content.js',
runAt: 'document_start',
Expand Down
34 changes: 0 additions & 34 deletions public/facet-extension-window-variable-content.js

This file was deleted.

153 changes: 153 additions & 0 deletions public/facet-mutation-observer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
(function () {
function getDomPath(el) {
if (!el || !isElement(el)) {
return '';
}
var stack = [];
var isShadow = false;
while (el.parentNode != null) {
var sibCount = 0;
var sibIndex = 0;
for (var i = 0; i < el.parentNode.childNodes.length; i++) {
var sib = el.parentNode.childNodes[i];
if (sib.nodeName == el.nodeName) {
if (sib === el) {
sibIndex = sibCount;
}
sibCount++;
}
}
var nodeName = el.nodeName.toLowerCase();
if (isShadow) {
nodeName += "::shadow";
isShadow = false;
}
if (sibCount > 1) {
if (sibIndex === 0) {
stack.unshift(nodeName);
} else {
stack.unshift(nodeName + ':nth-of-type(' + (sibIndex + 1) + ')');
}
} else {
stack.unshift(nodeName);
}
el = el.parentNode;
if (el.nodeType === 11) {
isShadow = true;
el = el.host;
}
}
var res = stack.slice(1).join(' > ');
return res.replace(/ /g, "");
}

/**
* Returns whether a given element in an HTML element or not
*
* @param element
* @returns {boolean}
*/
function isElement(element) {
return element instanceof Element || element instanceof HTMLDocument;
}

const keys = {
'FACET_EXTENSION_DISABLE_MO': 'FACET_EXTENSION_DISABLE_MO',
'FACET_EXTENSION_PREVIEW_TAB_ID': 'FACET_EXTENSION_PREVIEW_TAB_ID',
'FACET_EXTENSION_ALREADY_INTEGRATED': 'FACET_EXTENSION_ALREADY_INTEGRATED',
'FACET_EXTENSION_INJECTING_SCRIPT_TAG': 'FACET_EXTENSION_INJECTING_SCRIPT_TAG',
'FACET_MAP_PREVIEW': 'FACET_MAP_PREVIEW'
}

function getFacetExtensionCookie(key) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${key}=`);
if (parts.length === 2) {
return parts.pop().split(';').shift();
}
}

function extractDomainId() {
try {
let domainId = getFacetExtensionCookie(keys["FACET_EXTENSION_INJECTING_SCRIPT_TAG"]);
console.log('DOMAINid', domainId)
if (domainId) {
let splitArr = domainId.split('~');
console.log('[extractDomainId] RETURNING', splitArr[1]);
return splitArr[1];
}
return undefined;
} catch (e) {
console.log('[extractDomainId] [err]', e);
return undefined;
}
}

const globalFacetKey = 'GLOBAL-FACET-DECLARATION';
// TODO COME FROM TEMPLATE
const facetCookieData = getFacetExtensionCookie(keys["FACET_MAP_PREVIEW"]);
const data = JSON.parse(JSON.parse(facetCookieData));

let nodesToRemove = (data[window.location.pathname] || []).concat(data[globalFacetKey] || []) || [];
console.log('[PREVIEW][FACETMAP]', nodesToRemove);

/**
* Computes whether the element's path is in the nodesToRemove array
*
* @param {*} element
*/
const inHashMap = (element) => {
const domPath = getDomPath(element);
let exists = false;
nodesToRemove.forEach(path => {
if (path.includes(domPath)) {
exists = true;
return;
}
});
return exists;
}

const callback = async function (mutationsList) {
try {
for (let mutation of mutationsList) {
if (mutation && mutation.target && mutation.target.children) {
const subPathContainedInMap = inHashMap(mutation.target);
if (!subPathContainedInMap) {
continue;
}
domPathHide(mutation, mutation.target.children)
}
}
} catch (e) {
console.log('[ERROR]', e);
}
};

/**
* Recursive function that iterates among DOM children
*
* @param {*} mutation
* @param {*} mutationChildren
*/
const domPathHide = (mutation, mutationChildren) => {
if (!mutationChildren) {
return;
}
for (const child of mutationChildren) {
const childDomPath = getDomPath(child);
if (nodesToRemove.includes(childDomPath) && child.style) {
child.style.display = "none";
child.style.setProperty("display", "none", "important");
}
domPathHide(mutation, child.childNodes);
}
}

const targetNode = document;
const config = { subtree: true, childList: true, attributes: true };

const observer = new MutationObserver(callback);
observer.observe(targetNode, config);

})();
8 changes: 4 additions & 4 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Facet",
"version": "0.4.0",
"version": "0.4.1",
"description": "Facet.ninja | Deploy - Learn - Adapt",
"icons": {
"512": "facet_logo_512x512.png"
Expand All @@ -10,7 +10,7 @@
"default_icon": "facet_logo_512x512.png",
"default_popup": "popup.html"
},
"content_security_policy": "script-src 'self' https://unpkg.com; object-src 'self'",
"content_security_policy": "script-src 'self' https://api.facet.run https://unpkg.com; object-src 'self'",
"background": {
"scripts": [
"background.js"
Expand Down Expand Up @@ -38,8 +38,8 @@
}
],
"web_accessible_resources": [
"facet-extension-window-variable-content.js",
"preview-click-content.js"
"preview-click-content.js",
"facet-mutation-observer.js"
],
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxfhoAGlzfRw1Tdn5cbu9XozL65aZM+lNzvQIDCJBbXVV7sdireo6C9e0p4MmUl/VlJGeBRSQFVeh5cNIcYb0/zdoAmOlXI6vr1vaMZFWgA9dpmTUEiz2jqUV2jsTvhLUjNH8Xr0y0xs66Av2tP2Fj3+0dcSMF7op8MSyTOIimZipW5ct8ORwgXDvqhL5SaBm49rRDlwHmlmdLbdpQFbMFE5ZlM1ah9W/fHErATHsffZfXWQyVBZCOCKCYkPOHZj3wViIJbSaeVZv+XVUZl/uhPkL5Txzkb4qXricAzyyvmA3O/8XijiHjw+VpxzBIpAj7P4Bbgsal2r3VJeOC67OYwIDAQAB",
"permissions": [
Expand Down
32 changes: 19 additions & 13 deletions public/mutationObserverVariableInjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
'FACET_EXTENSION_DISABLE_MO': 'FACET_EXTENSION_DISABLE_MO',
'FACET_EXTENSION_PREVIEW_TAB_ID': 'FACET_EXTENSION_PREVIEW_TAB_ID',
'FACET_EXTENSION_ALREADY_INTEGRATED': 'FACET_EXTENSION_ALREADY_INTEGRATED',
'DURING_PREVIEW': 'DURING_PREVIEW',
'FACET_EXTENSION_PREVIEW_TAB_ID': 'FACET_EXTENSION_PREVIEW_TAB_ID',
'FACET_EXTENSION_INJECTING_SCRIPT_TAG': 'FACET_EXTENSION_INJECTING_SCRIPT_TAG'
}

function getFacetExtensionCookie(key) {
Expand All @@ -35,23 +38,26 @@
}
}

const previewId = getFacetExtensionCookie(keys["FACET_EXTENSION_PREVIEW_TAB_ID"]);
const duringPreview = getFacetExtensionCookie(keys["DURING_PREVIEW"]) === 'true';
console.log('[FACET][duringPreview]', duringPreview);

await chrome.runtime.sendMessage({
data: 'GET_CURRENT_TAB'
}, async (response) => {
// response.tabId
const val = response.tabId != previewId;
await chrome.runtime.sendMessage({
data: 'SET_COOKIE_VALUE',
config: {
url: window.location.origin,
name: 'FACET_EXTENSION_DISABLE_MO',
value: val.toString()
}
});

if (duringPreview || response.tabId.toString() === getFacetExtensionCookie(keys["FACET_EXTENSION_PREVIEW_TAB_ID"])) {

injectScript(chrome.extension.getURL('facet-mutation-observer.js'), 'body');

await chrome.runtime.sendMessage({
data: 'SET_COOKIE_VALUE',
config: {
url: window.location.origin,
name: keys["DURING_PREVIEW"],
value: 'false'
}
});
}
});

console.log("[FACET][mutationObserverVariableInjection] INJECTING facet-extension-window-variable-content.js");
injectScript(chrome.extension.getURL('facet-extension-window-variable-content.js'), 'body');
})();
8 changes: 5 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import isDevelopment from './utils/isDevelopment';

function App() {
const { enqueueSnackbar } = useSnackbar();
const { showSideBar, isPluginEnabled, setIsPluginEnabled, isDomainWhitelisted, facetMap, setFacetMap, setLoadingSideBar, setNonRolledOutFacets } = useContext(AppContext);
const { showSideBar, isPluginEnabled, setIsPluginEnabled,
isDomainWhitelisted, facetMap, setFacetMap, setLoadingSideBar,
globalFacets, setGlobalFacets, setNonRolledOutFacets } = useContext(AppContext);

// TODO potential need of refactor
chrome && chrome.runtime.onMessage && chrome.runtime.onMessage.addListener(
Expand All @@ -32,9 +34,9 @@ function App() {
return;
}
if (showSideBar) {
await updateEvents(true, facetMap, setFacetMap, enqueueSnackbar, setNonRolledOutFacets);
await updateEvents(true, facetMap, setFacetMap, enqueueSnackbar, setNonRolledOutFacets, setGlobalFacets);
} else {
await updateEvents(false, facetMap, setFacetMap, enqueueSnackbar, setNonRolledOutFacets);
await updateEvents(false, facetMap, setFacetMap, enqueueSnackbar, setNonRolledOutFacets, setGlobalFacets);
}
setLoadingSideBar(false);
}
Expand Down
Loading

0 comments on commit ea39613

Please sign in to comment.