Last active
December 24, 2015 22:23
-
-
Save kumatti1/99f285d16a761bf291fc to your computer and use it in GitHub Desktop.
Edgeでスタイルシート調整
This file contains hidden or 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
//#define UNICODE | |
#include <stdio.h> | |
#include <windows.h> | |
#include <Oleacc.h> | |
#include <stdio.h> | |
#include <Shlwapi.h> | |
#include <shobjidl.h> | |
#import <mshtml.tlb> | |
#define s L"microsoft-edge:https://twitter.com/login" | |
#define ID L"id" | |
#define PW L"pw" | |
#define strAppUserModelID L"Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge" | |
#define URL L"https://twitter.com/login" | |
BOOL CALLBACK EnumWindowsProc( | |
_In_ HWND hwnd, | |
_In_ LPARAM lParam | |
); | |
BOOL CALLBACK EnumChildProc( | |
_In_ HWND hwnd, | |
_In_ LPARAM lParam | |
); | |
HRESULT GetDOM(); | |
HRESULT _Twitter(MSHTML::IHTMLDocument2Ptr pDoc); | |
HWND hEdge = 0; | |
HWND hIES = 0; | |
DWORD dwProcessId = 0; | |
UINT msg = 0; | |
_COM_SMARTPTR_TYPEDEF(IApplicationActivationManager, __uuidof(IApplicationActivationManager)); | |
HRESULT hoge() | |
{ | |
HRESULT hr; | |
try { | |
//Edge起動 | |
//ShellExecuteW(0, nullptr, s, nullptr, nullptr, SW_SHOWNORMAL); | |
IApplicationActivationManagerPtr spAppActivationManager; | |
HRESULT hr = spAppActivationManager.CreateInstance(CLSID_ApplicationActivationManager); | |
if (FAILED(hr)) | |
{ | |
_com_issue_error(hr); | |
} | |
spAppActivationManager->ActivateApplication(strAppUserModelID, URL, AO_NONE, &dwProcessId); | |
Sleep(2000); | |
EnumWindows(EnumWindowsProc, 0); | |
if (hEdge == 0) | |
return S_FALSE; | |
EnumChildWindows(hEdge, EnumChildProc, 0); | |
if (hIES == 0) | |
return S_FALSE; | |
GetDOM(); | |
} | |
catch (_com_error& e) { | |
hr = e.Error(); | |
MessageBoxA(0, "", e.ErrorMessage(), MB_OK); | |
} | |
return hr; | |
} | |
HRESULT GetDOM() | |
{ | |
HRESULT hr; | |
try { | |
msg = RegisterWindowMessageW(L"WM_HTML_GETOBJECT"); | |
DWORD_PTR res = 0; | |
SendMessageTimeoutW(hIES, msg, 0, 0, SMTO_ABORTIFHUNG, 1000, &res); | |
MSHTML::IHTMLDocument2Ptr d = nullptr; | |
if (res){ | |
HRESULT hr = ObjectFromLresult(res, __uuidof(MSHTML::IHTMLDocument2Ptr), 0, (void**) &d); | |
if (d){ | |
for (;;){ | |
if (lstrcmpW(d->readyState, L"complete") == 0) | |
break; | |
::Sleep(100); | |
} | |
_Twitter(d); | |
} | |
} | |
} | |
catch (_com_error& e) { | |
hr = e.Error(); | |
} | |
return hr; | |
} | |
HRESULT _Twitter(MSHTML::IHTMLDocument2Ptr pDoc) | |
{ | |
HRESULT hr = S_OK; | |
try{ | |
MSHTML::IHTMLElementCollectionPtr pCol(pDoc->forms); | |
MSHTML::IHTMLFormElementPtr pForm(pCol->item(_variant_t(1))); | |
MSHTML::IHTMLInputElementPtr pInput_ID(pForm->item(_variant_t(L"session[username_or_email]"))); | |
pInput_ID->value = ID; | |
MSHTML::IHTMLInputElementPtr pInput_PW(pForm->item(_variant_t(L"session[password]"))); | |
pInput_PW->value = PW; | |
pForm->submit(); | |
for (;;){ | |
if (lstrcmpW(pDoc->readyState, L"complete") == 0) | |
break; | |
::Sleep(100); | |
} | |
MSHTML::IHTMLStyleSheetsCollectionPtr pCol2(pDoc->styleSheets); | |
MSHTML::IHTMLStyleSheetPtr pStyleSheet(pCol2->item(&_variant_t(0))); | |
pStyleSheet->addRule(L"body", L"background-color:MediumSeaGreen!important;", -1); | |
pStyleSheet->addRule(L"body", L"font-family:MS Pゴシック!important;", -1); | |
pStyleSheet->addRule(L"body", L"font-size:14px!important;", -1); | |
} | |
catch (_com_error& e) { | |
hr = e.Error(); | |
MessageBoxA(0, "", e.ErrorMessage(), MB_OK); | |
} | |
return hr; | |
} | |
BOOL CALLBACK EnumWindowsProc( | |
_In_ HWND hwnd, | |
_In_ LPARAM lParam | |
) | |
{ | |
WCHAR buf1[255]; | |
WCHAR buf2[255]; | |
GetClassNameW(hwnd, buf1, sizeof(buf1)); | |
if (lstrcmpW(buf1, L"ApplicationFrameWindow") == 0){ | |
GetWindowTextW(hwnd, buf2, sizeof(buf2)); | |
if (wcsstr(buf2, L"Microsoft Edge") != nullptr){ | |
hEdge = hwnd; | |
return FALSE; | |
} | |
} | |
return TRUE; | |
} | |
BOOL CALLBACK EnumChildProc( | |
_In_ HWND hwnd, | |
_In_ LPARAM lParam | |
) | |
{ | |
WCHAR buf[255]; | |
GetClassNameW(hwnd, buf, sizeof(buf)); | |
if (lstrcmpW(buf, L"Internet Explorer_Server") == 0){ | |
hIES = hwnd; | |
return FALSE; | |
} | |
return TRUE; | |
} | |
int CALLBACK WinMain( | |
_In_ HINSTANCE hInstance, | |
_In_ HINSTANCE hPrevInstance, | |
_In_ LPSTR lpCmdLine, | |
_In_ int nCmdShow | |
) | |
{ | |
CoInitialize(nullptr); | |
hoge(); | |
CoUninitialize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment