E-book viewer: When looking up words via Google in Europe pre-approve the GDPR consent cookie. Fixes #2047181 [Cannot use search google dictionary](https://bugs.launchpad.net/calibre/+bug/2047181)

This commit is contained in:
Kovid Goyal 2023-12-22 06:46:14 +05:30
parent 233891917a
commit 7f3ccb333d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 33 additions and 10 deletions

View File

@ -365,16 +365,26 @@ def google_parse_results(root, raw, log=prints, ignore_uncached=True):
return ans return ans
def google_consent_cookies():
# See https://github.com/benbusby/whoogle-search/pull/1054 for cookies
from datetime import date
from base64 import standard_b64encode
base = {'domain': '.google.com', 'path': '/'}
b = base.copy()
b['name'], b['value'] = 'CONSENT', 'PENDING+987'
yield b
template = b'\x08\x01\x128\x08\x14\x12+boq_identityfrontenduiserver_20231107.05_p0\x1a\x05en-US \x03\x1a\x06\x08\x80\xf1\xca\xaa\x06'
template.replace(b'20231107', date.today().strftime('%Y%m%d').encode('ascii'))
b = base.copy()
b['name'], b['value'] = 'SOCS', standard_b64encode(template).decode('ascii').rstrip('=')
yield b
def google_specialize_browser(br): def google_specialize_browser(br):
with webcache_lock: with webcache_lock:
if not hasattr(br, 'google_consent_cookie_added'): if not hasattr(br, 'google_consent_cookie_added'):
# See https://github.com/benbusby/whoogle-search/pull/1054 for cookies for c in google_consent_cookies():
br.set_simple_cookie('CONSENT', 'PENDING+987', '.google.com', path='/') br.set_simple_cookie(c['name'], c['value'], c['domain'], path=c['path'])
template = b'\x08\x01\x128\x08\x14\x12+boq_identityfrontenduiserver_20231107.05_p0\x1a\x05en-US \x03\x1a\x06\x08\x80\xf1\xca\xaa\x06'
from datetime import date
from base64 import standard_b64encode
template.replace(b'20231107', date.today().strftime('%Y%m%d').encode('ascii'))
br.set_simple_cookie('SOCS', standard_b64encode(template).decode('ascii').rstrip('='), '.google.com', path='/')
br.google_consent_cookie_added = True br.google_consent_cookie_added = True
return br return br

View File

@ -6,15 +6,17 @@ import sys
import textwrap import textwrap
from functools import lru_cache from functools import lru_cache
from qt.core import ( from qt.core import (
QAbstractItemView, QApplication, QCheckBox, QComboBox, QDialog, QDialogButtonBox, QAbstractItemView, QApplication, QCheckBox, QComboBox, QDateTime, QDialog,
QFormLayout, QHBoxLayout, QIcon, QLabel, QLineEdit, QListWidget, QListWidgetItem, QDialogButtonBox, QFormLayout, QHBoxLayout, QIcon, QLabel, QLineEdit, QListWidget,
QPalette, QPushButton, QSize, Qt, QTimer, QUrl, QVBoxLayout, QWidget, pyqtSignal, QListWidgetItem, QNetworkCookie, QPalette, QPushButton, QSize, Qt, QTimer, QUrl,
QVBoxLayout, QWidget, pyqtSignal,
) )
from qt.webengine import ( from qt.webengine import (
QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineView, QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineView,
) )
from calibre import prints, random_user_agent from calibre import prints, random_user_agent
from calibre.ebooks.metadata.sources.search_engines import google_consent_cookies
from calibre.gui2 import error_dialog from calibre.gui2 import error_dialog
from calibre.gui2.viewer.web_view import apply_font_settings, vprefs from calibre.gui2.viewer.web_view import apply_font_settings, vprefs
from calibre.gui2.widgets2 import Dialog from calibre.gui2.widgets2 import Dialog
@ -226,6 +228,17 @@ def create_profile():
insert_scripts(ans, create_script('lookup.js', js, injection_point=QWebEngineScript.InjectionPoint.DocumentCreation)) insert_scripts(ans, create_script('lookup.js', js, injection_point=QWebEngineScript.InjectionPoint.DocumentCreation))
s = ans.settings() s = ans.settings()
s.setDefaultTextEncoding('utf-8') s.setDefaultTextEncoding('utf-8')
cs = ans.cookieStore()
for c in google_consent_cookies():
cookie = QNetworkCookie()
cookie.setName(c['name'].encode())
cookie.setValue(c['value'].encode())
cookie.setDomain(c['domain'])
cookie.setPath(c['path'])
cookie.setSecure(False)
cookie.setHttpOnly(False)
cookie.setExpirationDate(QDateTime())
cs.setCookie(cookie)
create_profile.ans = ans create_profile.ans = ans
return ans return ans