void MainWindow::attachStyles() { if (!mainSettings->contains("attach/styles")) { return; } QStringList styles = mainSettings->value("attach/styles").toStringList(); if (!styles.length()) { return; } QWebElement headElem = view->page()->mainFrame()->findFirstElement("head"); QString content = ""; if (headElem.isNull() || headElem.toInnerXml().trimmed().isEmpty()) { // Page without head... We need something in <head> to interact with? return; } QStringListIterator stylesIterator(styles); QString file_name; QFileInfo finfo = QFileInfo(); quint32 countStyles = 0; while (stylesIterator.hasNext()) { file_name = stylesIterator.next(); if (!file_name.trimmed().length()) continue; qDebug() << "-- attach " << file_name; countStyles++; finfo.setFile(file_name); if (finfo.isFile()) { qDebug("-- it's local file"); QFile f(file_name); content += "\n<style type=\"text/css\">\n"; content += QString(f.readAll()); content += "</style>\n"; f.close(); } else { qDebug("-- it's remote file"); content += "\n<link type=\"text/css\" rel=\"stylesheet\" href=\""; content += file_name; content += "\"/>\n"; } } if (countStyles > 0 && content.trimmed().length() > 0) { headElem.setInnerXml(headElem.toInnerXml() + content); qDebug() << "Page loaded, found " << countStyles << " user style files..."; } }
void MainWindow::attachJavascripts() { if (!mainSettings->contains("attach/javascripts")) { return; } QStringList scripts = mainSettings->value("attach/javascripts").toStringList(); if (!scripts.length()) { return; } QWebElement bodyElem = view->page()->mainFrame()->findFirstElement("body"); QString content = ""; if (bodyElem.isNull() || bodyElem.toInnerXml().trimmed().isEmpty()) { // No body here... We need something in <body> to interact with? return; } QStringListIterator scriptsIterator(scripts); QFileInfo finfo = QFileInfo(); QString file_name; quint32 countScripts = 0; while (scriptsIterator.hasNext()) { file_name = scriptsIterator.next(); if (!file_name.trimmed().length()) continue; qDebug() << "-- attach " << file_name; countScripts++; finfo.setFile(file_name); if (finfo.isFile()) { qDebug("-- it's local file"); QFile f(file_name); content += "\n<script type=\"text/javascript\">"; content += QString(f.readAll()); content += "</script>\n"; f.close(); } else { qDebug("-- it's remote file"); content += "\n<script type=\"text/javascript\" src=\""; content += file_name; content += "\"></script>\n"; } } if (countScripts > 0 && content.trimmed().length() > 0) { bodyElem.setInnerXml(bodyElem.toInnerXml() + content); qDebug() << "Page loaded, found " << countScripts << " user javascript files..."; } }
bool MainWindow::disableSelection() { if (mainSettings->value("view/disable_selection").toBool()) { qDebug("Try to disable text selection..."); // Then webkit loads page and it's "empty" - empty html DOM loaded... // So we wait before real page DOM loaded... QWebElement bodyElem = view->page()->mainFrame()->findFirstElement("body"); if (!bodyElem.isNull() && !bodyElem.toInnerXml().trimmed().isEmpty()) { QWebElement headElem = view->page()->mainFrame()->findFirstElement("head"); if (headElem.isNull() || headElem.toInnerXml().trimmed().isEmpty()) { qDebug("... html head not loaded ... wait..."); return false; } //qDebug() << "... head element content:\n" << headElem.toInnerXml(); // http://stackoverflow.com/a/5313735 QString content; content = "<style type=\"text/css\">\n"; content += "body, div, p, span, h1, h2, h3, h4, h5, h6, caption, td, li, dt, dd {\n"; content += " -moz-user-select: none;\n"; content += " -khtml-user-select: none;\n"; content += " -webkit-user-select: none;\n"; content += " user-select: none;\n"; content += " }\n"; content += "</style>\n"; // Ugly hack, but it's works... if (!headElem.toInnerXml().contains(content)) { headElem.setInnerXml(headElem.toInnerXml() + content); qDebug("... html head loaded ... hack inserted..."); } else { qDebug("... html head loaded ... hack already inserted..."); } //headElem = view->page()->mainFrame()->findFirstElement("head"); //qDebug() << "... head element content after:\n" << headElem.toInnerXml() ; } else { qDebug("... html body not loaded ... wait..."); return false; } } return true; }
void EnmlFormatter::fixImgNode(QWebElement &e) { QString enType = e.attribute("en-tag", ""); // Check if we have an en-crypt tag. Change it from an img to en-crypt if (enType.toLower() == "en-crypt") { QString encrypted = e.attribute("alt"); QString cipher = e.attribute("cipher", "RC2"); QString hint = e.attribute("hint", ""); QString length = e.attribute("length", "64"); e.removeAttribute("onmouseover"); e.removeAttribute("name"); e.removeAttribute("alt"); e.removeAttribute("en-tag"); e.removeAttribute("contenteditable"); e.removeAttribute("style"); removeInvalidAttributes(e); e.setInnerXml(encrypted); e.setOuterXml("<en-crypt cipher=\"" +cipher +"\" length=\"" + length + "\" hint=\"" +hint +"\">"+encrypted+"</en-crypt>"); // e.setOuterXml(e.toOuterXml().replace("<img", "<en-crypt").replace("</img", "</en-crypt")); return; } // Check if we have a temporary image. If so, remove it if (enType.toLower() == "temporary") {; e.removeFromDocument(); return; } // Latex images are really just img tags, so we now handle them later // Check if we have a LaTeX image. Remove the parent link tag // if (enType.toLower() == "en-latex") { // enType = "en-media"; // parent.parentNode().replaceChild(e, parent); // } // If we've gotten this far, we have an en-media tag e.removeAttribute("en-tag"); int lid = e.attribute("lid").toInt(); resources.append(lid); removeInvalidAttributes(e); e.setOuterXml(e.toOuterXml().replace("<img", "<en-media").replace("</img", "</en-media")); }
/* This will go through and modify some of the ENML tags and turn them into HTML tags. Things like en-media & en-crypt have no HTML values, so we turn them into HTML. */ void NoteFormatter::modifyTags(QWebPage &doc) { tempFiles.clear(); // Modify en-media tags QLOG_TRACE() << "Searching for all en-media tags;"; QWebElementCollection anchors = doc.mainFrame()->findAllElements("en-media"); QLOG_TRACE() << "Search complete: " << anchors.toList().size(); foreach (QWebElement enmedia, anchors) { if (enmedia.hasAttribute("type")) { QString attr = enmedia.attribute("type"); QString hash = enmedia.attribute("hash"); QStringList type = attr.split("/"); if (type.size() >= 2) { QString appl = type[1]; QLOG_TRACE() << "En-Media tag type: " << type[0]; if (type[0] == "image") modifyImageTags(enmedia, hash); else modifyApplicationTags(enmedia, hash, appl); QLOG_TRACE() << "Type modified"; } } } // Modify todo tags anchors = doc.mainFrame()->findAllElements("en-todo"); qint32 enTodoCount = anchors.count(); for (qint32 i=enTodoCount-1; i>=0; i--) { QWebElement enmedia = anchors.at(i); modifyTodoTags(enmedia); } anchors = doc.mainFrame()->findAllElements("en-crypt"); qint32 enCryptLen = anchors.count(); for (qint32 i=enCryptLen-1; i>=0; i--) { QWebElement enmedia = anchors.at(i); QString hint = enmedia.attribute("hint"); QString cipher = enmedia.attribute("cipher", "RC2"); QString length = enmedia.attribute("length","64"); enmedia.setAttribute("contentEditable","false"); enmedia.setAttribute("src", QString("file://")+global.fileManager.getImageDirPath("encrypt.png")); enmedia.setAttribute("en-tag","en-crypt"); enmedia.setAttribute("cipher", cipher); enmedia.setAttribute("length", length); enmedia.setAttribute("hint", hint); enmedia.setAttribute("alt", enmedia.toInnerXml()); global.cryptCounter++; enmedia.setAttribute("id", "crypt"+QString().number(global.cryptCounter)); QString encryptedText = enmedia.toInnerXml(); // If the encryption string contains crlf at the end, remove them because they mess up the javascript. if (encryptedText.endsWith("\n")) encryptedText.truncate(encryptedText.length()-1); if (encryptedText.endsWith("\r")) encryptedText.truncate(encryptedText.length()-1); // Add the commands hint = hint.replace("'","'"); enmedia.setAttribute("onClick", "window.browserWindow.decryptText('crypt"+ QString().number(global.cryptCounter)+ "', '"+encryptedText+"', '"+ hint +"', '" + cipher+ "', " + length + ");"); enmedia.setAttribute("onMouseOver", "style.cursor='hand'"); enmedia.setInnerXml(""); QString k = enmedia.toOuterXml(); k.replace("<en-crypt", "<img"); k.replace("img>", "<en-crypt"); enmedia.setOuterXml(k); } // Modify link tags anchors = doc.mainFrame()->findAllElements("a"); enCryptLen = anchors.count(); for (qint32 i=0; i<anchors.count(); i++) { QWebElement element = anchors.at(i); if (!element.attribute("href").toLower().startsWith("latex://")) element.setAttribute("title", element.attribute("href")); else { element.setAttribute("title", element.attribute("title").toLower().replace("http://latex.codecogs.com/gif.latex?","")); } } }