首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用润滑脂/篡改猴子,我如何才能在Dictionary.com open Thesaurus.com上获得当前查看的定义?

使用润滑脂/篡改猴子,我如何才能在Dictionary.com open Thesaurus.com上获得当前查看的定义?
EN

Stack Overflow用户
提问于 2017-03-06 23:48:32
回答 1查看 195关注 0票数 0

使用Greasemonkey或Tamper猴子,如何在Dictionary.com打开Thesaurus.com (反之亦然)时查看当前的以红色标出的链接被单击。定义。

我看到我可以使用window.location.pathname检索“/浏览/测试”或搜索的任何单词,然后我可以将它们放在相反链接的主机名上。

Thesaurus.com链接的HTML:<a href="http://www.thesaurus.com/" data-linkid="qxnxzj">Thesaurus.com</a>

Dictionary.com链接的HTML:<a href="http://www.dictionary.com/" data-linkid="tqks0v">Dictionary.com</a>

所以我要用document.querySelectorAll("a[href='http://www.thesaurus.com']");document.querySelectorAll('[data-linkid=qxnxzj]');来选择它

我担心的是后一种选择方法,以防数据链接被公司的web开发人员更改。

然而,最终,我仍然不知道如何实现这一点。我是在监听document上的所有点击,还是仅仅在<a>标签上,等等?做这件事最有效的方法是什么?

此外,如何在ctrl+clicked时在新选项卡中打开这些链接,或者在shift+clicked时打开新窗口?目前,它在同一个浏览器选项卡中打开ctrl+clicked和shift+clicked链接。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-07 21:34:10

以下代码能够充分实现我问题中概述的所有目标:

代码语言:javascript
复制
// ==UserScript==
// @name         Restore links behavior at Thesaurus.com
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        *://www.thesaurus.com/*
// @match        *://www.dictionary.com/*
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    document.querySelectorAll('[data-linkid]').forEach(el => el.removeAttribute('data-linkid'));

    document.addEventListener('click', function(e) {
        var link = e.target.closest('a');
        //var link = e.target.closest('#content a'); // limit to the #content area
        var path = window.location.pathname; // /browse/myword

        if (link && link.getAttribute('href') != '#') {
            e.stopPropagation();
        }

        if (link && link.getAttribute('href') == 'http://www.thesaurus.com/') {
            link.setAttribute('href', 'http://www.thesaurus.com' + path);
        }
        else if (link && link.getAttribute('href') == 'http://www.dictionary.com/') {
            link.setAttribute('href', 'http://www.dictionary.com' + path);
        }
    }, true);

})();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42637715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档