stylus/popup/search-results.js
eight e3d3604afc
Refactor the entire storage system and the section editor (#518)
* Squashed commit of the following:

commit d84c4dc3fe
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 19:13:29 2018 +0800

    Fix: remove unused comment

commit 46027120ec
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 19:09:06 2018 +0800

    Add: handle styleUpdated message

commit f85d4de39b
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 18:59:29 2018 +0800

    Fix: handle styleAdded message in popup

commit 81f3e69574
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 18:50:54 2018 +0800

    Change: getStylesInfoByUrl -> getStylesByUrl

commit f9dc04558f
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 18:48:20 2018 +0800

    Fix: drop getStylesInfo

commit fea04d591f
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 18:39:28 2018 +0800

    Fix: remove unused ignoreChromeError

commit 2aff14e213
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 18:09:53 2018 +0800

    Fix: don't dup promisify in prefs

commit d4ddfcc713
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 17:56:16 2018 +0800

    Change: drop .last and .rotate

commit 85e70491e4
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 17:36:00 2018 +0800

    Fix: unused renderIndex

commit 7acb131642
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 17:32:49 2018 +0800

    Fix: update title on input

commit a39405ac4c
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 17:17:20 2018 +0800

    Fix: remove unused messages

commit 14c2fdbb58
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 16:36:12 2018 +0800

    Fix: dirty state for new added applies

commit fb1b49b8bb
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 16:27:17 2018 +0800

    Fix: minor

commit 2c2d849fa4
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 16:20:14 2018 +0800

    Fix: drop unused getCode

commit f133c3e67a
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 16:18:14 2018 +0800

    Fix: drop unused lastActive

commit 05a6208f5c
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 16:17:45 2018 +0800

    Fix: minor

commit 05a87ed00f
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 15:58:33 2018 +0800

    Fix: minor

commit 576f73f333
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 03:03:35 2018 +0800

    Fix: always register listeners

commit e93819deb4
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 02:58:49 2018 +0800

    Fix: unused statement

commit 39b11685b4
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 02:54:29 2018 +0800

    Fix: minor

commit 9dd3cd43c1
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 02:49:22 2018 +0800

    Fix: don't reorder options

commit 90aadfd728
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 02:43:52 2018 +0800

    Fix: drop __ERROR__

commit 838c21e3b3
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 02:36:20 2018 +0800

    Fix: use findStyle API

commit 93a4cdf595
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 02:34:05 2018 +0800

    Add: findStyle API

commit 8e75871b9b
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 02:19:01 2018 +0800

    Breaking: drop getStylesFallback

commit ad06551440
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 02:16:48 2018 +0800

    Fix: use dataurl to inject page script

commit cb5cbb4d10
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 01:39:50 2018 +0800

    Fix: various

commit 53efd78b89
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 01:12:57 2018 +0800

    Update doc

commit 7d005f3eaa
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 01:09:22 2018 +0800

    Change: kill style.reason

commit fc53bed3de
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 00:56:04 2018 +0800

    Fix: doo many indents

commit 14e321d258
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 00:40:23 2018 +0800

    Fix: don't update icon for popup and options

commit 01bdd529bc
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 00:39:17 2018 +0800

    Fix: updateCount

commit b9968830d3
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 00:38:49 2018 +0800

    Fix: don't send null value

commit ff3bf6f52d
Author: eight <eight04@gmail.com>
Date:   Sun Oct 14 00:03:34 2018 +0800

    Add: styleViaAPI updateCount

commit 39d21c3d29
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 23:57:45 2018 +0800

    Fix: broadcastError -> ignoreError

commit ecb622c93c
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 21:29:06 2018 +0800

    Fix: implement styleViaAPI

commit 7c3d49c005
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 17:50:28 2018 +0800

    Fix: ROOT may change in XML pages

commit 3fd8d937f3
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 16:49:43 2018 +0800

    Fix: various

commit 859afc8ee9
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 16:39:54 2018 +0800

    Enhance: don't cache enabled state

commit fbe77a8d15
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 16:15:07 2018 +0800

    Fix: various

commit a4fc3e9162
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 16:11:38 2018 +0800

    Fix: various

commit 7e0eddeb8f
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 15:58:31 2018 +0800

    Fix: various

commit 8b4ab47d89
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 15:20:10 2018 +0800

    Add: some type hint

commit 7d340d62dc
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 15:13:11 2018 +0800

    Change: drop storage.js, some functions are moved to sections-util

commit d286997d6a
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 15:12:00 2018 +0800

    Fix: minor

commit d60db9dbef
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 15:03:10 2018 +0800

    Fix: minor

commit 43afa31fa0
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 14:50:31 2018 +0800

    Fix: update tab icon on forward/backward

commit f08faea149
Author: eight <eight04@gmail.com>
Date:   Sat Oct 13 13:50:03 2018 +0800

    Fix: parallel import

commit 4d06435486
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 23:32:03 2018 +0800

    Add: importStyle API

commit c55675912e
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 23:14:46 2018 +0800

    Fix: refactor import-export

commit 86ea846a89
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 17:34:36 2018 +0800

    Fix: search db is broken

commit 831ca07c2d
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 17:29:35 2018 +0800

    fixup! Add: implement sloppy regexp indicator

commit e67b7f4f36
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 17:27:19 2018 +0800

    Add: implement sloppy regexp indicator

commit 36e13f88f0
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 16:59:23 2018 +0800

    Add: return excluded/sloppy state in getStylesInfoByUrl

commit f6ce78f55b
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 16:39:47 2018 +0800

    Fix: dead object

commit 5ae95a1ad9
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 16:27:54 2018 +0800

    Fix: don't reinit all editors on save

commit 1a5a206fe6
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 16:18:40 2018 +0800

    Refactor: pull out sections editor section

commit 8016346035
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 15:30:35 2018 +0800

    Fix: replaceStyle make style name undefined

commit fa080d1913
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 15:21:36 2018 +0800

    Fix: catch csp error

commit e0b064115d
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 15:03:00 2018 +0800

    Fix: use a simple eval to execute page scripts

commit 405b7f8f06
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 03:48:13 2018 +0800

    Fix: removed unused API

commit 1b2c88f926
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 03:46:51 2018 +0800

    Fix: no need to access db

commit a8131fc9c5
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 03:43:31 2018 +0800

    Fix: remove unused methods

commit 3ae0c4dd13
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 03:10:26 2018 +0800

    Enhance: allow matcher to return verbose info

commit 0ea7ada48f
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 02:02:14 2018 +0800

    Fix: content script may load before the background is ready

commit 04c2d6bbf6
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 01:49:52 2018 +0800

    Fix: throw receiving end doesn't exist message

commit f0c0bc4d6a
Author: eight <eight04@gmail.com>
Date:   Fri Oct 12 01:11:17 2018 +0800

    Fix: unwrap error

commit 4d42765d6c
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 23:55:16 2018 +0800

    fixup! Fix: match subdomain

commit 99626e4a48
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 23:54:58 2018 +0800

    Fix: match subdomain

commit a57b3b2716
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 23:39:11 2018 +0800

    Fix: firefox

commit 5cfea3933f
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 22:46:34 2018 +0800

    Add some comment to db.js

commit 25fd3a1c2b
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 22:14:56 2018 +0800

    Fix: remove unused prop

commit bdae1c3697
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 20:00:25 2018 +0800

    Change: simpler styleCodeEmpty

commit bd4a453f45
Merge: c1bf9f5 9058c06
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 19:49:37 2018 +0800

    Merge branch 'dev-usercss-meta' into dev-exclusions

commit c1bf9f57e9
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 19:29:17 2018 +0800

    Fix: minor

commit fd5eeb4b81
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 19:00:05 2018 +0800

    Add: refresh on view

commit 3e38810a49
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 18:13:24 2018 +0800

    Fix: make sure icons are refreshed at startup

commit c657d7e55c
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 17:32:27 2018 +0800

    Add: implement bug 461

commit 7ed39ab6ef
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 15:42:44 2018 +0800

    fixup! Add: icon-util

commit 30e494eda9
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 15:42:23 2018 +0800

    Add: icon-util

commit 510a886e14
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 03:21:38 2018 +0800

    Fix: exposeIframes

commit c7f81662c4
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 02:19:14 2018 +0800

    Fix: autoCloseBrackets is true by default

commit f3a103645d
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 02:11:14 2018 +0800

    Fix: various

commit d4436cde20
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 01:39:10 2018 +0800

    Add: implement exposeIframe

commit 43db875fd8
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 01:26:24 2018 +0800

    Kill more globals

commit dc491e9be3
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 01:22:13 2018 +0800

    Kill old storage, storage-dummy

commit ba64b95575
Author: eight <eight04@gmail.com>
Date:   Thu Oct 11 00:54:38 2018 +0800

    WIP: kill cachedStyles

commit 7eba890a21
Merge: d2b36a1 81e4823
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 23:15:14 2018 +0800

    Merge branch 'dev-private-prefs' into dev-exclusions

commit d2b36a168e
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 23:05:20 2018 +0800

    Kill hidden globals

commit 22d4767511
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 19:23:34 2018 +0800

    Fix: margin for deleted sections

commit 00687983f0
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 18:21:07 2018 +0800

    Fix: default value

commit ff6fd8cad3
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 18:02:51 2018 +0800

    Fix: default options

commit c23f315c52
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 17:40:07 2018 +0800

    Refactor: use CodeMirror.defineOption

commit 4419c5dc1e
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 16:32:39 2018 +0800

    Change: kill editors, styleId

commit 6494985b50
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 16:14:51 2018 +0800

    Fix: various

commit 37e1f43f75
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 15:04:03 2018 +0800

    Fix: minor

commit d26ce3238e
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 14:49:37 2018 +0800

    Add: codemirror-factory

commit 15a1f552f6
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 12:08:35 2018 +0800

    WIP: kill getSection

commit ba6159e067
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 02:43:09 2018 +0800

    WIP: edit page

commit fd9ab5d6e5
Author: eight <eight04@gmail.com>
Date:   Wed Oct 10 00:41:07 2018 +0800

    Fix: switch to editor

commit 06e22d0d18
Author: eight <eight04@gmail.com>
Date:   Tue Oct 9 23:38:29 2018 +0800

    Change: add sections-editor

commit 30e8662946
Author: eight <eight04@gmail.com>
Date:   Mon Oct 8 20:12:39 2018 +0800

    Add: preview error

commit 47b2b4fc49
Author: eight <eight04@gmail.com>
Date:   Mon Oct 8 18:38:01 2018 +0800

    Add: livePreview.show

commit 7b5e7c96d5
Author: eight <eight04@gmail.com>
Date:   Mon Oct 8 18:16:45 2018 +0800

    Hook up live preview

commit 15efafff3c
Author: eight <eight04@gmail.com>
Date:   Mon Oct 8 17:49:57 2018 +0800

    Add: live preview

commit a38558ef78
Author: eight <eight04@gmail.com>
Date:   Mon Oct 8 15:30:39 2018 +0800

    WIP: make notifyAllTabs a noop

commit 582e9078af
Author: eight <eight04@gmail.com>
Date:   Mon Oct 8 14:39:08 2018 +0800

    Fix: inject all scripts

commit f4651da8d8
Author: eight <eight04@gmail.com>
Date:   Sun Oct 7 23:41:46 2018 +0800

    Drop deleteStyle

commit 0489fb3b2f
Author: eight <eight04@gmail.com>
Date:   Sun Oct 7 23:33:51 2018 +0800

    Drop saveStyle

commit 02f471f077
Author: eight <eight04@gmail.com>
Date:   Sun Oct 7 23:28:41 2018 +0800

    Fix: usercss API

commit 057111b171
Author: eight <eight04@gmail.com>
Date:   Sun Oct 7 22:59:31 2018 +0800

    Update usercss API

commit 69cae02381
Author: eight <eight04@gmail.com>
Date:   Sun Oct 7 21:40:29 2018 +0800

    Drop getStyles

commit c5d41529d9
Author: eight <eight04@gmail.com>
Date:   Sun Oct 7 21:28:51 2018 +0800

    Minor fixes

commit 5b3b4e680f
Author: eight <eight04@gmail.com>
Date:   Sun Oct 7 21:20:39 2018 +0800

    Add: navigator-util

commit b5107b78a5
Author: eight <eight04@gmail.com>
Date:   Sun Oct 7 01:42:43 2018 +0800

    Add: broadcast messages with reasons

commit e7ef4948cd
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 18:10:47 2018 +0800

    Fix: observer is unavailable?

commit 1c635b5bc1
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 17:47:43 2018 +0800

    Drop requestStyles

commit 75f2561154
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 16:38:04 2018 +0800

    Fix: don't recreate element when style update in popup

commit 583ca31d97
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 15:40:07 2018 +0800

    fixup! Add: isCodeEmpty

commit 1cf6008514
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 15:33:18 2018 +0800

    Add: isCodeEmpty

commit 450cd60aeb
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 15:22:04 2018 +0800

    Fix: ignore comment block

commit 196b6aac63
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 15:16:00 2018 +0800

    Fix: the return value of getSectionsByUrl is changed

commit 3122d28c1a
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 15:14:05 2018 +0800

    Fix: always use promise in API call

commit e594b8ccb1
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 15:11:01 2018 +0800

    Cache enabled state

commit 1f18b13a92
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 13:48:46 2018 +0800

    Add: match global sections

commit fedf844ddd
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 13:45:37 2018 +0800

    Add: getStylesInfoByUrl

commit 095998f07c
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 13:27:58 2018 +0800

    Change: switch to msg.js

commit fa3127d988
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 13:02:45 2018 +0800

    Change: switch to msg.js

commit 05d582c726
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 11:43:42 2018 +0800

    Add: msg.sendBg

commit 171339f710
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 04:39:48 2018 +0800

    WIP: drop api.js

commit 3a618aca2a
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 03:19:51 2018 +0800

    WIP: use deepCopy

commit bb1cb58024
Author: eight <eight04@gmail.com>
Date:   Sat Oct 6 03:10:04 2018 +0800

    WIP: msg.js

commit 2472e91f57
Author: eight <eight04@gmail.com>
Date:   Fri Oct 5 21:28:19 2018 +0800

    WIP: emitChangesToTabs

commit 34497ebe16
Author: eight <eight04@gmail.com>
Date:   Fri Oct 5 18:47:52 2018 +0800

    WIP: switch to API

commit f1639cc33e
Author: eight <eight04@gmail.com>
Date:   Fri Oct 5 01:03:40 2018 +0800

    WIP: broadcastMessage

commit 81e4823f46
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 19:39:59 2018 +0800

    Debounce updateAllTabsIcon

commit dc5f3e209f
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 19:34:36 2018 +0800

    Fix: settings could be empty on the first install

commit 2328cf623a
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 19:34:22 2018 +0800

    Change: start-firefox -> start

commit 7be6a1cba9
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 19:24:35 2018 +0800

    Add: applications

commit 630725196f
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 19:22:44 2018 +0800

    fixup! Fix: update all icons when some prefs changed

commit 0d0e1b4dc0
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 19:20:36 2018 +0800

    Fix: update all icons when some prefs changed

commit 5c0288e9ba
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 19:20:11 2018 +0800

    fixup! Remove unused FIREFOX_NO_DOM_STORAGE

commit 56b737b65a
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 18:14:57 2018 +0800

    Remove unused FIREFOX_NO_DOM_STORAGE

commit 829a134ed1
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 18:10:53 2018 +0800

    Fix: this -> prefs

commit d35f92250e
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 18:08:19 2018 +0800

    Fixme: styleViaAPI

commit 8a6e8ac03a
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 18:05:41 2018 +0800

    Change: drop prefChanged, use prefs service

commit 10f9449144
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 17:46:45 2018 +0800

    Change: move setupLivePrefs to dom.js. Remove prefs.js dependencies

commit dd2b8ed091
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 17:18:38 2018 +0800

    Fix: type error

commit 3af310c341
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 17:09:26 2018 +0800

    Fix: open-manager has no default value

commit 874a2da33e
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 17:04:23 2018 +0800

    Enhance: make prefs use storage.sync

commit c01f93f62c
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 15:57:02 2018 +0800

    WIP

commit 6d32ffb76b
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 12:46:19 2018 +0800

    WIP

commit 0f148eac32
Author: eight <eight04@gmail.com>
Date:   Thu Oct 4 03:35:07 2018 +0800

    WIP

commit 282bdf7706
Author: eight <eight04@gmail.com>
Date:   Wed Oct 3 20:24:06 2018 +0800

    Fix: numbers are not compared correctly

commit 24b1eea8a4
Merge: 8a6011d 5cbe8a8
Author: eight <eight04@gmail.com>
Date:   Wed Oct 3 15:00:07 2018 +0800

    Merge branch 'master' of https://github.com/openstyles/stylus into dev-exclusions

commit 5cbe8a8d78
Author: eight <eight04@gmail.com>
Date:   Tue Oct 2 20:22:18 2018 +0800

    Add: fetch style object from DB directly in the editor (#507)

commit 9058c06c54
Author: eight <eight04@gmail.com>
Date:   Mon Oct 1 23:24:29 2018 +0800

    Fix: bad API

commit 1f2d116aae
Author: eight <eight04@gmail.com>
Date:   Mon Oct 1 23:14:56 2018 +0800

    Fix: use meta parser

commit 918e47b1ed
Author: eight <eight04@gmail.com>
Date:   Mon Oct 1 23:01:21 2018 +0800

    Fix: emit update event if no fatal errors

commit 81a7bb9ac9
Author: eight <eight04@gmail.com>
Date:   Mon Oct 1 22:56:25 2018 +0800

    Add: editorWorker.metalint

commit f47d57aea8
Author: eight <eight04@gmail.com>
Date:   Mon Oct 1 22:49:16 2018 +0800

    Change: use editorWorker.metalint

commit 5778d5c858
Author: eight <eight04@gmail.com>
Date:   Mon Oct 1 22:39:01 2018 +0800

    Change: editor-worker-body -> editor-worker

commit 268e1716b4
Author: eight <eight04@gmail.com>
Date:   Mon Oct 1 22:38:06 2018 +0800

    Change: switch to worker-util

commit cc2980b647
Author: eight <eight04@gmail.com>
Date:   Mon Oct 1 22:30:16 2018 +0800

    Drop: parserlib-loader

commit 08adcb60f2
Merge: 6909c73 2fd531e
Author: eight <eight04@gmail.com>
Date:   Mon Oct 1 22:29:39 2018 +0800

    Merge branch 'master' into dev-usercss-meta

commit e4135ce35d
Author: eight <eight04@gmail.com>
Date:   Fri Sep 28 11:57:34 2018 +0800

    Fix: remove unused function

commit 39a6d1909f
Author: eight <eight04@gmail.com>
Date:   Fri Sep 28 00:26:29 2018 +0800

    Fix: prefs doesn't work in FF's private windows. Add web-ext. Drop prefs.readOnlyValues

commit 6909c73c69
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 12:16:33 2018 +0800

    Fix: minor

commit 79833d8bba
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 11:40:04 2018 +0800

    Fix: a better way to draw list?

commit a849fd6dda
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 11:39:53 2018 +0800

    Fix: missing placeholders

commit d5ee31a080
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 11:37:50 2018 +0800

    Fix: a better way to draw character list?

commit 7b959af3e3
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 11:30:10 2018 +0800

    Update usercss-meta

commit fefa987c4d
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 10:37:28 2018 +0800

    Change: sections-equal -> sections-util

commit 2abbf670d8
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 10:37:14 2018 +0800

    Fix: check err.code

commit 1fe0586b29
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 10:33:02 2018 +0800

    Add: i18n error message

commit ab0ef239cf
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 09:34:57 2018 +0800

    Change: move styleCodeEmpty to sections-util, load colorConverter in background worker

commit d5ade807f0
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 09:27:30 2018 +0800

    Fix: display error message

commit 4f5337e51d
Author: eight <eight04@gmail.com>
Date:   Wed Sep 26 09:26:55 2018 +0800

    Fix: remove unused colorconverter

commit 29b8f51292
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 23:21:44 2018 +0800

    Fix: vars could be undefined

commit a7cfeb22e4
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 22:54:40 2018 +0800

    Fix: window is undefined

commit 9713c6a3be
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:56:38 2018 +0800

    Fix: throw an error for unparsable color

commit 3c30bc3eb0
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:55:55 2018 +0800

    Fix: try to get error message

commit 3d32b0428b
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:38:40 2018 +0800

    Fix: vars might be empty

commit 7d75dd8754
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:18:39 2018 +0800

    Add: meta-parser

commit a4df641b96
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:18:18 2018 +0800

    Enhance: set flag in parserlib so we don't need another loader

commit 8028a3529f
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:17:40 2018 +0800

    Include util, worker-util in background

commit ba5d6cc31a
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:16:59 2018 +0800

    Fix: use spread syntax in loadScript

commit b853be13f8
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:14:46 2018 +0800

    Enhance: swith to usercss-meta (in worker)

commit a3e7915199
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:11:54 2018 +0800

    Fix: use promise API

commit 5d07a8cd4e
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:11:09 2018 +0800

    Fix: buildMeta now returns a promise

commit a004bc3c7d
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:10:35 2018 +0800

    Move styleCodeEmpty to util

commit 41ac66a137
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:09:40 2018 +0800

    Add: background worker

commit ffb13bf1db
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 21:09:04 2018 +0800

    Enhance: move moz-parser/meta-parser/usercss compiler to worker

commit 42e97ef153
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 20:45:07 2018 +0800

    Fix: display error on install page

commit 64aa9fcf53
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 17:34:54 2018 +0800

    Add: background worker

commit b0e407e98f
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 14:52:35 2018 +0800

    Add: worker util

commit 7a24547e09
Author: eight <eight04@gmail.com>
Date:   Tue Sep 25 00:01:18 2018 +0800

    Add: usercss-meta

commit 8a6011de8c
Author: Rob Garrison <wowmotty@gmail.com>
Date:   Sun Jul 22 09:15:09 2018 -0500

    Attempt to update icon count

commit 4fcb1a88d7
Author: Rob Garrison <wowmotty@gmail.com>
Date:   Sun Jul 15 13:44:29 2018 -0500

    Fix empty exclusion storage error

commit bfe54ab4c4
Author: Rob Garrison <wowmotty@gmail.com>
Date:   Sun Jul 15 12:59:51 2018 -0500

    Add tab communication

commit 983a7bc219
Author: Rob Garrison <wowmotty@gmail.com>
Date:   Sun Jul 15 10:51:11 2018 -0500

    Fix escaped regex example

commit 3950482f34
Author: Rob Garrison <wowmotty@gmail.com>
Date:   Wed Apr 25 18:11:37 2018 -0500

    Fix undefined error

commit e94c7edb38
Author: Rob Garrison <wowmotty@gmail.com>
Date:   Wed Apr 25 17:09:45 2018 -0500

    Attempt to fix popup exclusion issues

commit 2b4a1a5635
Author: Rob Garrison <wowmotty@gmail.com>
Date:   Thu Apr 19 13:00:27 2018 -0500

    Modify input method

commit 9f75b69cd8
Author: Rob Garrison <wowmotty@gmail.com>
Date:   Wed Mar 7 11:54:05 2018 -0600

    Include iframe urls in exclusion popup

commit 68dfa0153c
Author: Rob Garrison <wowmotty@gmail.com>
Date:   Wed Jan 24 19:42:02 2018 -0600

    Add style exclusions. Closes #113

* Revert: exclusions

* Fix: pass eslint

* Fix: the style is injected twice

* Fix: don't load script async

* Fix: styleCodeEmpty returns true for empty string

* Fix: drop array selection

* Fix: the config dialog is broken

* Fix: popup doesn't use getStyle/getStylesByUrl correctly

* Fix: keep disabled state in setStyleContent

* Fix: allow live-preview to assign newest vars

* Fix: transition fix is broken because setStyleContent becomes async

* Fix: typo, TypeError in styleExists

* Fix: use new API

* Fix: pass linter

* Fix: LICENCE -> LICENSE

* Fix: remove unused distroy function
2018-11-07 14:09:29 +08:00

866 lines
27 KiB
JavaScript
Executable File

/* global tabURL handleEvent $ $$ prefs template FIREFOX chromeLocal debounce
$create t API tWordBreak formatDate tryCatch tryJSONparse LZString
ignoreChromeError */
'use strict';
window.addEventListener('showStyles:done', function _() {
window.removeEventListener('showStyles:done', _);
if (!tabURL) {
return;
}
//region Init
const BODY_CLASS = 'search-results-shown';
const RESULT_ID_PREFIX = 'search-result-';
const BASE_URL = 'https://userstyles.org';
const UPDATE_URL = 'https://update.userstyles.org/%.md5';
// normal category is just one word like 'github' or 'google'
// but for some sites we need a fallback
// key: category.tld
// value <string>: use as category
// value true: fallback to search_terms
const CATEGORY_FALLBACK = {
'userstyles.org': 'userstyles.org',
'last.fm': true,
'Stylus': true,
};
const RX_CATEGORY = /^(?:.*?)([^.]+)(?:\.com?)?\.(\w+)$/;
const DISPLAY_PER_PAGE = 10;
// Millisecs to wait before fetching next batch of search results.
const DELAY_AFTER_FETCHING_STYLES = 0;
// Millisecs to wait before fetching .JSON for next search result.
const DELAY_BEFORE_SEARCHING_STYLES = 0;
// update USO style install counter
// if the style isn't uninstalled in the popup
const PINGBACK_DELAY = 60e3;
const BLANK_PIXEL_DATA = 'data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAA' +
'C1HAwCAAAAC0lEQVR42mOcXQ8AAbsBHLLDr5MAAAAASUVORK5CYII=';
const CACHE_SIZE = 1e6;
const CACHE_PREFIX = 'usoSearchCache/';
const CACHE_DURATION = 24 * 3600e3;
const CACHE_CLEANUP_THROTTLE = 10e3;
const CACHE_CLEANUP_NEEDED = CACHE_PREFIX + 'clean?';
const CACHE_EXCEPT_PROPS = ['css', 'discussions', 'additional_info'];
let searchTotalPages;
let searchCurrentPage = 1;
let searchExhausted = false;
let usoFrame;
let usoFrameQueue;
const processedResults = [];
const unprocessedResults = [];
let loading = false;
// Category for the active tab's URL.
let category;
let scrollToFirstResult = true;
let displayedPage = 1;
let totalPages = 1;
let totalResults = 0;
// fade-in when the entry took that long to replace its placeholder
const FADEIN_THRESHOLD = 50;
const dom = {};
Object.assign($('#find-styles-link'), {
href: getSearchPageURL(tabURL),
onclick(event) {
if (!prefs.get('popup.findStylesInline') || dom.container) {
handleEvent.openURLandHide.call(this, event);
return;
}
event.preventDefault();
this.textContent = this.title;
this.title = '';
init();
load();
},
});
return;
function init() {
setTimeout(() => document.body.classList.add(BODY_CLASS));
$('#find-styles-inline-group').classList.add('hidden');
dom.container = $('#search-results');
dom.container.dataset.empty = '';
dom.error = $('#search-results-error');
dom.nav = {};
const navOnClick = {prev, next};
for (const place of ['top', 'bottom']) {
const nav = $(`.search-results-nav[data-type="${place}"]`);
nav.appendChild(template.searchNav.cloneNode(true));
dom.nav[place] = nav;
for (const child of $$('[data-type]', nav)) {
const type = child.dataset.type;
child.onclick = navOnClick[type];
nav['_' + type] = child;
}
}
dom.list = $('#search-results-list');
addEventListener('scroll', loadMoreIfNeeded, {passive: true});
if (FIREFOX) {
let lastShift;
addEventListener('resize', () => {
const scrollbarWidth = window.innerWidth - document.scrollingElement.clientWidth;
const shift = document.body.getBoundingClientRect().left;
if (!scrollbarWidth || shift === lastShift) return;
lastShift = shift;
document.body.style.setProperty('padding',
`0 ${scrollbarWidth + shift}px 0 ${-shift}px`, 'important');
}, {passive: true});
}
addEventListener('styleDeleted', ({detail: {id}}) => {
const result = processedResults.find(r => r.installedStyleId === id);
if (result) {
result.installed = false;
result.installedStyleId = -1;
window.clearTimeout(result.pingbackTimer);
renderActionButtons($('#' + RESULT_ID_PREFIX + result.id));
}
});
addEventListener('styleAdded', ({detail: {style: {id, md5Url}}}) => {
const usoId = parseInt(md5Url && md5Url.match(/\d+|$/)[0]);
const result = usoId && processedResults.find(r => r.id === usoId);
if (result) {
result.installed = true;
result.installedStyleId = id;
renderActionButtons($('#' + RESULT_ID_PREFIX + usoId));
}
});
chromeLocal.getValue(CACHE_CLEANUP_NEEDED).then(value =>
value && debounce(cleanupCache, CACHE_CLEANUP_THROTTLE));
}
//endregion
//region Loader
/**
* Sets loading status of search results.
* @param {Boolean} isLoading If search results are idle (false) or still loading (true).
*/
function setLoading(isLoading) {
if (loading !== isLoading) {
loading = isLoading;
// Refresh elements that depend on `loading` state.
render();
}
}
function showSpinner(parent) {
parent = parent instanceof Node ? parent : $(parent);
parent.appendChild($create('.lds-spinner',
new Array(12).fill($create('div')).map(e => e.cloneNode())));
}
/** Increments displayedPage and loads results. */
function next() {
if (loading) {
debounce(next, 100);
return;
}
displayedPage += 1;
scrollToFirstResult = true;
render();
loadMoreIfNeeded();
}
/** Decrements currentPage and loads results. */
function prev() {
if (loading) {
debounce(next, 100);
return;
}
displayedPage = Math.max(1, displayedPage - 1);
scrollToFirstResult = true;
render();
}
/**
* Display error message to user.
* @param {string} message Message to display to user.
*/
function error(reason) {
dom.error.textContent = reason === 404 ? t('searchResultNoneFound') : reason;
dom.error.classList.remove('hidden');
dom.container.classList.toggle('hidden', !processedResults.length);
document.body.classList.toggle('search-results-shown', processedResults.length > 0);
if (dom.error.getBoundingClientRect().bottom < 0) {
dom.error.scrollIntoView({behavior: 'smooth', block: 'start'});
}
}
/**
* Initializes search results container, starts fetching results.
*/
function load() {
if (searchExhausted) {
if (!processedResults.length) {
error(404);
}
return;
}
setLoading(true);
dom.container.classList.remove('hidden');
dom.error.classList.add('hidden');
let pass = category ? 1 : 0;
category = category || getCategory();
search({category})
.then(function process(results) {
const data = results.data.filter(sameCategory);
pass++;
if (pass === 1 && !data.length) {
category = getCategory({keepTLD: true});
return search({category, restart: true}).then(process);
}
const numIrrelevant = results.data.length - data.length;
totalResults = results.current_page === 1 ? results.total_entries : totalResults;
totalResults = Math.max(0, totalResults - numIrrelevant);
totalPages = Math.ceil(totalResults / DISPLAY_PER_PAGE);
setLoading(false);
if (data.length) {
unprocessedResults.push(...data);
processNextResult();
} else if (numIrrelevant) {
load();
} else {
return Promise.reject(404);
}
})
.catch(error);
}
function loadMoreIfNeeded(event) {
let pageToPrefetch = displayedPage;
if (event instanceof Event) {
if ((loadMoreIfNeeded.prefetchedPage || 0) <= pageToPrefetch &&
document.scrollingElement.scrollTop > document.scrollingElement.scrollHeight / 2) {
loadMoreIfNeeded.prefetchedPage = ++pageToPrefetch;
} else {
return;
}
}
if (processedResults.length < pageToPrefetch * DISPLAY_PER_PAGE) {
setTimeout(load, DELAY_BEFORE_SEARCHING_STYLES);
}
}
/**
* Processes the next search result in `unprocessedResults` and adds to `processedResults`.
* Skips installed/non-applicable styles.
* Fetches more search results if unprocessedResults is empty.
* Recurses until shouldLoadMore() is false.
*/
function processNextResult() {
const result = unprocessedResults.shift();
if (!result) {
loadMoreIfNeeded();
return;
}
const md5Url = UPDATE_URL.replace('%', result.id);
API.styleExists({md5Url}).then(exist => {
if (exist) {
totalResults = Math.max(0, totalResults - 1);
} else {
processedResults.push(result);
render();
}
setTimeout(processNextResult, !exist && DELAY_AFTER_FETCHING_STYLES);
});
}
//endregion
//region UI
function render() {
let start = (displayedPage - 1) * DISPLAY_PER_PAGE;
const end = displayedPage * DISPLAY_PER_PAGE;
let plantAt = 0;
let slot = dom.list.children[0];
// keep rendered elements with ids in the range of interest
while (
plantAt < DISPLAY_PER_PAGE &&
slot && slot.id === 'search-result-' + (processedResults[start] || {}).id
) {
slot = slot.nextElementSibling;
plantAt++;
start++;
}
const plantEntry = entry => {
if (slot) {
dom.list.replaceChild(entry, slot);
slot = entry.nextElementSibling;
} else {
dom.list.appendChild(entry);
}
entry.classList.toggle('search-result-fadein',
!slot || performance.now() - slot._plantedTime > FADEIN_THRESHOLD);
return entry;
};
while (start < Math.min(end, processedResults.length)) {
plantEntry(createSearchResultNode(processedResults[start++]));
plantAt++;
}
for (const place in dom.nav) {
const nav = dom.nav[place];
nav._prev.disabled = displayedPage <= 1;
nav._next.disabled = displayedPage >= totalPages;
nav._page.textContent = displayedPage;
nav._total.textContent = totalPages;
}
// Fill in remaining search results with blank results + spinners
const maxResults = end > totalResults &&
totalResults % DISPLAY_PER_PAGE ||
DISPLAY_PER_PAGE;
while (plantAt < maxResults) {
if (!slot || slot.id.startsWith(RESULT_ID_PREFIX)) {
const entry = plantEntry(template.emptySearchResult.cloneNode(true));
entry._plantedTime = performance.now();
showSpinner(entry);
}
plantAt++;
if (!processedResults.length) {
break;
}
}
while (dom.list.children.length > maxResults) {
dom.list.lastElementChild.remove();
}
if (processedResults.length && 'empty' in dom.container.dataset) {
delete dom.container.dataset.empty;
}
if (scrollToFirstResult && (!FIREFOX || FIREFOX >= 55)) {
debounce(doScrollToFirstResult);
}
}
function doScrollToFirstResult() {
if (dom.container.scrollHeight > window.innerHeight * 2) {
scrollToFirstResult = false;
dom.container.scrollIntoView({behavior: 'smooth', block: 'start'});
}
}
/**
* Constructs and adds the given search result to the popup's Search Results container.
* @param {Object} result The SearchResult object from userstyles.org
*/
function createSearchResultNode(result) {
/*
userstyleSearchResult format: {
id: 100835,
name: "Reddit Flat Dark",
screenshot_url: "19339_after.png",
description: "...",
user: {
id: 48470,
name: "holloh"
},
style_settings: [...]
}
*/
const entry = template.searchResult.cloneNode(true);
Object.assign(entry, {
_result: result,
id: RESULT_ID_PREFIX + result.id,
});
Object.assign($('.search-result-title', entry), {
onclick: handleEvent.openURLandHide,
href: BASE_URL + result.url
});
const displayedName = result.name.length < 300 ? result.name : result.name.slice(0, 300) + '...';
$('.search-result-title span', entry).textContent = tWordBreak(displayedName);
const screenshot = $('.search-result-screenshot', entry);
let url = result.screenshot_url;
if (!url) {
url = BLANK_PIXEL_DATA;
screenshot.classList.add('no-screenshot');
} else if (/^[0-9]*_after.(jpe?g|png|gif)$/i.test(url)) {
url = BASE_URL + '/style_screenshot_thumbnails/' + url;
}
screenshot.src = url;
if (url !== BLANK_PIXEL_DATA) {
screenshot.classList.add('search-result-fadein');
screenshot.onload = () => {
screenshot.classList.remove('search-result-fadein');
};
}
const description = result.description
.replace(/<[^>]*>/g, ' ')
.replace(/([^.][.。?!]|[\s,].{50,70})\s+/g, '$1\n')
.replace(/([\r\n]\s*){3,}/g, '\n\n');
Object.assign($('.search-result-description', entry), {
textContent: description,
title: description,
});
Object.assign($('[data-type="author"] a', entry), {
textContent: result.user.name,
title: result.user.name,
href: BASE_URL + '/users/' + result.user.id,
onclick: handleEvent.openURLandHide,
});
let ratingClass;
let ratingValue = result.rating;
if (ratingValue === null) {
ratingClass = 'none';
ratingValue = '';
} else if (ratingValue >= 2.5) {
ratingClass = 'good';
ratingValue = ratingValue.toFixed(1);
} else if (ratingValue >= 1.5) {
ratingClass = 'okay';
ratingValue = ratingValue.toFixed(1);
} else {
ratingClass = 'bad';
ratingValue = ratingValue.toFixed(1);
}
$('[data-type="rating"]', entry).dataset.class = ratingClass;
$('[data-type="rating"] dd', entry).textContent = ratingValue;
Object.assign($('[data-type="updated"] time', entry), {
dateTime: result.updated,
textContent: formatDate(result.updated)
});
$('[data-type="weekly"] dd', entry).textContent = formatNumber(result.weekly_install_count);
$('[data-type="total"] dd', entry).textContent = formatNumber(result.total_install_count);
renderActionButtons(entry);
return entry;
}
function formatNumber(num) {
return (
num > 1e9 ? (num / 1e9).toFixed(1) + 'B' :
num > 10e6 ? (num / 1e6).toFixed(0) + 'M' :
num > 1e6 ? (num / 1e6).toFixed(1) + 'M' :
num > 10e3 ? (num / 1e3).toFixed(0) + 'k' :
num > 1e3 ? (num / 1e3).toFixed(1) + 'k' :
num
);
}
function renderActionButtons(entry) {
if (!entry) {
return;
}
const result = entry._result;
if (result.installed && !('installed' in entry.dataset)) {
entry.dataset.installed = '';
$('.search-result-status', entry).textContent = t('clickToUninstall');
} else if (!result.installed && 'installed' in entry.dataset) {
delete entry.dataset.installed;
$('.search-result-status', entry).textContent = '';
}
const screenshot = $('.search-result-screenshot', entry);
screenshot.onclick = result.installed ? onUninstallClicked : onInstallClicked;
screenshot.title = result.installed ? '' : t('installButton');
const uninstallButton = $('.search-result-uninstall', entry);
uninstallButton.onclick = onUninstallClicked;
const installButton = $('.search-result-install', entry);
installButton.onclick = onInstallClicked;
if ((result.style_settings || []).length > 0) {
// Style has customizations
installButton.classList.add('customize');
uninstallButton.classList.add('customize');
const customizeButton = $('.search-result-customize', entry);
customizeButton.dataset.href = BASE_URL + result.url;
customizeButton.dataset.sendMessage = JSON.stringify({method: 'openSettings'});
customizeButton.classList.remove('hidden');
customizeButton.onclick = function (event) {
event.stopPropagation();
handleEvent.openURLandHide.call(this, event);
};
}
}
function onUninstallClicked(event) {
event.stopPropagation();
const entry = this.closest('.search-result');
saveScrollPosition(entry);
API.deleteStyle(entry._result.installedStyleId)
.then(restoreScrollPosition);
}
/** Installs the current userstyleSearchResult into Stylus. */
function onInstallClicked(event) {
event.stopPropagation();
const entry = this.closest('.search-result');
const result = entry._result;
const installButton = $('.search-result-install', entry);
showSpinner(entry);
saveScrollPosition(entry);
installButton.disabled = true;
entry.style.setProperty('pointer-events', 'none', 'important');
// Fetch settings to see if we should display "configure" button
Promise.all([
fetchStyleJson(result),
fetchStyleSettings(result),
])
.then(([style, settings]) => {
pingback(result);
// show a 'config-on-homepage' icon in the popup
style.updateUrl += settings.length ? '?' : '';
return API.installStyle(style);
})
.catch(reason => {
const usoId = result.id;
console.debug('install:saveStyle(usoID:', usoId, ') => [ERROR]: ', reason);
error('Error while downloading usoID:' + usoId + '\nReason: ' + reason);
})
.then(() => {
$.remove('.lds-spinner', entry);
installButton.disabled = false;
entry.style.pointerEvents = '';
restoreScrollPosition();
});
function fetchStyleSettings(result) {
return result.style_settings ||
fetchStyle(result.id).then(style => {
result.style_settings = style.style_settings || [];
return result.style_settings;
});
}
}
function pingback(result) {
const wnd = window;
// FIXME: move this to background page and create an API like installUSOStyle
result.pingbackTimer = wnd.setTimeout(wnd.download, PINGBACK_DELAY,
BASE_URL + '/styles/install/' + result.id + '?source=stylish-ch');
}
function saveScrollPosition(entry) {
dom.scrollPosition = entry.getBoundingClientRect().top;
dom.scrollPositionElement = entry;
}
function restoreScrollPosition() {
const t0 = performance.now();
new MutationObserver((mutations, observer) => {
if (performance.now() - t0 < 1000) {
window.scrollBy(0, dom.scrollPositionElement.getBoundingClientRect().top - dom.scrollPosition);
}
observer.disconnect();
}).observe(document.body, {childList: true, subtree: true, attributes: true});
}
//endregion
//region USO API wrapper
function getSearchPageURL() {
const category = getCategory();
return BASE_URL +
'/styles/browse/' +
(category in CATEGORY_FALLBACK ? '?search_terms=' : '') +
category;
}
/**
* Resolves the Userstyles.org "category" for a given URL.
*/
function getCategory({keepTLD} = {}) {
const u = tryCatch(() => new URL(tabURL));
if (!u) {
// Invalid URL
return '';
} else if (u.protocol === 'file:') {
return 'file:';
} else if (u.protocol === location.protocol) {
return 'Stylus';
} else {
// Website address, strip TLD & subdomain
const [, category = u.hostname, tld = ''] = u.hostname.match(RX_CATEGORY) || [];
const categoryWithTLD = category + '.' + tld;
const fallback = CATEGORY_FALLBACK[categoryWithTLD];
return fallback === true && categoryWithTLD || fallback || category + (keepTLD ? tld : '');
}
}
function sameCategory(result) {
return result.subcategory && (
category === result.subcategory ||
category === 'Stylus' && /^(chrome|moz)-extension$/.test(result.subcategory) ||
category.replace('.', '').toLowerCase() === result.subcategory.replace('.', '').toLowerCase()
);
}
/**
* Fetches the JSON style object from userstyles.org (containing code, sections, updateUrl, etc).
* Stores (caches) the JSON within the given result, to avoid unnecessary network usage.
* Style JSON is fetched from the /styles/chrome/{id}.json endpoint.
* @param {Object} result A search result object from userstyles.org
* @returns {Promise<Object>} Promises the response as a JSON object.
*/
function fetchStyleJson(result) {
return Promise.resolve(
result.json ||
downloadInFrame(BASE_URL + '/styles/chrome/' + result.id + '.json').then(json => {
result.json = json;
return json;
}));
}
/**
* Fetches style information from userstyles.org's /api/v1/styles/{ID} API.
* @param {number} userstylesId The internal "ID" for a style on userstyles.org
* @returns {Promise<Object>} An object containing info about the style, e.g. name, author, etc.
*/
function fetchStyle(userstylesId) {
return readCache(userstylesId).then(json =>
json ||
downloadInFrame(BASE_URL + '/api/v1/styles/' + userstylesId).then(writeCache));
}
/**
* Fetches (and JSON-parses) search results from a userstyles.org search API.
* Automatically sets searchCurrentPage and searchTotalPages.
* @param {string} category The usrestyles.org "category" (subcategory) OR a any search string.
* @return {Object} Response object from userstyles.org
*/
function search({category, restart}) {
if (restart) {
searchCurrentPage = 1;
searchTotalPages = undefined;
}
if (searchTotalPages !== undefined && searchCurrentPage > searchTotalPages) {
return Promise.resolve({'data':[]});
}
const searchURL = BASE_URL +
'/api/v1/styles/subcategory' +
'?search=' + encodeURIComponent(category) +
'&page=' + searchCurrentPage +
'&country=NA';
const cacheKey = category + '/' + searchCurrentPage;
return readCache(cacheKey)
.then(json =>
json ||
downloadInFrame(searchURL).then(writeCache))
.then(json => {
searchCurrentPage = json.current_page + 1;
searchTotalPages = json.total_pages;
searchExhausted = (searchCurrentPage > searchTotalPages);
return json;
}).catch(reason => {
searchExhausted = true;
return Promise.reject(reason);
});
}
//endregion
//region Cache
function readCache(id) {
const key = CACHE_PREFIX + id;
return chromeLocal.getValue(key).then(item => {
if (!cacheItemExpired(item)) {
return chromeLocal.loadLZStringScript().then(() =>
tryJSONparse(LZString.decompressFromUTF16(item.payload)));
} else if (item) {
chrome.storage.local.remove(key);
}
});
}
function writeCache(data, debounced) {
data.id = data.id || category + '/' + data.current_page;
for (const prop of CACHE_EXCEPT_PROPS) {
delete data[prop];
}
if (!debounced) {
// using plain setTimeout because debounce() replaces previous parameters
setTimeout(writeCache, 100, data, true);
return data;
} else {
chromeLocal.setValue(CACHE_CLEANUP_NEEDED, true);
debounce(cleanupCache, CACHE_CLEANUP_THROTTLE);
return chromeLocal.loadLZStringScript().then(() =>
chromeLocal.setValue(CACHE_PREFIX + data.id, {
payload: LZString.compressToUTF16(JSON.stringify(data)),
date: Date.now(),
})).then(() => data);
}
}
function cacheItemExpired(item) {
return !item || !item.date || Date.now() - item.date > CACHE_DURATION;
}
function cleanupCache() {
chromeLocal.remove(CACHE_CLEANUP_NEEDED);
if (chrome.storage.local.getBytesInUse) {
chrome.storage.local.getBytesInUse(null, size => {
if (size > CACHE_SIZE) {
chrome.storage.local.get(null, cleanupCacheInternal);
}
ignoreChromeError();
});
} else {
chrome.storage.local.get(null, cleanupCacheInternal);
}
}
function cleanupCacheInternal(storage) {
const sortedByTime = Object.keys(storage)
.filter(key => key.startsWith(CACHE_PREFIX))
.map(key => Object.assign(storage[key], {key}))
.sort((a, b) => a.date - b.date);
const someExpired = cacheItemExpired(sortedByTime[0]);
const expired = someExpired ? sortedByTime.filter(cacheItemExpired) :
sortedByTime.slice(0, sortedByTime.length / 2);
const toRemove = expired.length ? expired : sortedByTime;
if (toRemove.length) {
chrome.storage.local.remove(toRemove.map(item => item.key), ignoreChromeError);
}
ignoreChromeError();
}
//endregion
//region USO referrer spoofing via iframe
function downloadInFrame(url) {
return usoFrame ? new Promise((resolve, reject) => {
const id = performance.now();
const timeout = setTimeout(() => {
const {reject} = usoFrameQueue.get(id) || {};
usoFrameQueue.delete(id);
if (reject) reject();
}, 10e3);
const data = {url, resolve, reject, timeout};
usoFrameQueue.set(id, data);
usoFrame.contentWindow.postMessage({xhr: {id, url}}, '*');
}) : setupFrame().then(() => downloadInFrame(url));
}
function setupFrame() {
usoFrame = $create('iframe', {src: BASE_URL});
usoFrameQueue = new Map();
const stripHeaders = info => ({
responseHeaders: info.responseHeaders.filter(({name}) => !/^X-Frame-Options$/i.test(name)),
});
chrome.webRequest.onHeadersReceived.addListener(stripHeaders, {
urls: [BASE_URL + '/'],
types: ['sub_frame'],
}, [
'blocking',
'responseHeaders',
]);
let frameId;
const stripResources = info => {
if (!frameId &&
info.frameId &&
info.type === 'sub_frame' &&
(info.initiator === location.origin || !info.initiator) && // Chrome 63+
(info.originUrl === location.href || !info.originUrl) && // FF 48+
info.url === BASE_URL + '/') {
frameId = info.frameId;
} else if (frameId === info.frameId && info.type !== 'xmlhttprequest') {
return {redirectUrl: 'data:,'};
}
};
chrome.webRequest.onBeforeRequest.addListener(stripResources, {
urls: ['<all_urls>'],
}, [
'blocking',
]);
setTimeout(() => {
chrome.webRequest.onBeforeRequest.removeListener(stripResources);
}, 10e3);
window.addEventListener('message', ({data, origin}) => {
if (!data || origin !== BASE_URL) return;
const {resolve, reject, timeout} = usoFrameQueue.get(data.id) || {};
if (!resolve) return;
chrome.webRequest.onBeforeRequest.removeListener(stripResources);
usoFrameQueue.delete(data.id);
clearTimeout(timeout);
// [being overcautious] a string response is used instead of relying on responseType=json
// because it was invoked in a web page context so another extension may have incorrectly spoofed it
const json = tryJSONparse(data.response);
if (json && data.status < 400) {
resolve(json);
} else {
reject(data.status);
}
});
return new Promise((resolve, reject) => {
const done = event => {
chrome.webRequest.onHeadersReceived.removeListener(stripHeaders);
(event.type === 'load' ? resolve : reject)();
usoFrameQueue.forEach(({url}, id) => {
usoFrame.contentWindow.postMessage({xhr: {id, url}}, '*');
});
};
usoFrame.addEventListener('load', done, {once: true});
usoFrame.addEventListener('error', done, {once: true});
usoFrame.style.setProperty('display', 'none', 'important');
document.body.appendChild(usoFrame);
});
}
//endregion
});