Skip to content

Instantly share code, notes, and snippets.

@snowman
Last active April 22, 2020 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snowman/3a7b2e39f278b7768e25c1f6ee0b6af1 to your computer and use it in GitHub Desktop.
Save snowman/3a7b2e39f278b7768e25c1f6ee0b6af1 to your computer and use it in GitHub Desktop.
scripts
COLUMNS = 5
// requires lodash
a = Array.from(document.querySelectorAll("table th, table td")).map(th => th.innerText.trim())
r = _.chunk(a, COLUMNS).map(([c1, c2, c3, c4, c5]) => `|${c1}|${c2}|${c3}|`).join("\n")
copy(r)
_copy = copy
SEARCH_PATTERN = 'java'
chrome.storage.local.get(null, (pages) => process(pages))
byAscTime = (a, b) => a.time - b.time
byDescTime = (a, b) => b.time - a.time
uniq = lst => [...new Set(lst)]
process = (pages) => {
keys = Object.keys(pages)
found_pages = keys
.filter((key) => {
page = pages[key]
return new RegExp(SEARCH_PATTERN, "ig").test(page.text)
})
.map(key => pages[key])
.sort(byAscTime)
report = uniq(found_pages.map(format_page)).join('\n')
console.log(report)
_copy(report)
}
format_page = ({ title, url }) => `[[${url}][${title}]]`
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
// https://docs.microsoft.com/en-us/windows/win32/menurc/menus
// https://stackoverflow.com/questions/2862883/is-it-possible-to-enumerate-menu-items-of-other-programs-on-windows
// https://blog.csdn.net/greless/article/details/83143465
// https://blog.csdn.net/seele52/article/details/17542265
// Check
// https://stackoverflow.com/questions/18589385/retrieve-list-of-menu-items-in-windows-in-c
// https://blog.csdn.net/liangzhonglin/article/details/5603792
// https://stackoverflow.com/questions/20012990/how-can-i-access-menu-items-of-a-given-program?noredirect=1&lq=1
// http://zetcode.com/gui/winapi/menus/
// idea
// display tree like `tree` commands, thinking menu as file inside folder.
#include <stdio.h>
#include <windows.h>
TCHAR _szMenuString[128] = {NULL};
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message and exit the process
LocalFree(lpMsgBuf);
printf("Error, %d: %s", dw, lpMsgBuf);
ExitProcess(dw);
}
void getMenuString(HMENU hMenu, int nPos) {
GetMenuStringA(hMenu, nPos, _szMenuString, 128, MF_BYPOSITION);
}
void printMenu(HMENU hMenu, int nPos, int level) {
TCHAR fmt_string[128] = {NULL};
for (int col=0; col<level*2; col++)
fmt_string[col] = ' ';
getMenuString(hMenu, nPos);
UINT menuId = GetMenuItemID(hMenu, nPos);
if (menuId != -1) {
strcpy(fmt_string + level*2, "id, name: %5d, %s\n");
printf(fmt_string, menuId, _szMenuString);
} else {
strcpy(fmt_string + level*2, "menu: %s\n");
printf(fmt_string, _szMenuString);
}
}
int main(int argc, char *argv[]) {
LPCTSTR lpClassName = argc-1 > 0 ? argv[1] : "notepad";
HWND hWnd = FindWindowA(lpClassName, NULL);
printf("hWnd: %d\n", hWnd);
HMENU hmenuMain = GetMenu(hWnd);
printf("\nhmenuMain: %d\n", hmenuMain);
int menuCount = GetMenuItemCount(hmenuMain);
if (menuCount == -1) {
ErrorExit(TEXT("GetMenuItemCount"));
}
for (int nPos=0; nPos<menuCount; nPos++) {
printMenu(hmenuMain, nPos, 0);
HMENU hSubMenu = GetSubMenu(hmenuMain, nPos);
int subMenuItemCount = GetMenuItemCount(hSubMenu);
if (subMenuItemCount == -1) {
ErrorExit(TEXT("GetMenuItemCount"));
}
for (int subMenu_nPos=0; subMenu_nPos<subMenuItemCount; subMenu_nPos++) {
printMenu(hSubMenu, subMenu_nPos, 1);
}
}
UINT menuId = 65; // Pickup your menuId
// PostMessage(hWnd, WM_COMMAND, menuId, 0);
return 0;
}
#include <stdio.h>
//
// o n
// ---------------------->
// | . . . . .
// | . . . . .
// | . . . . .
// | . . . . .
// | . . . . .
// | n
// > n
//
//
// condition:
// 1. the outer loop: step <= n*n
// 2. the inner loop (direction loop):
// 1. element location should be inside matrix
// row, col >= 0 && row, col < n
// 2. next element "matrix[row][col]" on current direction is empty
// row+1 < n --> row<n-1
// col+1 < n --> col<n-1
// row-1 >= 0 --> row>=1 --> row>0
// col-1 >= 0 --> col>=1 --> col>0
int main() {
int n, row = 0, col = 0, matrix[15][15] = {0}, step = 1;
printf("Enter n (n<=15): ");
scanf("%d", &n);
while (step <= n*n) {
// right
while(matrix[row][col] == 0) {
matrix[row][col] = step++;
col < n-1 && matrix[row][col+1] == 0 && ++col;
}
row++; // down one
// down
while(matrix[row][col] == 0) {
matrix[row][col] = step++;
row < n-1 && matrix[row+1][col] == 0 && ++row;
}
col--; // left one
// left
while(matrix[row][col] == 0) {
matrix[row][col] = step++;
col > 0 && matrix[row][col-1] == 0 && col--;
}
row--; // up one
// up
while(matrix[row][col] == 0) {
matrix[row][col] = step++;
row > 0 && matrix[row-1][col] == 0 && row--;
}
col++; // right one
}
for (row = 0; row < n; row++) {
for (col = 0; col < n; col++) {
printf("%5d", matrix[row][col]);
}
printf("\n");
}
return 0;
}
(setq url-proxy-services
'(
("http" . "127.0.0.1:7890")
("https" . "127.0.0.1:7890")
))
import random
import string
"".join(random.sample(list(string.ascii_uppercase), 26))
shuff = lambda s, steps: s[steps:] + s[0:steps]
if __name__ == '__main__':
import string
print(shuff(string.ascii_uppercase, 19))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment