在后台运行Javascript-Jquery,不要打开窗口

时间:2019-10-30 02:14:41

标签: javascript jquery google-chrome google-chrome-extension

我正在使用以下代码通过chrome浏览器单击超链接来拨打电话。效果很好。

唯一的问题是,单击电话号码后会打开一个新窗口。

理想情况下,根本不会出现弹出窗口,或者X毫秒/秒后窗口会自动关闭。

Window.close似乎不起作用。

有人知道一种解决方法是在后台处理所有内容时完全不打开窗口吗?

// load options
chrome.extension.sendRequest({action: 'options'}, function(response) {

    var url = "https://" +
    response.options.domain +
    "/app/click_to_call/click_to_call.php?" +
    //'username=' + response.options.username +
    //'&password=' + response.options.password +
    'src_cid_name=' + response.options.src_cid_name +
    '&src_cid_number=' + response.options.src_cid_number +
    '&dest_cid_name=' + response.options.dest_cid_name +
    '&dest_cid_number=' + response.options.dest_cid_number +
    '&src=' + response.options.src +

    '&rec=' + response.options.rec +
    '&ringback=' + response.options.ringback;

    var inject = [  'function WebDial(dest) {',
                    '   win=window.open("","Dialing","height=480,width=800");',
                    '   if (window.focus) {',
                    '       win.focus();',
                    '   }',

                    '   var form = document.createElement("form");',
                    '   form.setAttribute("method", "post");',
                    '   form.setAttribute("action", "'+url+'&dest=\"+dest);',
                    '   form.setAttribute("target", "Dialing");',

                    '   var userField = document.createElement("input");',
                    '   userField.setAttribute("type", "hidden");',
                    '   userField.setAttribute("name", "username");',
                    '   userField.setAttribute("value", "'+response.options.username+'");',
                    '   form.appendChild(userField);',

                    '   var passField = document.createElement("input");',
                    '   passField.setAttribute("type", "hidden");',
                    '   passField.setAttribute("name", "password");',
                    '   passField.setAttribute("value", "'+response.options.password+'");',
                    '   form.appendChild(passField);',

                    '   document.body.appendChild(form);',

                    //  window.open('', 'view');

                    '   form.submit();',
                    '   form.parentNode.removeChild(form);',

                    '   return false;',
                    '}'
                ].join('\n');

    var script = document.createElement('script');
    script.textContent = inject;
    (document.head||document.documentElement).appendChild(script);
    script.parentNode.removeChild(script);

    // the localStorage mechanism converts the regex to a string, so we have to convert it back
    var stripper = /^\/|\/$/g;
    var intlRegex = RegExp(response.options.intlRegex.replace(stripper, ''), 'gm');
    var homeRegex = RegExp(response.options.homeRegex.replace(stripper, ''), 'gm');


    var vopts = "'Popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=420,height=400,left=430,top=23'";

    var intlReplacement = '<a href="javascript:void(0);" onclick="WebDial(\''+response.options.intlReplacement+'\');">$&</a>';
    //'<a href="'+url+'&dest='+response.options.intlReplacement+'">$&</a>';
    var homeReplacement = '<a href="javascript:void(0);" onclick="WebDial(\''+response.options.homeReplacement+'\');">$&</a>';
    //'<a href="'+url+'&dest='+response.options.homeReplacement+'">$&</a>';

    var found = false;

    // Test the text of the body element against our international regular expression.
    if (intlRegex.test(document.body.innerText)) {
        $(document).find(':not(textarea)').replaceText( intlRegex, intlReplacement );
        found = true;
    }
    // Test the text of the body element against our home regular expression.
    if (homeRegex.test(document.body.innerText)) {
        $(document).find(':not(textarea)').replaceText( homeRegex, homeReplacement );
        found = true;
    }
    if (found) {
        // Notify the background page to update the page icon
        chrome.extension.sendRequest({action: 'showPageAction'}, function() {});
    }
});

// Check that options are set. If not, set them using the defaults from options.js
for (key in defaults) {
    if (!localStorage[key]) {
        localStorage[key] = defaults[key];
    }
}

// Called when a message is passed.
function onRequest(request, sender, sendResponse) {

    if (request.action == 'options') {
        // Send the localStorage variable to the content script so that it
        // can use the options set in the options page
        sendResponse({ options: localStorage });
    } else if (request.action == 'showPageAction') {
        // Show the page action for the tab that the sender (content script)
        // was on.
        chrome.pageAction.show(sender.tab.id);
    }
    // Return nothing to let the connection be cleaned up.
    sendResponse({});
};

// Listen for the content script to send a message to the background page.
chrome.extension.onRequest.addListener(onRequest);

var path = "nothing";

function dynamic () {
    var newPath = location.href;
    if (newPath !== path) {
        chrome.pageAction.show(sender.tab.id);
        path = newPath;
    }
}

setInterval(dynamic, 500);

0 个答案:

没有答案