Запустите Javascript-Jquery в фоновом режиме и не открывайте окно - PullRequest
0 голосов
/ 30 октября 2019

Я использую код ниже, чтобы позвонить по номеру, нажав на гиперссылку в браузере 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);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...