Inject String.replaceAll javascript implementation if needed into webengine when browsing SongSelect.

This commit is contained in:
Tomas Groth 2023-10-10 19:20:51 +00:00 committed by Raoul Snyman
parent 86b798ec7c
commit 58e3312ace
3 changed files with 43 additions and 16 deletions

View File

@ -42,27 +42,27 @@ log = logging.getLogger(__name__ + '.__init__')
USER_AGENTS = {
'win32': [
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36'
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/117.0.5938.132 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.5938.132 '
'Safari/537.36 Edg/117.0.2045.43',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/118.0'
],
'darwin': [
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) '
'Chrome/26.0.1410.43 Safari/537.31',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.11 (KHTML, like Gecko) '
'Chrome/20.0.1132.57 Safari/536.11',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) '
'Chrome/20.0.1132.47 Safari/536.11',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 14.0; rv:109.0) Gecko/20100101 Firefox/118.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/117.0.5938.132 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) AppleWebKit/605.1.15 (KHTML, like Gecko) '
'Version/16.5 Safari/605.1.15',
],
'linux2': [
'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1364.160 '
'Chrome/25.0.1364.160 Safari/537.22',
'Mozilla/5.0 (X11; CrOS armv7l 2913.260.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.99 '
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.5938.132 Safari/537.36',
'Mozilla/5.0 (X11; CrOS armv7l 2913.260.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/117.0.5938.132 '
'Safari/537.11',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.27 (KHTML, like Gecko) Chrome/26.0.1389.0 Safari/537.27'
'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0'
],
'default': [
'Mozilla/5.0 (X11; NetBSD amd64; rv:18.0) Gecko/20130120 Firefox/18.0'
'Mozilla/5.0 (X11; NetBSD amd64; rv:109.0) Gecko/20100101 Firefox/118.0'
]
}
CONNECTION_TIMEOUT = 30

View File

@ -44,6 +44,20 @@ WIN_OS_USER_AGENT = 'Windows NT 10.0; Win64; x64'
MAC_OS_USER_AGENT = 'Macintosh; Intel Mac OS X 13_5_2'
LINUX_OS_USER_AGENT = 'X11; Linux x86_64'
REPLACE_ALL_JS = """
// Based on: https://vanillajstoolkit.com/polyfills/stringreplaceall/
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function(str, newStr){
// If a regex pattern
if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
return this.replace(str, newStr);
}
// If a string
return this.split(str).join(newStr);
};
}
"""
log = logging.getLogger(__name__)
@ -85,6 +99,19 @@ class SongSelectForm(QtWidgets.QDialog, Ui_SongSelectDialog, RegistryProperties)
self.webview.page().loadFinished.connect(self.page_loaded)
self.webview.page().profile().downloadRequested.connect(self.on_download_requested)
self.webview.urlChanged.connect(self.update_url)
self.inject_js_str_replaceall()
def inject_js_str_replaceall(self):
"""
Inject an implementation of string replaceAll which are missing in pre 5.15.3 QWebEngine
"""
script = QtWebEngineWidgets.QWebEngineScript()
script.setInjectionPoint(QtWebEngineWidgets.QWebEngineScript.InjectionPoint.DocumentCreation)
script.setSourceCode(REPLACE_ALL_JS)
script.setWorldId(QtWebEngineWidgets.QWebEngineScript.ScriptWorldId.MainWorld)
script.setRunsOnSubFrames(True)
script.setName('string_replaceall')
self.webview.page().scripts().insert(script)
def update_url(self, new_url):
self.url_bar.setText(new_url.toString())

View File

@ -437,8 +437,8 @@ def test_initialise(mocked_ss_import: MagicMock, form: SongSelectForm):
# THEN: The import object should exist, song var should be None, and the page hooked up
assert form.song is None
assert isinstance(form.song_select_importer, SongSelectImport), 'SongSelectImport object should be created'
assert form.webview.page.call_count == 4, 'Page should be called 3 times, once for each load handler and ' \
'one from setting user agent'
assert form.webview.page.call_count == 5, 'Page should be called 5 times, once for each load handler and ' \
'one from setting user agent, one for user agent and one for JS inject'
@patch('openlp.plugins.songs.forms.songselectform.QtWidgets.QDialog.exec')