2012-11-26 37 views
1

我是中国互联网用户。 Google/Yahoo搜索引擎在我的国家非常不稳定。
当我点击雅虎搜索结果的链接,我经常会收到这样的错误页面:如何在单击链接时将“href”更改为“dirtyhref”?

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 [email protected] 
by DXT-GZ-APP02 

我注意到,雅虎将改变hrefdirtyhref值自动当我点击一个链接。我试图$('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; 
}); 

的问题是:我不能使用鼠标中键单击功能。 (无声打开tabpage)

+0

改回手动? –

+0

当我将它改回去时,雅虎将会再次改变它。 – kev

+0

您是否在单独的窗口(无导航)或搜索窗口中打开链接(在导航回时更改)? –

回答

1

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

只要其任何属性发生变化,就会覆盖属性a[id|=link]的值为dirtyhref。请注意,将一个属性指定的当前值不能算作一个变化:

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

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

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

如果新的链接可能连续出现(比如,自动翻页时),你可能还需要结合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(){ 
    ... 
    } 
} 
+0

[突变事件已弃用](http://www.w3.org/TR/DOM-Level-3-Events/#events-mutationevents)。这种方法会有问题,并且会吸取CPU周期和内存。 (并且它将不能用于FF的未来版本。) –

+0

@BrockAdams感谢您的链接。请注意,_only_表示“MutationEvent”接口已被弃用 - 事件本身似乎仍然存在,至少在引入DOM4之前。我不使用界面(也许我应该?),我只听事件。 –

+0

它工作得很好。谢谢! – kev

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

基本上,你需要做的是去除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));