avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 1 | var authuser = (new URL(location.href)).searchParams.get('authuser') || '0'; |
| 2 | |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 3 | chrome.storage.sync.get(null, function(items) { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 4 | if (items.history && |
| 5 | document.querySelector('.user-profile__user-links') === null) { |
| 6 | var nameElement = document.querySelector('.user-profile__user-name'); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 7 | if (nameElement !== null) { |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 8 | var profileLink = document.querySelector('.community-console'); |
| 9 | if (profileLink === null) { |
| 10 | console.error( |
| 11 | '[previousposts] ' + |
| 12 | 'The user is not a PE so we can\'t show the previous posts link.'); |
| 13 | return; |
| 14 | } |
| 15 | |
| 16 | var profileUrl = profileLink.href || ''; |
| 17 | var profileUrlSplit = profileUrl.split('/forum/'); |
| 18 | if (profileUrlSplit.length < 2) { |
| 19 | console.error('[previousposts] Can\'t get forum id.'); |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | var forumId = profileUrlSplit[1].split('/')[0]; |
avm99963 | 14116b0 | 2020-11-02 14:04:14 +0100 | [diff] [blame] | 24 | var name = escapeUsername(nameElement.textContent); |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 25 | var filter = |
| 26 | '(creator:"' + name + '" | replier:"' + name + '") forum:' + forumId; |
| 27 | var urlpart = encodeURIComponent('query=' + encodeURIComponent(filter)); |
| 28 | var authuserpart = |
| 29 | (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser)); |
| 30 | var url = 'https://support.google.com/s/community/search/' + urlpart + |
| 31 | authuserpart; |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 32 | |
| 33 | var links = document.createElement('div'); |
| 34 | links.classList.add('user-profile__user-links'); |
| 35 | |
| 36 | var linkTitle = document.createElement('div'); |
| 37 | linkTitle.classList.add('user-profile__user-link-title'); |
| 38 | linkTitle.textContent = chrome.i18n.getMessage('inject_links'); |
| 39 | |
| 40 | links.appendChild(linkTitle); |
| 41 | |
| 42 | var ul = document.createElement('ul'); |
| 43 | |
| 44 | var li = document.createElement('li'); |
| 45 | li.classList.add('user-profile__user-link'); |
| 46 | |
| 47 | var a = document.createElement('a'); |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 48 | a.classList.add('user-profile__user-link', 'TWPT-user-link'); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 49 | a.href = url; |
| 50 | a.setAttribute( |
| 51 | 'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963'); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 52 | |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 53 | var badge = document.createElement('span'); |
| 54 | badge.classList.add('TWPT-badge'); |
| 55 | badge.setAttribute( |
| 56 | 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [ |
| 57 | chrome.i18n.getMessage('appName') |
| 58 | ])); |
| 59 | |
| 60 | var badgeImg = document.createElement('img'); |
| 61 | badgeImg.src = |
| 62 | 'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg'; |
| 63 | |
| 64 | badge.appendChild(badgeImg); |
| 65 | a.appendChild(badge); |
| 66 | |
| 67 | var span = document.createElement('span'); |
| 68 | span.textContent = chrome.i18n.getMessage('inject_previousposts'); |
| 69 | |
| 70 | a.appendChild(span); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 71 | li.appendChild(a); |
| 72 | ul.appendChild(li); |
| 73 | links.appendChild(ul); |
| 74 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 75 | document.querySelector('.user-profile__user-details-container') |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 76 | .appendChild(links); |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 77 | } else { |
| 78 | console.error('[previousposts] Can\'t find username.'); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | }); |