//void ArestShopPlugin::SinglePageFound() //{ //#ifdef USE_WEBKIT // QWebFrame * ptrFrame = getWebPage()->mainFrame(); // parseProductPage(m_stCompData); // m_stCompData.strCompURL = ptrFrame->url().toString(); // emit priceSearchedFinished(m_stCompData); // productFoundFinish(); //#endif //}; void ArestShopPlugin::parseSearchProductPage(SearchResult & stResult,bool & bNextPage) { //run XML search page parse bNextPage=false; #ifdef USE_WEBKIT QWebFrame * ptrFrame = getWebPage()->mainFrame(); ////////////////////////////////////////////////////////////////////////// QWebElementCollection tableProdRows = ptrFrame->findAllElements("table[class=pricelist]"); for(int iIndex=0;iIndex<tableProdRows.count();++iIndex) { QWebElement prodNameHeader = tableProdRows.at(iIndex).findFirst("td[class=nazwa]"); if (prodNameHeader.isNull()) continue; QWebElement productLink = prodNameHeader.findFirst("a"); QUrl stProductURL = productLink.attribute("href"); ////////////////////////////////////////////////////////////////////////// QString strName = productLink.toPlainText(); stResult.insert(std::make_pair(strName,stProductURL)); } ////////////////////////////////////////////////////////////////////////// QWebElement tablePageNavi = ptrFrame->findFirstElement("li[class=next]"); if (tablePageNavi.isNull()) { bNextPage=false; return; } bNextPage=true; #endif stResult.insert(Arest::mSearchResult.begin(),Arest::mSearchResult.end()); bNextPage=m_bLoadNextPage; }
bool ArestShopPlugin::isSingleProductPage() { #ifdef USE_WEBKIT QWebFrame * ptrFrame = getWebPage()->mainFrame(); QWebElementCollection productPage = ptrFrame->findAllElements("h1[class=nazwatowaru]"); if (productPage.count()) return true; #endif return false; }
bool ArestShopPlugin::isNotFoundPage() { #ifdef USE_WEBKIT QWebFrame * ptrFrame = getWebPage()->mainFrame(); QWebElementCollection notFoundPage = ptrFrame->findAllElements("div[class=searchfailed]"); if (notFoundPage.count()) return true; #endif return false; }
static void methodCallCallback(GDBusConnection* connection, const char* sender, const char* objectPath, const char* interfaceName, const char* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData) { if (g_strcmp0(interfaceName, "org.webkit.gtk.WebExtensionTest")) return; if (!g_strcmp0(methodName, "GetTitle")) { uint64_t pageID; g_variant_get(parameters, "(t)", &pageID); WebKitWebPage* page = getWebPage(WEBKIT_WEB_EXTENSION(userData), pageID, invocation); if (!page) return; WebKitDOMDocument* document = webkit_web_page_get_dom_document(page); GUniquePtr<char> title(webkit_dom_document_get_title(document)); g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", title.get())); } else if (!g_strcmp0(methodName, "RunJavaScriptInIsolatedWorld")) { uint64_t pageID; const char* script; g_variant_get(parameters, "(t&s)", &pageID, &script); WebKitWebPage* page = getWebPage(WEBKIT_WEB_EXTENSION(userData), pageID, invocation); if (!page) return; GRefPtr<WebKitScriptWorld> world = adoptGRef(webkit_script_world_new()); g_assert(webkit_script_world_get_default() != world.get()); WebKitFrame* frame = webkit_web_page_get_main_frame(page); JSGlobalContextRef jsContext = webkit_frame_get_javascript_context_for_script_world(frame, world.get()); JSRetainPtr<JSStringRef> jsScript(Adopt, JSStringCreateWithUTF8CString(script)); JSEvaluateScript(jsContext, jsScript.get(), 0, 0, 0, 0); g_dbus_method_invocation_return_value(invocation, 0); } else if (!g_strcmp0(methodName, "AbortProcess")) { abort(); } else if (!g_strcmp0(methodName, "GetInitializationUserData")) { g_assert(initializationUserData); g_assert(g_variant_is_of_type(initializationUserData.get(), G_VARIANT_TYPE_STRING)); g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", g_variant_get_string(initializationUserData.get(), nullptr))); } else if (!g_strcmp0(methodName, "GetProcessIdentifier")) { g_dbus_method_invocation_return_value(invocation, g_variant_new("(u)", static_cast<guint32>(getCurrentProcessID()))); } }
void ArestShopPlugin::NotFoundPage() { #ifdef USE_WEBKIT QWebFrame * ptrFrame = getWebPage()->mainFrame(); m_stCompData.dPrice = 0; m_stCompData.strCompURL = ptrFrame->url().toString(); m_stCompData.eSearchResult=SR_COMPNOTFOUND; emit priceSearchedFinished(m_stCompData); afterSearchCleanup(); #endif }
void ArestShopPlugin::parseProductPage() { #ifdef USE_WEBKIT QWebFrame * ptrFrame = getWebPage()->mainFrame(); printPageContent(ptrFrame->toHtml()); QWebElement elCompFullName = ptrFrame->findFirstElement("h1[class=nazwatowaru]"); QWebElement elPrice = ptrFrame->findFirstElement("span[class=price]"); //div class="altFontFace price" QString strPrice = elPrice.toPlainText(); for(int i = 0;i<strPrice.size();i++) { if (strPrice.at(i).isLetterOrNumber()) continue; strPrice = strPrice.left(i); break; } m_stCompData.dPrice = strPrice.toDouble(); m_stCompData.eSearchResult = SR_COMPFOUND; m_stCompData.strCompURL = getWebPage()->mainFrame()->url().toString(); #endif #ifdef USE_XML_PARSER m_stCompData.dPrice = Arest::dPrice; m_stCompData.eSearchResult = SR_COMPFOUND; #endif }
char *WebClient::executeWebMethod( char *inMethod, char *inURL, int *outContentLength, char **outFinalURL, char **outMimeType, long inTimeoutInMilliseconds ) { char *returnString = NULL; if (outFinalURL) *outFinalURL = NULL; char *startString = "http://"; char *urlCopy = strdup( inURL ); char *urlStart = stringLocateIgnoreCase( urlCopy, startString ); char *serverStart; if( urlStart == NULL ) { // no http:// at start of URL serverStart = urlCopy; } else { serverStart = &( urlStart[ strlen( startString ) ] ); } // find the first occurrence of "/", which is the end of the // server name char *serverNameCopy = strdup( serverStart ); char *serverEnd = strstr( serverNameCopy, "/" ); char *getPath = strstr( serverStart, "/" ); if( serverEnd == NULL ) { serverEnd = &( serverStart[ strlen( serverStart ) ] ); getPath = "/"; } // terminate the url here to extract the server name serverEnd[0] = '\0'; int portNumber = 80; // look for a port number char *colon = strstr( serverNameCopy, ":" ); if( colon != NULL ) { char *portNumberString = &( colon[1] ); int numRead = sscanf( portNumberString, "%d", & portNumber ); if( numRead != 1 ) { portNumber = 80; } // terminate the name here so port isn't taken as part // of the address colon[0] = '\0'; } HostAddress *host = new HostAddress( strdup( serverNameCopy ), portNumber ); // will be set to true if we time out while connecting char timedOut; Socket *sock = SocketClient::connectToServer( host, inTimeoutInMilliseconds, &timedOut ); char *finalURL = strdup( inURL ); char *mimeType = NULL; int receivedLength = 0; if( sock != NULL ) { SocketStream *stream = new SocketStream( sock ); // reuse the same timeout for read operations stream->setReadTimeout( inTimeoutInMilliseconds ); // method and trailing space need to be sent in the same // buffer to work around a bug in certain web servers char *methodWithSpace = new char[ strlen( inMethod ) + 2 ]; sprintf( methodWithSpace, "%s ", inMethod ); // send the request stream->writeString( methodWithSpace ); stream->writeString( getPath ); stream->writeString( " HTTP/1.0\r\n" ); stream->writeString( "Host: " ); stream->writeString( serverNameCopy ); stream->writeString( "\r\n\r\n" ); delete [] methodWithSpace; // the simplest thing to do is to read upto the // socket close first, then extract the content char *received = receiveData( stream, &receivedLength ); char *content = NULL; char notFound = false; if( stringLocateIgnoreCase ( received, "404 Not Found" ) != NULL ) { notFound = true; } // watch for redirection headers if( stringLocateIgnoreCase( received, "302 Found" ) != NULL || stringLocateIgnoreCase( received, "301 Moved Permanently" ) != NULL || stringLocateIgnoreCase( received, "302 Object Moved" ) != NULL ) { // call ourself recursively to fetch the redirection char *locationTag = "Location: "; char *locationTagStart = stringLocateIgnoreCase( received, locationTag ); if( locationTagStart != NULL ) { char *locationStart = &( locationTagStart[ strlen( locationTag ) ] ); // replace next \r with \0 char *nextChar = locationStart; while( nextChar[0] != '\r' && nextChar[0] != '\0' ) { nextChar = &( nextChar[1] ); } nextChar[0] = '\0'; char *newFinalURL=NULL; if (strcmp(inURL,locationStart)) content = getWebPage( locationStart, &receivedLength, &newFinalURL, &mimeType ); delete [] locationTagStart; delete [] finalURL; finalURL = newFinalURL; if( content == NULL ) { // not found recursively notFound = true; } } } char *contentStartString = "\r\n\r\n"; char *contentTypeStartString = "Content-type:"; if( notFound ) { returnString = NULL; } else { if( content == NULL ) { // scan for content type char *contentTypeStartMarker = stringLocateIgnoreCase( received, contentTypeStartString ); if( contentTypeStartMarker != NULL ) { // skip marker char *contentTypeStart = &( contentTypeStartMarker[ strlen( contentTypeStartString ) ] ); // extract content type // make sure the buffer is big enough char *contentType = new char[ strlen( contentTypeStartMarker ) ]; int numRead = sscanf( contentTypeStart, "%s", contentType ); if( numRead == 1 ) { // trim mimeType = strdup( contentType ); } delete [] contentType; delete [] contentTypeStartMarker; } // extract the content from what we've received char *contentStart = strstr( received, contentStartString ); if( contentStart != NULL ) { content = &( contentStart[ strlen( contentStartString ) ] ); receivedLength = receivedLength - strlen( contentStartString ) - ( (long)(contentStart - received) ); returnString = new char[ receivedLength + 1 ]; returnString = (char*)memcpy( returnString, content, receivedLength ); returnString[ receivedLength ] = '\0'; } } else { // we already obtained our content recursively returnString = new char[ receivedLength + 1 ]; returnString = (char*)memcpy( returnString, content, receivedLength ); returnString[ receivedLength ] = '\0'; delete [] content; } } delete [] received; delete stream; delete sock; } delete host ; free ( serverNameCopy ) ; free ( urlCopy ) ; if( outFinalURL != NULL ) { *outFinalURL = finalURL; } else { free ( finalURL ) ; } if( outMimeType != NULL ) { *outMimeType = mimeType; } else { if( mimeType != NULL ) { free ( mimeType ) ; } } *outContentLength = receivedLength; return returnString; }