IMDb - Set default user reviews sorting

Select which sorting option should be used by default when click on a link "User reviews"

目前為 2021-10-08 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         IMDb - Set default user reviews sorting
// @namespace    https://github.com/Procyon-b
// @version      0.5
// @description  Select which sorting option should be used by default when click on a link "User reviews"
// @author       Achernar
// @include      /^https:\/\/www\.imdb\.com\/title\/[^\/]+\/(reference\/?)?(\?.*)?$/
// @run-at       document-end
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
"use strict";

var L, sort, c={ 'helpfulnessScore':'Helpfulness', 'submissionDate':'Review date', 'totalVotes':'Total votes', 'reviewVolume':'Prolific reviewer', 'userRating':'Review rating'};

function fix() {
  L=document.querySelectorAll('a[href*="/title/"][href*="/reviews"], a[href^="reviews"]');
  sort=GM_getValue('sort','helpfulnessScore');

  L.forEach(function(e){
    let H=e.href.split('?');
    if (!H[0].endsWith('reviews') && !H[0].endsWith('reviews/')) return;
    let s=H[1] || '';
    let r=[];
    s.split('&').forEach(function(e){
      var p=e.split('=');
      if (p[0]) r[p.shift().trim()]=p.join('=');
      });
    if (sort=='unset') delete r.sort;
    else r.sort=sort;
    let r2=[];
    for (let k in r) { r2.push(k+'='+r[k]); }

    H[1]=r2.join('&');
    if (H[1]=='') H.pop();
    e.href=H.join('?');
    });

  }

fix();


var dialog=document.createElement('div'); 
dialog.id='fix_def_sort';
dialog.innerHTML=`<style>
#fix_def_sort {
  position: absolute;
  color: black;
  background-color:white;
  top: 4em;
  z-index: 100 !important;
  font-size: 13px;
  font-family: arial;
  padding: 3px 8px;
  border: 2px solid gray;
  min-width: 23em;
  text-align: left;
}
#fix_def_sort, #fix_def_sort * {
  white-space: nowrap;
}
#fix_def_sort #close {
  float: right;
  color: red;
  cursor: pointer;
  z-index: 10;
}
.reviewSortSettings {
  position: relative !important;
}
</style><b>Select default user reviews sorting</b> &nbsp; <div id="close">&#10062;</div><br>
<select><option value="unset">[ unset ]</option></select>
`;

dialog.querySelector('#close').onclick=function(){
  dialog.parentNode.classList.toggle('reviewSortSettings',false);
  dialog.remove();
  };

var E, sel=dialog.querySelector('select');
if (sel) {
  for (let k in c) {
    sel.innerHTML+='<option value="'+k+'"'+(k==sort?' selected':'')+'>'+c[k]+'</option>';
    }
  sel.onchange=function(){
    GM_setValue('sort',this.value);
    fix();
    }
  }

if (L.length) 
  for (let i=0; E=L[i]; i++) {
    E.addEventListener('click', function(ev){
      if (ev.ctrlKey && ev.altKey) {
        this.parentNode.appendChild(dialog);
        this.parentNode.classList.toggle('reviewSortSettings',true);
        ev.preventDefault();
        ev.stopPropagation();
        }
      }, {capture:true});
    }


})();