From 87b968f4cc336b8c241e4a21d164e1806382e695 Mon Sep 17 00:00:00 2001 From: iorate Date: Sat, 22 Feb 2020 22:57:36 +0900 Subject: [PATCH] Follow an update of Google SERP. --- src/scripts/engines/google.ts | 313 ++++++++++++--------------------- src/styles/engines/google.scss | 243 ++++++++++++------------- 2 files changed, 235 insertions(+), 321 deletions(-) diff --git a/src/scripts/engines/google.ts b/src/scripts/engines/google.ts index 232121af7..30a5c9f3b 100644 --- a/src/scripts/engines/google.ts +++ b/src/scripts/engines/google.ts @@ -29,61 +29,62 @@ function getURLFromQuery(selector: string): (entry: HTMLElement) => string | nul const device = new UAParser(window.navigator.userAgent).getDevice().type ?? ''; const tbm = new URL(window.location.href).searchParams.get('tbm') ?? ''; switch (`${device}/${tbm}`) { - // /All + // All case '/': window.ubContentHandlers = { controlHandlers: [ { - createControl: createControlDefault('#mBMHK', 'ub-control-all'), + createControl: createControlDefault('#mBMHK', 'ub-control_all_v1'), }, { - createControl: createControlDefault('#resultStats', 'ub-control-all'), + createControl: createControlDefault('#resultStats', 'ub-control_all_v1'), }, ], entryHandlers: [ + // Default, Web Result { - getEntries: getEntriesDefault('.srg > .g'), + getEntries: getEntriesDefault('.srg > .g, .bkWMgd > .g:not(.mnr-c)'), getURL: getURLDefault('a'), - createAction: createActionDefault('.yWc32e', 'ub-action-all--202001'), + createAction: createActionDefault('.eFM0qc', 'ub-action_all_default_v1'), }, { - getEntries: getEntriesDefault('div#rso > div > div.srg > div.g'), - getURL: getURLDefault('> div > div.rc > div.r > a'), - createAction: createActionDefault('> div > div.rc > div.r', 'ub-action-all'), + getEntries: getEntriesDefault('.srg > .g, .bkWMgd > .g:not(.mnr-c)'), + getURL: getURLDefault('a'), + createAction: createActionDefault('.yWc32e', 'ub-action_all_default_v1'), + }, + { + getEntries: getEntriesDefault('.srg > .g, .bkWMgd > .g:not(.mnr-c)'), + getURL: getURLDefault('a'), + // Fall back to the bottom. + createAction: createActionDefault('', 'ub-action_all_default_v1'), }, // Featured Snippet { - getEntries: getEntriesDefault('.g.mnr-c.g-blk'), + getEntries: getEntriesDefault('.bkWMgd > .g > .kp-blk > .xpdopen > .ifM9O > div > .g', 5), getURL: getURLDefault('.r > a'), - createAction: createActionDefault('.yWc32e', 'ub-action-all--202001'), + createAction: createActionDefault('.eFM0qc', 'ub-action_all_default_v1'), }, { - getEntries: getEntriesDefault( - 'div#rso > div > div.g.mnr-c.g-blk > div.kp-blk > div.xpdopen > div > div > div.g', - 5, - ), - getURL: getURLDefault( - '> div.kp-blk > div.xpdopen > div > div > div.g > div > div.rc > div.r > a', - ), - createAction: createActionDefault( - '> div.kp-blk > div.xpdopen > div > div > div.g > div > div.rc > div.r', - 'ub-action-all', - ), + getEntries: getEntriesDefault('.bkWMgd > .g.mnr-c'), + getURL: getURLDefault('.r > a'), + createAction: createActionDefault('.yWc32e', 'ub-action_all_default_v1'), }, - // Latest + // Latest, Top Stories (Horizontal) { - getEntries: getEntriesDefault( - 'div#rso > div > div > g-section-with-header > div > g-scrolling-carousel > div > div > div > div > g-inner-card', - 1, - ), - getURL: getURLDefault('> g-inner-card > a'), - createAction: createActionDefault('> g-inner-card', 'ub-action-all-latest'), + getEntries: getEntriesDefault('.So9e7d:nth-child(-n+3) > .ttfMne > .Pd7qJe', 2), + getURL: getURLDefault('.VlJC0'), + createAction: createActionDefault('.ttfMne', 'ub-action_all_latest_v1'), + }, + { + getEntries: getEntriesDefault('.So9e7d:nth-child(n+4) > .ttfMne', 1), + getURL: getURLDefault('.VlJC0'), + createAction: createActionDefault('.ttfMne', 'ub-action_all_latest_v1'), }, // Recipes { getEntries: getEntriesDefault('.YwonT'), getURL: getURLDefault('a'), - createAction: createActionDefault('a', 'ub-action-all__recipes'), + createAction: createActionDefault('a', 'ub-action_all_recipes_v1'), }, { getEntries: (addedElement: HTMLElement): HTMLElement[] => { @@ -92,133 +93,53 @@ switch (`${device}/${tbm}`) { : []; }, getURL: getURLDefault('a'), - createAction: createActionDefault('a', 'ub-action-all__recipes'), - }, - // Top Stories (Horizontal) - { - getEntries: getEntriesDefault( - 'div#rso > div > div > g-section-with-header > div > div > g-scrolling-carousel > div > div > div > div:nth-child(-n+3) > g-inner-card > div:nth-child(2)', - 2, - ), - getURL: getURLDefault('> g-inner-card > a'), - createAction: createActionDefault( - '> g-inner-card', - 'ub-action-all-top-stories-horizontal', - ), - }, - { - getEntries: getEntriesDefault( - 'div#rso > div > div > g-section-with-header > div > div > g-scrolling-carousel > div > div > div > div:nth-child(n+4) > g-inner-card', - 1, - ), - getURL: getURLDefault('> g-inner-card > a'), - createAction: createActionDefault( - '> g-inner-card', - 'ub-action-all-top-stories-horizontal', - ), + createAction: createActionDefault('a', 'ub-action_all_recipes_v1'), }, // Top Stories (Vertical) { getEntries: getEntriesDefault( - 'div#rso > div > div > g-section-with-header > div > div > div > div > div > div > lazy-load-item > div.dbsr > a > div > div:last-child > div:nth-child(2)', + 'div > div > div > lazy-load-item > .dbsr > a > .P5BnJb > .Od9uAe > .tYlW7b', 8, ), - getURL: getURLDefault('> div > div > lazy-load-item > div.dbsr > a'), - createAction: createActionDefault( - '> div > div > lazy-load-item > div.dbsr > a > div > div:last-child > div:nth-child(2)', - 'ub-action-all-top-stories-vertical', - ), - }, - { - getEntries: getEntriesDefault( - 'div#rso > div > div > g-section-with-header > div > div > div > div > div > div.dbsr > a > div > div > div:nth-child(2)', - 6, - ), - getURL: getURLDefault('> div > div.dbsr > a'), - createAction: createActionDefault( - '> div > div.dbsr > a > div > div > div:nth-child(2)', - 'ub-action-all-top-stories-vertical', - ), + getURL: getURLDefault('a'), + createAction: createActionDefault('.tYlW7b', 'ub-action_all_top-stories-vertical_v1'), }, { - getEntries: getEntriesDefault( - 'div#rso > div > div > g-section-with-header > div > div > g-inner-card', - ), - getURL: getURLDefault('> div.dbsr.kno-fb-ctx > g-card-section > a'), - createAction: createActionDefault( - '> div.dbsr.kno-fb-ctx > g-card-section > div', - 'ub-action-all', - ), + getEntries: getEntriesDefault('div > div > .dbsr > a > .P5BnJb > .Od9uAe > .tYlW7b', 6), + getURL: getURLDefault('a'), + createAction: createActionDefault('.tYlW7b', 'ub-action_all_top-stories-vertical_v1'), }, // Twitter { - getEntries: getEntriesDefault('.g'), + getEntries: getEntriesDefault('.bkWMgd > div > .g'), getURL: getURLDefault('a'), - createAction: createActionDefault('.qdrjAc.Dwsemf', 'ub-action-all__twitter--202001'), - }, - { - getEntries: getEntriesDefault('#rso > .bkWMgd > div > .g'), - getURL: getURLDefault('> .s > .DOqJne > .zTpPx > g-link > a'), - createAction: createActionDefault( - '> .s > .DOqJne > .qdrjAc.Dwsemf', - 'ub-action-all-twitter', - ), + createAction: createActionDefault('.qdrjAc', 'ub-action_all_twitter_v1'), }, // Twitter Search { - getEntries: getEntriesDefault('.g'), + getEntries: getEntriesDefault('.bkWMgd > div > .g'), getURL: getURLDefault('a'), - createAction: createActionDefault( - '.otisdd > .Dwsemf', - 'ub-action-all__twitter-search--202001', - ), - }, - { - getEntries: getEntriesDefault('div#rso > div > div > div.g'), - getURL: getURLDefault('> g-section-with-header > div > div > div > div > g-link > a'), - createAction: createActionDefault( - '> g-section-with-header > div > div > div > div:nth-child(3)', - 'ub-action-all-twitter', - ), + createAction: (entry: HTMLElement): HTMLElement | null => { + const nextSibling = entry.querySelector('.r'); + if (!nextSibling) { + return null; + } + const action = document.createElement('div'); + action.className = 'ub-action_all_twitter-search_v2'; + nextSibling.parentElement!.insertBefore(action, nextSibling); + return action; + }, }, { - getEntries: getEntriesDefault('div#rso > div > div > div.g'), - getURL: getURLDefault('> g-section-with-header > div > div > h3.r > g-link > a'), - createAction: createActionDefault( - '> g-section-with-header > div > div > div', - 'ub-action-all-twitter-search', - ), + getEntries: getEntriesDefault('.bkWMgd > div > .g'), + getURL: getURLDefault('a'), + createAction: createActionDefault('.Dwsemf', 'ub-action_all_twitter-search_v1'), }, // Videos { - getEntries: getEntriesDefault( - 'div#rso > div > div > g-section-with-header > div > g-scrolling-carousel > div > div > div > div > g-inner-card', - 1, - ), - getURL: getURLDefault('> g-inner-card > div > a'), - createAction: createActionDefault('> g-inner-card', 'ub-action-all-videos'), - }, - // Web Result - { - getEntries: getEntriesDefault('.bkWMgd > .g:not(.mnr-c)'), - getURL: getURLDefault('> * > .rc > .r > a'), - createAction: createActionDefault('.yWc32e', 'ub-action-all--202001'), - }, - { - getEntries: getEntriesDefault('div#rso > div > div.g'), - getURL: getURLDefault('> div > div.rc > div.r > a'), - createAction: createActionDefault('> div > div.rc > div.r', 'ub-action-all'), - }, - // Web Result with Site Links - { - getEntries: getEntriesDefault('.bkWMgd > .g:not(.mnr-c)'), - getURL: getURLDefault('> * > * > .rc > .r > a'), - createAction: createActionDefault('.yWc32e', 'ub-action-all--202001'), - }, - { - getEntries: getEntriesDefault('div#rso > div > div.g'), - getURL: getURLDefault('> div > div > div.rc > div.r > a'), - createAction: createActionDefault('> div > div > div.rc > div.r', 'ub-action-all'), + getEntries: getEntriesDefault('.P94G9b > .ZTH1s', 1), + getURL: getURLDefault('a'), + createAction: createActionDefault('.ZTH1s', 'ub-action_all_video_v1'), }, ], autoPagerizeHandlers: [ @@ -229,57 +150,49 @@ switch (`${device}/${tbm}`) { }; break; - // /Books + // Books case '/bks': window.ubContentHandlers = { controlHandlers: [ { - createControl: createControlDefault('#mBMHK', 'ub-control-all'), + createControl: createControlDefault('#mBMHK', 'ub-control_books_v1'), }, { - createControl: createControlDefault('#resultStats', 'ub-control-books'), + createControl: createControlDefault('#resultStats', 'ub-control_books_v1'), }, ], entryHandlers: [ - { - getEntries: getEntriesDefault('.g'), - getURL: getURLDefault('a'), - createAction: createActionDefault('.yWc32e', 'ub-action-books--202001'), - }, { getEntries: getEntriesDefault('.Yr5TG'), - getURL: getURLDefault('a'), - createAction: createActionDefault('.TbwUpd', 'ub-action-books--202001'), - }, - { - getEntries: getEntriesDefault('.Yr5TG'), - getURL: getURLDefault('> .bHexk > a'), - createAction: createActionDefault('> .bHexk > a', 'ub-action-books'), + getURL: getURLDefault('.bHexk > a'), + createAction: createActionDefault('.eFM0qc', 'ub-action_books_default_v1'), }, { - getEntries: getEntriesDefault('div#rso > div > div.srg > div.g'), - getURL: getURLDefault('> div > div.rc > div.r > a'), - createAction: createActionDefault('> div > div.rc > div.r', 'ub-action-books'), + getEntries: getEntriesDefault('.g'), + getURL: getURLDefault('a'), + createAction: createActionDefault('.eFM0qc', 'ub-action_books_default_v1'), }, - // All { - getEntries: getEntriesDefault('div#rso > div > div.g'), - getURL: getURLDefault('> div > div.rc > div.r > a'), - createAction: createActionDefault('> div > div.rc > div.r', 'ub-action-books'), + getEntries: getEntriesDefault('.g'), + getURL: getURLDefault('a'), + createAction: createActionDefault('.yWc32e', 'ub-action_books_default_v1'), }, ], autoPagerizeHandlers: [ { - getAddedElements: getAddedElementsDefault('.Yr5TG, .g'), + getAddedElements: getAddedElementsDefault('.g'), }, ], }; break; - // /Images + // Images case '/isch': window.ubContentHandlers = { controlHandlers: [ + { + createControl: createControlDefault('.cj2HCb', 'ub-control_images_v2'), + }, { createControl(): HTMLElement | null { const parent = document.getElementById('ab_ctls') as HTMLElement | null; @@ -287,115 +200,113 @@ switch (`${device}/${tbm}`) { return null; } const control = document.createElement('li'); - control.className = 'ab_ctl ub-control-images'; + control.className = 'ab_ctl ub-control_images_v1'; parent.appendChild(control); return control; }, }, - { - createControl: createControlDefault('.cj2HCb', 'ub-control-images-1'), - }, ], entryHandlers: [ { - getEntries: getEntriesDefault('div#rg_s > div.rg_bx.rg_di.rg_el.ivg-i'), + getEntries: getEntriesDefault('.isv-r'), + getURL: getURLDefault('.VFACy'), + createAction: createActionDefault('', 'ub-action_images_default_v1'), + }, + { + getEntries: getEntriesDefault('.rg_bx'), getURL(entry: HTMLElement): string | null { - const div = entry.querySelector(':scope > div.rg_meta.notranslate'); + const div = entry.querySelector('.rg_meta'); if (!div) { return null; } return /"ru":"([^"]+)"/.exec(div.textContent!)?.[1] ?? null; }, - createAction: createActionDefault('', 'ub-action-images'), - }, - { - getEntries: getEntriesDefault('div#islrg > div.islrc > div.isv-r'), - getURL: getURLDefault('> a:nth-child(2)'), - createAction: createActionDefault('', 'ub-action-images'), + createAction: createActionDefault('', 'ub-action_images_default_v1'), }, ], }; break; - // /News + // News case '/nws': window.ubContentHandlers = { controlHandlers: [ { - createControl: createControlDefault('#mBMHK', 'ub-control-all'), + createControl: createControlDefault('#mBMHK', 'ub-control_news_v1'), }, { - createControl: createControlDefault('#resultStats', 'ub-control-news'), + createControl: createControlDefault('#resultStats', 'ub-control_news_v1'), }, ], entryHandlers: [ { - getEntries: getEntriesDefault('g-card'), - getURL: getURLDefault('.dbsr > a'), - createAction: createActionDefault('.pDavDe', 'ub-action-news'), + getEntries: getEntriesDefault('.nChh6e'), + getURL: getURLDefault('a'), + createAction: createActionDefault('.pDavDe', 'ub-action_news_default_v2'), }, { - getEntries: getEntriesDefault('.g > .ts > .gG0TJc'), - getURL: getURLDefault('> h3.r > a.l'), - createAction: createActionDefault('> .slp', 'ub-action-news-1'), + getEntries: getEntriesDefault('.gG0TJc'), + getURL: getURLDefault('.l'), + createAction: createActionDefault('.slp', 'ub-action_news_default_v1'), adjustEntry(entry: HTMLElement): void { const image = entry.previousElementSibling; - if (image && !image.querySelector('.f')) { + if (image && !image.querySelector('.Y6GIfb')) { entry.insertBefore(image, entry.firstChild); } }, }, { - getEntries: getEntriesDefault('.g > .ts > .card-section'), - getURL: getURLDefault('> a'), - createAction: createActionDefault('', 'ub-action-news-1'), + getEntries: getEntriesDefault('.YiHbdc, .ErI7Gd'), + getURL: getURLDefault('a'), + createAction: createActionDefault('', 'ub-action_news_default_v1'), adjustEntry(entry: HTMLElement): void { const viewAll = entry.querySelector('.cWEW3c'); - if (viewAll) { - const parent = entry.parentElement!; - const nextSibling = entry.nextSibling; - const div = document.createElement('div'); - div.style.display = 'inline-block'; - div.appendChild(entry); - div.appendChild(viewAll); - parent.insertBefore(div, nextSibling); + if (!viewAll) { + return; } + const parent = entry.parentElement!; + const nextSibling = entry.nextSibling; + const div = document.createElement('div'); + div.style.display = 'inline-block'; + div.appendChild(entry); + div.appendChild(viewAll); + parent.insertBefore(div, nextSibling); }, }, { getEntries: (addedElement: HTMLElement): HTMLElement[] => { - return addedElement.matches('.g > .ts > a.top') && addedElement.querySelector('.f') + return addedElement.matches('.top') && addedElement.querySelector('.Y6GIfb') ? [addedElement] : []; }, getURL: getURLDefault(''), - createAction: createActionDefault('', 'ub-action-news-image'), + createAction: createActionDefault('', 'ub-action_news_image_v1'), }, ], autoPagerizeHandlers: [ { - getAddedElements: getAddedElementsDefault('g-card, .gG0TJc, .card-section, .top'), + getAddedElements: getAddedElementsDefault('.nChh6e, .gG0TJc, .YiHbdc, .ErI7Gd, .top'), }, ], }; break; - // /Videos + // Videos case '/vid': window.ubContentHandlers = { controlHandlers: [ { - createControl: createControlDefault('#mBMHK', 'ub-control-all'), + createControl: createControlDefault('#mBMHK', 'ub-control_videos_v1'), }, { - createControl: createControlDefault('#resultStats', 'ub-control-videos'), + createControl: createControlDefault('#resultStats', 'ub-control_videos_v1'), }, ], entryHandlers: [ { - getEntries: getEntriesDefault('div#rso > div > div.srg > div.g'), - getURL: getURLDefault('> div > div.rc > div.r > a'), - createAction: createActionDefault('> div > div.rc > div.r', 'ub-action-videos'), + getEntries: getEntriesDefault('.g'), + getURL: getURLDefault('a'), + createAction: createActionDefault('.r', 'ub-action_videos_default_v1'), }, ], autoPagerizeHandlers: [ diff --git a/src/styles/engines/google.scss b/src/styles/engines/google.scss index ff707a709..e20f7e947 100644 --- a/src/styles/engines/google.scss +++ b/src/styles/engines/google.scss @@ -1,164 +1,167 @@ -// /All +// All -.ub-control-all { - display: inline; +.ub-control_all { + &_v1 { + display: inline; + } } -.ub-action-all--202001 { - display: inline-block; - font-size: 14px; - margin-left: 4px; - white-space: nowrap; - width: 0; - span + & { - margin-left: 0; +.ub-action_all { + &_default_v1 { + display: inline-block; + font-size: 14px; + margin-left: 4px; + white-space: nowrap; + width: 0; + span + & { + margin-left: 0; + } } -} -.ub-action-all { - display: inline; - font-size: 16px; - margin-left: 4px; -} + &_latest_v1 { + display: block; + font-size: 14px; + margin: -10px 0 10px 0; + padding: 0 16px; + } -.ub-action-all-latest { - display: block; - font-size: 14px; - margin: -10px 0 10px 0; - padding: 0 16px; -} + &_recipes_v1 { + display: block; + font-size: 12px; + } -.ub-action-all__recipes { - display: block; - font-size: 12px; -} + &_top-stories-vertical_v1 { + display: inline; + font-size: 14px; + } -.ub-action-all-top-stories-horizontal { - display: block; - font-size: 14px; - margin: -10px 0 10px 0; - padding: 0 16px; -} + &_twitter_v1 { + flex: 25 0 auto; + font-size: 14px; + line-height: 1.3; + padding-top: 1px; + } -.ub-action-all-top-stories-vertical { - display: inline; - font-size: 14px; - margin-left: 4px; -} + &_twitter-search_v2 { + display: inline; + font-size: 14px; + margin-left: 4px; + } -.ub-action-all__twitter--202001 { - display: block; - flex: 25 0 auto; - font-size: 14px; - padding-top: 1px; -} + &_twitter-search_v1 { + flex: 25 0 auto; + font-size: 14px; + line-height: 20px; + padding-top: 1px; + } -.ub-action-all__twitter-search--202001 { - display: block; - flex: 25 0 auto; - font-size: 14px; - line-height: 20px; - padding-top: 1px; + &_video_v1 { + display: block; + font-size: 12px; + margin: -10px 0 10px 0; + padding: 0 16px; + } } -.ub-action-all-twitter { - display: inline; - flex: 25 0 auto; - font-size: 16px; - line-height: 24px; - padding-top: 1px; -} +// Books -.ub-action-all-twitter-search { - display: inline; - flex: 25 0 auto; - font-size: 16px; - line-height: 20px; - padding-top: 1px; +.ub-control_books { + &_v1 { + display: inline; + } } -.ub-action-all-videos { - display: block; - font-size: 12px; - margin: -10px 0 10px 0; - padding: 0 16px; +.ub-action_books { + &_default_v1 { + display: inline-block; + font-size: 14px; + margin-left: 4px; + white-space: nowrap; + width: 0; + } } -// /Books +// Images -.ub-control-books { - display: inline; -} +.ub-control_images { + &_v2 { + color: rgba(0, 0, 0, 0.54); + display: inline-block; + font-size: 13px; + margin-left: -13px; + margin-right: 29px; + padding-top: 20px; + } -.ub-action-books--202001 { - @extend .ub-action-all--202001; + &_v1 { + font-size: 12px; + margin-left: 16px; + } } -.ub-action-books { - display: inline; - font-size: 14px; - margin-left: 4px; +.ub-action_images { + &_default_v1 { + display: block; + font-size: 12px; + padding: 0 4px; + white-space: nowrap; + width: 0; + } } -// /Images - -.ub-control-images { - font-size: 12px; - margin-left: 16px; -} +// News -.ub-control-images-1 { - color: rgba(0, 0, 0, 0.54); - display: inline-block; - font-size: 13px; - margin-left: -13px; - margin-right: 29px; - padding-top: 20px; +.ub-control_news { + &_v1 { + display: inline; + } } -.ub-action-images { - display: block; - font-size: 12px; - padding: 0 4px; -} +.ub-action_news { + &_default_v2 { + display: inline; + font-size: 12px; + margin-left: 4px; + } -// /News + &_default_v1 { + display: inline; + font-size: 13px; + margin-left: 4px; + } -.ub-control-news { - display: inline; + &_image_v1 { + display: block; + font-size: 10px; + white-space: nowrap; + width: 0; + } } -.ub-action-news { - display: inline; - font-size: 12px; - margin-left: 8px; -} +// Videos -.ub-action-news-1 { - display: inline; - font-size: 13px; - margin-left: 4px; +.ub-control_videos { + &_v1 { + display: inline; + } } -.ub-action-news-image { - display: block; - font-size: 10px; +.ub-action_videos { + &_default_v1 { + display: inline; + font-size: 14px; + margin-left: 4px; + } } -// /Videos - -.ub-control-videos { - display: inline; -} +// Mobile/All, Mobile/News, Mobile/Videos, Tablet/All, Tablet/News, Tablet/Videos -.ub-action-videos { +.ub-action-all { display: inline; - font-size: 14px; + font-size: 16px; margin-left: 4px; } -// Mobile/All, Mobile/News, Mobile/Videos, Tablet/All, Tablet/News, Tablet/Videos - .ub-action-mobile-all { font-size: 12px; margin-left: 12px;