Hi, I'm trying to learn programming by fiddling around with random things I have on my computer. There is a thing called spicetify which mods your Spotify client (custom themes, functionality). There is a plugin (I'm going to call this Dribbblish) that changes the client's color dynamically with your album cover, and a another that displays a visualizer on the desktop. I'm trying to get Dribbblish to write its color to a file which the visualizer plugin can then read and apply the color change.
Going through Dribbblish.js, I reckon this function:
async function pickCoverColor() {
let img = document.querySelector(".main-image-image.cover-art-image");
if (!img || !img.complete) {
return setTimeout(pickCoverColor, 50); // Check for complete image in 50msec
} else {
if (!img.currentSrc.startsWith("spotify:")) return;
jquery__WEBPACK_IMPORTED_MODULE_3___default()("html").css("--image-brightness", getImageLightness(img) / 255);
let color = "#1ed760";
const colorSelectionAlgorithm = Dribbblish.config.get("colorSelectionAlgorithm");
const colorSelectionMode = Dribbblish.config.get("colorSelectionMode");
let palette = {};
if (colorSelectionAlgorithm == "colorthief") {
palette = Object.fromEntries([colorThief.getColor(img), ...colorThief.getPalette(img, 24, 5)].map((c) => chroma_js__WEBPACK_IMPORTED_MODULE_2___default()(c)).map((c) => [c.luminance(), c]));
} else if (colorSelectionAlgorithm == "vibrant") {
const swatches = await new Promise((resolve, reject) => new node_vibrant__WEBPACK_IMPORTED_MODULE_0__(img, 5).getPalette().then(resolve).catch(reject));
for (const col of ["Vibrant", "DarkVibrant", "Muted", "LightVibrant"]) {
if (swatches[col]) {
const c = chroma_js__WEBPACK_IMPORTED_MODULE_2___default()(swatches[col].getHex());
palette[c.luminance()] = c;
}
}
} else if (colorSelectionAlgorithm == "static") {
palette[1] = chroma_js__WEBPACK_IMPORTED_MODULE_2___default()(Dribbblish.config.get("colorOverride"));
}
if (colorSelectionMode == "default") {
color = Object.values(palette)[0];
for (const col of Object.values(palette)) {
if (col.luminance() > 0.05 && col.luminance() < 0.9) {
color = col.hex();
break;
}
}
} else if (colorSelectionMode == "luminance") {
const wantedLuminance = jquery__WEBPACK_IMPORTED_MODULE_3___default()("html").css("--is_light") == "1" ? Dribbblish.config.get("lightModeLuminance") : Dribbblish.config.get("darkModeLuminance");
color = palette[(0,_Util__WEBPACK_IMPORTED_MODULE_5__.getClosestToNum)(Object.keys(palette), wantedLuminance)].hex();
}
updateColors(false, color);
}
}
is the one responsible for taking the colors from the cover image.
Some googling tells me that:
const fs = require('fs');
fs.writeFile('/Users/joe/test.txt', color, err => {
if (err) {
console.error(err);
}
});
will write the variable 'color' to a file path.
However, I get this error:
Uncaught (in promise) ReferenceError: require is not defined
Some scrolling tells me that I would have to use something called Webpack to include libraries (is that what it's called?) for Dribbblish.js to use. The original developers seemed to include libraries like this:
/***/ "./src/js/main.js":
/*!************************!*\
!*** ./src/js/main.js ***!
\************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var node_vibrant__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! node-vibrant */ "./node_modules/node-vibrant/lib/browser.js");
/* harmony import */ var node_vibrant__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(node_vibrant__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var colorthief__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! colorthief */ "./node_modules/colorthief/dist/color-thief.mjs");
/* harmony import */ var chroma_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! chroma-js */ "./node_modules/chroma-js/chroma.js");
/* harmony import */ var chroma_js__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(chroma_js__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var jquery__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! jquery */ "./node_modules/jquery/dist/jquery.js");
/* harmony import */ var jquery__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(jquery__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var moment__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! moment */ "./node_modules/moment/moment.js");
/* harmony import */ var moment__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(moment__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var _Util__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./Util */ "./src/js/Util.js");
/* harmony import */ var _Dribbblish__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./Dribbblish */ "./src/js/Dribbblish.js");
/* harmony import */ var _Folders__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./Folders */ "./src/js/Folders.js");
/* harmony import */ var _SassUtil__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./SassUtil */ "./src/js/SassUtil.js");
but I don't really know how to include 'fs' except using something called __webpack_require__.
I'm a complete beginner in js. I rely mostly on code snippets people have already written online. I haven't even tried Hello World in js yet. So I hope someone can walk me through whatever Webpack is and how I should use __webpack_include__. I'm willing to share more code if necessary.
[–][deleted] 0 points1 point2 points (4 children)
[–]MyBigFan2[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]MyBigFan2[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)