Skip to content

Commit

Permalink
Various tweaks to make Chrome packaged apps run smoothly
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-ludwig committed Mar 18, 2016
1 parent 5b1214b commit 7bf81c7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/supports/focus-in-hidden-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export default memorizeResult(() => detectFocus({

// writing the iframe's content is synchronous
_document.open();
_document.write('<input>');

This comment has been minimized.

Copy link
@ryan-ludwig

ryan-ludwig Mar 21, 2016

Author

Throws a console error, "Uncaught document.write() is not available in packaged apps."

This comment has been minimized.

Copy link
@rodneyrehm

rodneyrehm Mar 21, 2016

If your change produces the same result as the document.write() example, I'd appreciate a PR for this ;)

This comment has been minimized.

Copy link
@ryan-ludwig

ryan-ludwig Mar 21, 2016

Author

Can do :)

const input = _document.createElement('input');
_document.body.appendChild(input);
_document.close();

return _document.querySelector('input');
Expand Down
4 changes: 1 addition & 3 deletions src/supports/media/svg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@

export default 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtb'

This comment has been minimized.

Copy link
@ryan-ludwig

ryan-ludwig Mar 21, 2016

Author

Throws console error: "Refused to load plugin data from 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zd…V4dCB4PSIxMCIgeT0iMjAiIGlkPSJzdmctbGluay10ZXh0Ij50ZXh0PC90ZXh0Pjwvc3ZnPg==' because it violates the following Content Security Policy directive: "default-src 'self' blob: filesystem: chrome-extension-resource:". Note that 'object-src' was not explicitly set, so 'default-src' is used as a fallback."

This comment has been minimized.

Copy link
@rodneyrehm

rodneyrehm Mar 21, 2016

Is it possible to work around the CSP limitation by using URL.createObjectURL as shown in Cross-origin XMLHttpRequest? If this works, we could use URL.createObjectURL if available and fall back to the string when not.

Otherwise I have no experience with CSP and could not find a way to obtain the document's setting for a given directive (img-src, I guess). If this is not possible - which I assume is the case for security reasons - we could try identifying if we're running as a Chrome App and overwrite the test results that would've otherise used media/svg.js for feature detection.

again, issue or PR welcome :)

This comment has been minimized.

Copy link
@rodneyrehm

rodneyrehm Mar 23, 2016

Analyzed the impact on CSP and opened medialize#129 for discussion

+ 'G5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiBpZD0ic3ZnIj48dGV4dCB4PSIxMCIgeT0iMjAiIGlkPSJ'
+ 'zdmctbGluay10ZXh0Ij50ZXh0PC90ZXh0Pjwvc3ZnPg==';
export default '';
30 changes: 3 additions & 27 deletions src/supports/supports-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,15 @@ function readLocalStorage(key) {
// allow reading from storage to retrieve previous support results
// even while the document does not have focus
let data;

try {
data = window.localStorage && window.localStorage.getItem(key);

This comment has been minimized.

Copy link
@ryan-ludwig

ryan-ludwig Mar 21, 2016

Author

Unfortunately even within a try, any reference to window.localStorage causes chrome to throw "window.localStorage is not available in packaged apps. Use chrome.storage.local instead.".

This comment has been minimized.

Copy link
@rodneyrehm

rodneyrehm Mar 21, 2016

We could check for the availability of chrome.storage.local and use the StorageArea API if available. If that does not make sense, checking for window.chrome.app and ignoring the cache would be another option.

again, issue or PR welcome :)

This comment has been minimized.

Copy link
@rodneyrehm

rodneyrehm Mar 23, 2016

opened an issue for this: medialize#130

data = data ? JSON.parse(data) : {};
} catch (e) {
data = {};
}

data = {};
return data;
}

function writeLocalStorage(key, value) {
if (!document.hasFocus()) {
// if the document does not have focus when tests are executed, focus() may
// not be handled properly and events may not be dispatched immediately.
// This can happen when a document is reloaded while Developer Tools have focus.
try {
window.localStorage && window.localStorage.removeItem(key);
} catch (e) {
// ignore
}

return;
}

try {
window.localStorage && window.localStorage.setItem(key, JSON.stringify(value));
} catch (e) {
// ignore
}
return
}

const userAgent = typeof window !== 'undefined' && window.navigator.userAgent || '';
const userAgent = '';
const cacheKey = 'ally-supports-cache';
let cache = readLocalStorage(cacheKey);

Expand Down

1 comment on commit 7bf81c7

@rodneyrehm
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain why these changes are necessary for "Chrome packaged apps"?

Please sign in to comment.