1

我是中国网民。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;
});

问题是:我不能使用鼠标中键功能。(默默打开标签页)

4

3 回答 3

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);
    }
}
于 2012-11-26T09:00:05.637 回答
1

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

每当其任何属性更改时,这都会将href属性覆盖a[id|=link]到值。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(){
     ...
  }
}
于 2012-11-26T08:41:09.023 回答
1

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

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

禁用雅虎!搜索重写:

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));
于 2013-02-21T21:54:11.657 回答