当我点击一个链接时,如何阻止雅虎将“href”更改为“dirtyhref”?

时间:2012-11-26 07:19:24

标签: jquery greasemonkey

我是中国互联网用户。 Google / Yahoo搜索引擎在我的国家非常不稳定 当我点击雅虎搜索结果链接时,我经常会收到此错误页面:

ERROR
The requested URL could not be retrieved    
While trying to retrieve the URL: http://search.yahoo.com/r/_ylt=A0oGdUY7FbNQFQsA5rZXNyoA;_ylu=X3oDMTE0ODJ2YTduBHNlYwNzcgRwb3MDMQRjb2xvA3NrMQR2dGlkA1ZJUDA1MV83Ng--/SIG=11ac0sa5q/EXP=1353942459/**http%3a//www.google.com/

The following error was encountered:    
    Access Denied.    
    Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect. 

Your cache administrator is noc_admin@163.com.
by DXT-GZ-APP02 

我注意到当我点击链接时,雅虎会自动将href的值更改为dirtyhref
我尝试$('a[id|=link]').unbind('click mousedown'),但它不起作用。
如何阻止雅虎这样做?


目前,我使用这个firefox greasemonkey代码:

// ==UserScript==
// @name        Clean URL
// @namespace   http://hjkl.me
// @include     https://www.google.com/search*
// @include     http://search.yahoo.com/search*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @version     1
// ==/UserScript==

// GOOGLE
$('h3.r>a').removeAttr('onmousedown');

// YAHOO
$('a[id|=link]').on('click', function(){
    var url = $(this).attr('dirtyhref').split('**')[1];
    url = decodeURIComponent(url);
    $(this).attr('href', url);  //<-- yahoo will change it back!
    window.open(url, '_blank');
    return false;
});

问题是:我无法使用鼠标中键单击功能。 (静默打开标签页)

3 个答案:

答案 0 :(得分:2)

通常,人们只需将“好”href值复制到错误/跟踪dirtyhref属性,然后让雅虎做到这一点。

如今,只需清理这两个值。

这是一个似乎对我有用的AJAX处理脚本:

// ==UserScript==
// @name        Clean URL
// @namespace   http://hjkl.me
// @include     http://search.yahoo.com/search*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
// @version     1
// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

// GOOGLE
$('h3.r>a').removeAttr('onmousedown');

// YAHOO
waitForKeyElements ("a[id|=link]", fixDirtyLinks);

function fixDirtyLinks (jNode) {
    var url = jNode.attr('dirtyhref').split('**');
    if (url.length > 1) {
        var goodURL = decodeURIComponent (url[1]);
        jNode.attr ('href', goodURL);
        jNode.attr ('dirtyhref', goodURL);
    }
}

答案 1 :(得分:1)

IE9 +,Opera和Firefox定义DOMAttrModified事件。遗憾的是,当前版本的Webkit没有定义此事件。

只要href的任何属性发生变化,它就会覆盖a[id|=link]的{​​{1}}属性值dirtyhref。请注意,为属性分配其当前值不会计为更改:

$('a[id|=link]').on('DOMAttrModified', function(){
    $(this).attr("href", $(this).attr("dirtyhref"));
});

测试:http://jsfiddle.net/ZzJae/

您还需要覆盖页面加载上的链接。

如果新链接可能会持续显示(例如,存在AutoPager),您可能还需要绑定DOMNodeInserted并使用事件委派:$(document).on("DOM...", "a[id|=link]", handler)

IE9 +,Chrome + Safari和Firefox定义DOMSubtreeModified,但Opera不会。如果您想添加Webkit支持,您还需要收听此事件。

最终解决方案的草图(仅限firefox):

(function(){
  function overrideOne(){
    var dirty = $(this).attr("dirtyhref");
    var clean = dirty.split("**")[1];
    $(this).attr("href", clean);
  }
  function overrideAll(){
    $("a[id|=link]").each(overrideOne)
  }

  $(document).on("ready DOMNodeInserted", overrideAll);
  $(document).on("DOMAttrChanged", "a[id|=link]", overrideOne);
  $(document).on("click", "a[id|=link]",function(){
     ...
  }
}

答案 2 :(得分:1)

基本上,您需要删除dirtyhref属性。为了防止href变脏,您的dirtyhref删除功能必须在Yahoo!的功能之前运行。为此,请使用鼠标事件捕获事件。

这是我在Opera中使用的用户JS:

停用Yahoo!搜索重写:

https://gist.github.com/XP1/5008515/

// ==UserScript==
// @name Disable Yahoo! Search rewrite
// @version 1.00
// @description Disables the rewrite of links in Yahoo! Search results.
// @namespace https://gist.github.com/XP1/5008515/
// @copyright 2013
// @author XP1
// @homepage https://github.com/XP1/
// @license Apache License, Version 2.0; http://www.apache.org/licenses/LICENSE-2.0
// @include http*://search.yahoo.*/search?*
// @include http*://*.search.yahoo.*/search?*
// ==/UserScript==

/*
 * Copyright 2013 XP1
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*jslint browser: true, vars: true, maxerr: 50, indent: 4 */
(function (window, MouseEvent, HTMLElement) {
    "use strict";

    function disableRewrite(event) {
        if (!(event instanceof MouseEvent)) {
            return;
        }

        var target = event.target;

        var current = null;
        for (current = target; current instanceof HTMLElement; current = current.parentNode) {
            if (current.hasAttribute("dirtyhref")) {
                current.removeAttribute("dirtyhref");
                return;
            }
        }
    }

    window.addEventListener("mousedown", disableRewrite, true);
    window.addEventListener("click", disableRewrite, true);
}(this, this.MouseEvent, this.HTMLElement));