Created
February 9, 2023 08:00
-
-
Save BlackHole1/75059286c4be110c6cbfb957ed1e02a3 to your computer and use it in GitHub Desktop.
oomol github sign up and sign in - electron
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP --> | |
<link href="./styles.css" rel="stylesheet"> | |
<title>Hello World!</title> | |
</head> | |
<body> | |
<h1>Hello World!</h1> | |
We are using Node.js <span id="node-version"></span>, | |
Chromium <span id="chrome-version"></span>, | |
and Electron <span id="electron-version"></span>. | |
<button onClick="openWindow()">Login Github</button> | |
<!-- You can also require other files to run in this process --> | |
<script src="./renderer.js"></script> | |
</body> | |
</html> |
This file contains 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
const {app, BrowserWindow, session, ipcMain} = require('electron') | |
const path = require('path') | |
function createWindow () { | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
preload: path.join(__dirname, 'preload.js') | |
} | |
}) | |
mainWindow.loadFile('index.html') | |
mainWindow.webContents.openDevTools() | |
} | |
app.whenReady().then(() => { | |
createWindow() | |
app.on('activate', function () { | |
if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
}) | |
ipcMain.handle("get-cookie", async (_event, key) => { | |
return await session.defaultSession.cookies.get({ | |
domain: ".oomol.dev", | |
name: key, | |
}) | |
.then(results => { | |
if (results.length === 0) { | |
return null; | |
} | |
if (results[0].httpOnly) { | |
return null; | |
} | |
return results[0].value; | |
}) | |
.catch(err => { | |
console.error(err); | |
return null; | |
}) | |
}) | |
ipcMain.handle("remove-cookie", async (_event, key) => { | |
return await session.defaultSession.cookies.remove("https://oomol.dev/", key); | |
}) | |
session.defaultSession.cookies.on("changed", (_event, cookie) => { | |
if (cookie.domain === ".oomol.dev" && cookie.sameSite !== "no_restriction") { | |
session.defaultSession.cookies.set({ | |
...cookie, | |
url: "https://oomol.dev/", | |
sameSite: "no_restriction", | |
}) | |
} | |
}) | |
}) | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') app.quit() | |
}) |
This file contains 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
{ | |
"name": "solid-giants-fix-3a2gk", | |
"productName": "solid-giants-fix-3a2gk", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "Black-Hole<158blackhole@gmail.com>", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "22.0.0-beta.6" | |
} | |
} |
This file contains 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
// preload with contextIsolation enabled | |
const { contextBridge, ipcRenderer } = require('electron') | |
contextBridge.exposeInMainWorld('cookie', { | |
get: async (key) => { | |
return await ipcRenderer.invoke("get-cookie", key) | |
}, | |
remove: async(key) => { | |
return await ipcRenderer.invoke("remove-cookie", key) | |
} | |
}) | |
window.addEventListener('DOMContentLoaded', () => { | |
const replaceText = (selector, text) => { | |
const element = document.getElementById(selector) | |
if (element) element.innerText = text | |
} | |
for (const type of ['chrome', 'node', 'electron']) { | |
replaceText(`${type}-version`, process.versions[type]) | |
} | |
}) | |
This file contains 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
function openWindow() { | |
const url = "https://api.oomol.dev/v1/auth/github/redirect"; | |
// 如果想让窗口居中,见: https://stackoverflow.com/a/16861050/6596777 | |
const popup = window.open(url, "_blank", "width=800,height=750"); | |
window.a = popup | |
const loginGithub = new Promise((r, j) => { | |
const id = setInterval(async () => { | |
const authStatus = await window.cookie.get("authStatus"); | |
if (authStatus !== null) { | |
clearInterval(id); | |
popup.close(); | |
await window.cookie.remove("authStatus"); | |
return authStatus === "succeed" ? r() : j(new Error("login failed")); | |
} | |
}, 50); | |
}); | |
loginGithub | |
.then(() => { | |
console.log("跳转到登录后的页面") | |
}) | |
.catch((error) => { | |
console.log("登录失败"); | |
}) | |
} |
This file contains 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
/* styles.css */ | |
/* Add styles here to customize the appearance of your app */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment