File indexing completed on 2024-05-19 04:59:18
0001 /* ============================================================ 0002 * StatusBarIcons - Extra icons in statusbar for Falkon 0003 * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "sbi_javascripticon.h" 0019 #include "browserwindow.h" 0020 #include "tabwidget.h" 0021 #include "tabbedwebview.h" 0022 #include "webpage.h" 0023 #include "jsoptions.h" 0024 0025 #include <QGraphicsColorizeEffect> 0026 #include <QWebEngineSettings> 0027 #include <QMenu> 0028 0029 SBI_JavaScriptIcon::SBI_JavaScriptIcon(BrowserWindow* window) 0030 : SBI_Icon(window) 0031 { 0032 setObjectName(QSL("sbi_javascripticon")); 0033 setCursor(Qt::PointingHandCursor); 0034 setToolTip(tr("Modify JavaScript settings per-site and globally")); 0035 0036 m_icon = QIcon::fromTheme(QSL("application-x-javascript"), QIcon(QSL(":sbi/data/javascript.png"))); 0037 setPixmap(m_icon.pixmap(16)); 0038 0039 connect(m_window->tabWidget(), SIGNAL(currentChanged(int)), this, SLOT(updateIcon())); 0040 connect(this, &ClickableLabel::clicked, this, &SBI_JavaScriptIcon::showMenu); 0041 0042 updateIcon(); 0043 } 0044 0045 void SBI_JavaScriptIcon::showMenu(const QPoint &point) 0046 { 0047 QFont boldFont = font(); 0048 boldFont.setBold(true); 0049 0050 QMenu menu; 0051 menu.addAction(m_icon, tr("Current Page Settings"))->setFont(boldFont); 0052 0053 if (testCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled)) { 0054 menu.addAction(tr("Disable JavaScript (temporarily)"), this, &SBI_JavaScriptIcon::toggleJavaScript); 0055 } 0056 else { 0057 menu.addAction(tr("Enable JavaScript (temporarily)"), this, &SBI_JavaScriptIcon::toggleJavaScript); 0058 } 0059 0060 // JavaScript needs to be always enabled for falkon: sites 0061 if (currentPage() && currentPage()->url().scheme() == QLatin1String("falkon")) { 0062 menu.actions().at(1)->setEnabled(false); 0063 } 0064 0065 menu.addSeparator(); 0066 menu.addAction(m_icon, tr("Global Settings"))->setFont(boldFont); 0067 menu.addAction(tr("Manage JavaScript settings"), this, &SBI_JavaScriptIcon::openJavaScriptSettings); 0068 menu.exec(point); 0069 } 0070 0071 void SBI_JavaScriptIcon::updateIcon() 0072 { 0073 if (testCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled)) { 0074 setGraphicsEffect(nullptr); 0075 } 0076 else { 0077 auto* effect = new QGraphicsColorizeEffect(this); 0078 effect->setColor(Qt::gray); 0079 setGraphicsEffect(effect); 0080 } 0081 } 0082 0083 void SBI_JavaScriptIcon::toggleJavaScript() 0084 { 0085 WebPage *page = currentPage(); 0086 if (!page) { 0087 return; 0088 } 0089 0090 bool current = testCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled); 0091 setCurrentPageWebAttribute(QWebEngineSettings::JavascriptEnabled, !current); 0092 0093 m_settings[page] = !current; 0094 0095 connect(page, &WebPage::navigationRequestAccepted, this, [=](const QUrl &, WebPage::NavigationType, bool isMainFrame) { 0096 if (isMainFrame) { 0097 page->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, m_settings[page]); 0098 } 0099 }); 0100 0101 m_window->weView()->reload(); 0102 0103 updateIcon(); 0104 } 0105 0106 void SBI_JavaScriptIcon::openJavaScriptSettings() 0107 { 0108 auto* dialog = new JsOptions(m_window); 0109 dialog->open(); 0110 }