DQ Client (for DeepQuery Secure)

客户端:把查询请求放到 GM 通道,等待结果返回

Versione datata 04/09/2025. Vedi la nuova versione l'ultima versione.

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greatest.deepsurf.us/scripts/548365/1654687/DQ%20Client%20%28for%20DeepQuery%20Secure%29.js

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         DQ Client (for DeepQuery Secure)
// @namespace    dq.secure.client
// @version      1.0.0
// @description  客户端:把查询请求放到 GM 通道,等待结果返回
// @match        http://*/*
// @match        https://*/*
// @include      about:blank
// @run-at       document-start
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_addValueChangeListener
// @grant        GM_removeValueChangeListener
// @grant        GM_getTab
// @grant        GM_saveTab
// ==/UserScript==

(function(){
  'use strict';
  const now = () => Date.now();
  const rid = () => (now().toString(36) + Math.random().toString(36).slice(2,8)).toUpperCase();

  function getTabId(){
    return new Promise(resolve=>{
      GM_getTab(tab=>{
        if(!tab) tab = {};
        if(!tab.__dq_tab_id__){ tab.__dq_tab_id__ = rid(); try{ GM_saveTab(tab); }catch{} }
        resolve(tab.__dq_tab_id__);
      });
    });
  }

  async function request(spec){
    const TAB_ID = await getTabId();
    const reqId = rid();
    const KEY_REQ = `DQ_REQ:${TAB_ID}`;
    const KEY_RES = `DQ_RES:${TAB_ID}:${reqId}`;

    return await new Promise((resolve)=>{
      const lid = GM_addValueChangeListener(KEY_RES, (name, oldVal, val, remote)=>{
        if(!remote) return;
        try{ GM_removeValueChangeListener(lid); }catch{}
        try{ GM_setValue(name, null); }catch{}
        resolve(val);
      });
      GM_setValue(KEY_REQ, { tabId: TAB_ID, reqId, spec, at: Date.now() });
    });
  }

  // 语法糖,保持与旧 API 一致
  const DQ = {
    get: (spec)=>request(spec),
    attr:  ({framePath, chain, name, timeout, frameTimeout}) => request({framePath, chain, timeout, frameTimeout, pick: {attr: name}}),
    prop:  ({framePath, chain, name, timeout, frameTimeout}) => request({framePath, chain, timeout, frameTimeout, pick: {prop: name}}),
    text:  ({framePath, chain, timeout, frameTimeout}) => request({framePath, chain, timeout, frameTimeout, pick: {text: true}}),
    html:  ({framePath, chain, timeout, frameTimeout}) => request({framePath, chain, timeout, frameTimeout, pick: {html: true}}),
    rect:  ({framePath, chain, timeout, frameTimeout}) => request({framePath, chain, timeout, frameTimeout, pick: {rect: true}})
  };

  // 暴露到当前脚本的沙箱(不是 page)
  window.DQ = DQ;

  try{ console.debug('[DQ Client] ready: use await DQ.get({...})'); }catch{}
})();