Skip to content

Commit

Permalink
js: make js code use separate shape and color css
Browse files Browse the repository at this point in the history
  • Loading branch information
paveyry committed Aug 22, 2019
1 parent 76122b9 commit bdbf6ea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
9 changes: 5 additions & 4 deletions generate_patch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ mkdir -p out/slacktheme

generateFiles() {
cat js/plugin.js \
| sed "s/CSSURL/$1/g" \
| sed "s/CSSSHAPEURL/$1/g" \
| sed "s/CSSCOLORURL/$2/g" \
> "$PLUGINGEN"
echo "document.addEventListener(\"DOMContentLoaded\", function() {" > "$PLUGINGEN2"
cat "$PLUGINGEN" >> "$PLUGINGEN2"
echo "});" >> "$PLUGINGEN2"
[ -z "$ASARFILE" ] || sed -e "10r $PLUGINGEN2" $DEFAULTINIT > "$DIR/renderer/init.js"
cat js/gmonkeyscript_template.js "$PLUGINGEN" > "$2/gmonkeyscript.js"
cat js/gmonkeyscript_template.js "$PLUGINGEN" > "$3/gmonkeyscript.js"
[ -z "$ASARFILE" ] || asar pack "$DIR" electron.asar
[ -z "$ASARFILE" ] || mv electron.asar "$2/electron.asar"
[ -z "$ASARFILE" ] || mv electron.asar "$3/electron.asar"
}

generateFiles "https:\/\/raw.githubusercontent.com\/paveyry\/Slack-Theme-for-Hangouts-Chat\/master\/custom.css" "out/slacktheme"
generateFiles "https:\/\/raw.githubusercontent.com\/paveyry\/Slack-Theme-for-Hangouts-Chat\/master\/css\/shape.css" "https:\/\/raw.githubusercontent.com\/paveyry\/Slack-Theme-for-Hangouts-Chat\/master\/css\/color_slack.css" "out/slacktheme"

rm -rf "$DIR" "$DEFAULTINIT" "$PLUGINGEN" "$PLUGINGEN2"
63 changes: 33 additions & 30 deletions js/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,39 @@ for (i = 0; i < topics.length; i++) {
}
insertionQ('.cZICLc').every(linkFunction);


// Then get its webviews
let webviews = document.querySelectorAll("webview");

// Fetch our CSS in parallel ahead of time
const cssPath = 'CSSURL';
let cssPromise = fetch(cssPath).then(response => response.text());

// Insert a style tag into the wrapper view
cssPromise.then(css => {
let s = document.createElement('style');
s.type = 'text/css';
s.innerHTML = css;
document.head.appendChild(s);
});

// Wait for each webview to load
webviews.forEach(webview => {
webview.addEventListener('ipc-message', message => {
if (message.channel == 'didFinishLoading')
// Finally add the CSS into the webview
cssPromise.then(css => {
let script = `
let s = document.createElement('style');
s.type = 'text/css';
s.id = 'slack-custom-css';
s.innerHTML = \`${css}\`;
document.head.appendChild(s);
`
webview.executeJavaScript(script);
})
});
});
function loadCSS(cssPath) {
// Fetch our CSS in parallel ahead of time
let cssPromise = fetch(cssPath).then(response => response.text());

// Insert a style tag into the wrapper view
cssPromise.then(css => {
let s = document.createElement('style');
s.type = 'text/css';
s.innerHTML = css;
document.head.appendChild(s);
});

// Wait for each webview to load
webviews.forEach(webview => {
webview.addEventListener('ipc-message', message => {
if (message.channel == 'didFinishLoading')
// Finally add the CSS into the webview
cssPromise.then(css => {
let script = `
let s = document.createElement('style');
s.type = 'text/css';
s.id = 'slack-custom-css';
s.innerHTML = \`${css}\`;
document.head.appendChild(s);
`
webview.executeJavaScript(script);
})
});
});
}

loadCSS('CSSSHAPEURL');
loadCSS('CSSCOLORURL');

0 comments on commit bdbf6ea

Please sign in to comment.