Comments (4)
Hey @schmijos, great to know your company is putting Witchcraft to good use.
One easy way I see is to add that functionality to your own scripts, no need to fork Witchcraft at all. Let me give you an example:
function run() {
let userName = window.localStorage.getItem("user-name");
if (!userName) {
userName = window.prompt("Please type in your name");
window.localStorage.setItem("user-name", userName);
}
document.querySelector('input[type="text"]').value = "Hello, " + userName;
}
window.addEventListener("load", run);
Name it google.com.js
and then head to google.com to see it working. You should see something like this:
The idea here is to use the local storage to store your user's configs. The first time the user runs it, they get prompted to type in their name, since the configuration key user-name
does not exist yet. The script then stores it and proceeds to use the information.
The next time the page gets loaded, the prompt will not appear anymore, since the information will be found in the local storage.
What do you think?
from witchcraft.
Since our people can use git, I ended up with the following _config.js
:
let config = {};
try {
eval(`
config =
// @include .config.json
;
`);
}
catch(err) {
throw 'There is something wrong with your configuration. Did you create a file called ".config.json"?';
}
function callIfEnabled(configKey, callback) {
if(config[configKey]) {
callback();
}
}
This also enables me to do my own error handling (e.g. sending a report to Sentry).
The config is then used in each file like that:
// @include _config.js
callIfEnabled('sentry.io', function() {
…
});
from witchcraft.
Cool! Thank you very much for your input. I already had a similar idea, but rejected it because you would have to include this piece of checking-code in all the scripts, right? Is there a possibility to use partials? Maybe to include a config file loaded from the same web server which is serving the assets anyways?
from witchcraft.
Actually, it is possible 🎉
There's an @include
command you can use exactly for that. Say you create a use configuration script named user-config.js
:
function loadUserConfiguration() {
// ToDo get it from local storage
}
You can then include it in your scripts like this:
// @include user-config.js
const userConfig = loadUserConfiguration();
You can use @include
anywhere in your code, not just at the beginning. You can also include as many scripts as you want and those scripts can include others recursively. Just remember to always put it in a single line comment.
Alternatively, you could also put it in _global.js
, which a special script that gets loaded for every page, but @include
probably makes more sense in this case.
from witchcraft.
Related Issues (20)
- Quick item... code example on your homepage HOT 1
- Cannot assign variables to the window or document scope? HOT 1
- Google analytics is causing couples of issues HOT 11
- The js and css modification is not being picked up by the extension without reloading the page HOT 2
- Quick question: Is it possible to match chrome extensions (i.e. options pages or the LastPass vault)? HOT 2
- question: how can I include resourse, for example: image HOT 3
- Background being unloaded by Chrome due to inactivity
- Question: Disable / unload scripts from running -- str.replace HTML before page is loaded ? HOT 2
- Cannot remove once added script even after deleting file HOT 2
- Support for glob matching HOT 3
- Wildcard support? HOT 3
- File path resolution? HOT 8
- add locally stored custom css/js HOT 1
- Files are still being cached HOT 2
- The script list has not been loaded HOT 5
- How to get a reference to the page level global scope? HOT 1
- Chrome doesnt remember the host url
- Very Basic Question
- Chrome Web Server Alternatives HOT 4
- check if scripts exist (v3 branch)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from witchcraft.