当网址使用javascript时,Chrome扩展程序tab.url返回空

时间:2017-01-11 11:38:57

标签: javascript url google-chrome-extension tabs href

我正在使用一个包含< iframe >的网页持有大学网站。

但是,大学网站上的一些链接会在浏览器中打开一个新标签,通常不是问题,但我的网页将以全屏模式显示在55英寸的触摸屏上。因此,我的网页将被阻止通过新标签。

所以,现在我正在使用Chrome扩展程序来处理新标签页。我 修改 来自互联网的Extension之一关闭并将新标签重定向回< iframe >使用内容脚本背景

大多数新标签的工作。 (不是所有新标签)
因为某些< a > s'href正在使用像这样的javascript:

<a href="javascript:popupWin=window.open('http://www.ce.ust.hk/Web/EventDetail.aspx?EventId=547', 'Popup', 'toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=754,height=700');popupWin.focus();" class="ilink">High Hybrid Reinforced Soil Slope as Runway Support - Tana Toraja Airport Case Study</a>


tab.url没有返回任何内容(它返回空字符串)。 alert()喜欢

alert('link:'+tab.url)

此。将成为这个:

alert('link:'+'')


无论如何要解决这个问题?


----代码参考----

的manifest.json

{
https://clients2.google.com/service/update2/crx",
  "manifest_version": 2,

  "name": "Inteset Chrome Kiosk Helper",
  "short_name": "Kiosk Helper",
  "description": "Traps network errors. Shows user friendly error page allowing user to return to previous page. Blocks new windows and new tabs.",
  "version": "1.1.12",
  "options_page": "options.html",

  "options_ui": {
    "page": "options.html",
    "chrome_style": true
  },
   "content_scripts": [
    {
      "matches": ["http://localhost/newyear/index.php"],
      "js": ["myjs.js"]
    }
  ],
  "background": {
    "persistent": false,
    "scripts": ["background.js"]
  },

  "web_accessible_resources": [
    "style.css",
    "popup.js",
    "options.js"
  ],

  "permissions": [
    "webNavigation",
    "tabs", 
    "http://*/*", "https://*/*",
    "history",
    "activeTab",
    "storage"
  ],

  "icons": { 
        "16" : "Logo16x16.png",
        "48" : "Logo48x48.png",
        "128": "Logo128x128.png" 
    }
}




background.js
修改里面的代码
chrome.tabs.onCreated.addListener(...)

chrome.webNavigation.onErrorOccurred.addListener(function (data) {
    var errs = "net::ERR_BLOCKED_BY_ADMINISTRATOR;net::ERR_NAME_NOT_RESOLVED;net::ERR_CONNECTION_FAILED;net::ERR_CONNECTION_RESET;net::ERR_EMPTY_RESPONSE;net::ERR_FAILED;net::ERR_CONNECTION_REFUSED;net::ERR_INTERNET_DISCONNECTED";

    if (errs.indexOf(data.error) != -1) {
        var Page = GetPageName( data.error )

        chrome.tabs.update(data.tabId, {
            url: Page
        });
    }
});

chrome.tabs.onCreated.addListener(function (tab) {
    chrome.tabs.query(
        { currentWindow: true },
        function (tabs) {
            if (tabs.length > 1) {
                chrome.storage.local.get({
                    blockWindows: false,
                    blockTabs: false
                }, function (items) {

                    chrome.windows.getCurrent({}, function (window) {
                        if (window.state == "fullscreen"||true) {
                            if (items.blockTabs) {
                                setTimeout(function(){ 
                                    var _url = tabs[tabs.length - 1].url;
                                    alert("New tabs [" + tabs.length + "]   { " + _url + " }");
                                    for(var i=0;i<tabs.length;i++){
                                    //  alert(tabs[i].url);
                                        if(tabs[i].url == "http://localhost/UST_VW_web/index.php"){
                                            alert('bg found url ' + tabs[i].url + '\n' + _url);
                                            chrome.tabs.sendMessage(tabs[i].id, {new_link:_url }, function(response) {
                                                //alert('bg response ');
                                            });
                                        }
                                    }
                                    chrome.tabs.remove(tab.id);
                                },10000);
                            }
                        }
                    });


                });
            }
        }
    );
});

chrome.windows.onCreated.addListener(function (window) {
    chrome.windows.getAll({populate: true}, function (windows) {
        if (windows.length > 1) {
            chrome.storage.local.get({
                blockWindows: false,
                blockTabs: false
            }, function (items) {

                if (items.blockWindows) {
                    // check if at least one window is in full screen
                    for (var i = 0; i < windows.length; i++) {
                        if (windows[i].state == "fullscreen") {
                            chrome.windows.remove(window.id);
                            alert("New windows are not allowed by administrator. :)");
                            RedirectNewPage("new Win", window.url);
                            break;
                        }
                    }
                }

            });

        }
    });
});


function RedirectNewPage(caller,url){
    alert("RedirectNewPage");
    var tabs_str = "";
chrome.windows.getAll({populate:true},function(windows){
    windows.forEach(function(window){
        window.tabs.forEach(function(tab){
            //collect all of the urls here, I will just log them instead
            console.log(tab.url);
        });
    });
});

    chrome.windows.getAll({populate: true}, function (windows) {
        windows.forEach(function(window){
            window.tabs.forEach(function(tab){
                console.log(tab.url);
                tabs_str += (tab.id + "\t");
            });
        });

    });
    alert("tabs  "+tabs_str);
}

function GetPageName( error ) {
    var ErrText = new String(error);

    if (ErrText.indexOf("net::ERR_BLOCKED_BY_ADMINISTRATOR") >= 0)
        return "Error-Blocked-by-Administrator.html";
    if (ErrText.indexOf("net::ERR_NAME_NOT_RESOLVED") >= 0)
        return "Error-Name-not-Resolved.html";
    if (ErrText.indexOf("net::ERR_CONNECTION_FAILED") >= 0)
        return "Error-Connectoin-Failed.html";
    if (ErrText.indexOf("net::ERR_CONNECTION_RESET") >= 0)
        return "Error-Connection-Reset.html";
    if (ErrText.indexOf("net::ERR_EMPTY_RESPONSE") >= 0)
        return "Error-Empty-Response.html";
    if (ErrText.indexOf("net::ERR_FAILED") >= 0)
        return "Error.html";
    if (ErrText.indexOf("net::ERR_CONNECTION_REFUSED") >= 0)
        return "Error-Connection-Refused.html";
    if (ErrText.indexOf("net::ERR_INTERNET_DISCONNECTED") >= 0)
        return "Error-Internet-Disconnected.html";

    return "Error.html";
}





myjs.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    document.getElementById("mainframe").src = request.new_link;
    alert("GET 2 --- " + document.getElementById("mainframe").name);
    alert(request.new_link);
    //sendResponse(myFunc(request.args));
});

0 个答案:

没有答案