-
-
Notifications
You must be signed in to change notification settings - Fork 137
Added browser storage handling as seperate extension #1025
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8899ff5
feat(storage-handling): added handling of localStorage and sessionSto…
a055f94
Merge branch 'macbre:devel' into add-storage-handling
kevinkammleiter 7d88833
feat(storage-handling): added tests for storage injection
7db739f
feat(program.js): changed wording of added program option
fe3129c
Merge branch 'macbre:devel' into add-storage-handling
kevinkammleiter 08b562b
Merge branch 'add-storage-handling' of https://github.com/EXXETA/phan…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /** | ||
| * Support for session-/localStorage injection. | ||
| */ | ||
| "use strict"; | ||
|
|
||
| module.exports = function (phantomas) { | ||
| const SESSION_STORAGE = "session-storage", | ||
| LOCAL_STORAGE = "local-storage"; | ||
|
|
||
| phantomas.on("init", async (page) => { | ||
| let sessionStorage = phantomas.getParam(SESSION_STORAGE, false); | ||
| let localStorage = phantomas.getParam(LOCAL_STORAGE, false); | ||
|
|
||
| // Mapping given "storage-string" to json object if string has been given | ||
| if (typeof sessionStorage == "string" || sessionStorage instanceof String) { | ||
| sessionStorage = parseStorage(sessionStorage); | ||
| } | ||
|
|
||
| if (typeof localStorage == "string" || localStorage instanceof String) { | ||
| localStorage = parseStorage(localStorage); | ||
| } | ||
|
|
||
| if (sessionStorage) { | ||
| phantomas.log( | ||
| "Injecting sessionStorage: %j", | ||
| JSON.stringify(sessionStorage) | ||
| ); | ||
| await injectStorage(page, sessionStorage, SESSION_STORAGE); | ||
| } | ||
| if (localStorage) { | ||
| phantomas.log("Injecting localStorag: %j", JSON.stringify(localStorage)); | ||
| await injectStorage(page, localStorage, LOCAL_STORAGE); | ||
| } | ||
| }); | ||
|
|
||
| function parseStorage(storageString) { | ||
| // --sessionStorage='bar=foo;domain=url' | ||
| // --localStorage='bar=fooLocal;domain=urlLocal' | ||
| var storageMap = {}; | ||
| storageString.split(";").forEach(function (singleEntry) { | ||
| var entryKeyValue = singleEntry.split("="); | ||
| storageMap[entryKeyValue[0]] = entryKeyValue[1]; | ||
| }); | ||
| return storageMap; | ||
| } | ||
|
|
||
| /** | ||
| * Inject the given storage into the specified page storage. | ||
| * Either localStorage or sessionStorage | ||
| * | ||
| * @param {Page} page in which page the storage should be injected | ||
| * @param {Object} storage the JSON object consisting of the storage keys and values | ||
| * @param {string} storageType either localStorage or sessionStorage | ||
| */ | ||
| async function injectStorage(page, storage, storageType) { | ||
| if (!page || !storage || !storageType) { | ||
| return; | ||
| } | ||
|
|
||
| /* istanbul ignore next */ | ||
| await page.evaluateOnNewDocument( | ||
| (storage, storageType, SESSION_STORAGE, LOCAL_STORAGE) => { | ||
| const keys = Object.keys(storage); | ||
| const values = Object.values(storage); | ||
| if (storageType === SESSION_STORAGE) { | ||
| for (let i = 0; i < keys.length; i++) { | ||
| sessionStorage.setItem(keys[i], values[i]); | ||
| } | ||
| } | ||
| if (storageType === LOCAL_STORAGE) { | ||
| for (let i = 0; i < keys.length; i++) { | ||
| localStorage.setItem(keys[i], values[i]); | ||
| } | ||
| } | ||
| }, | ||
| storage, | ||
| storageType, | ||
| SESSION_STORAGE, | ||
| LOCAL_STORAGE | ||
| ); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <body> | ||
| <script> | ||
| console.log('Got local-storage: ' + JSON.stringify(window.localStorage)); | ||
| // for unit tests | ||
| (function(phantomas) { | ||
| phantomas.setMetric('local-storage', JSON.stringify(window.localStorage)); | ||
| })(window.__phantomas); | ||
| </script> | ||
| </body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <body> | ||
| <script> | ||
| console.log('Got session-storage: ' + JSON.stringify(sessionStorage)); | ||
| // for unit tests | ||
| (function(phantomas) { | ||
| phantomas.setMetric('session-storage', JSON.stringify(sessionStorage)); | ||
| })(window.__phantomas); | ||
| </script> | ||
| </body> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!