2012-11-26 36 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));