javascript - chrome extension : Stop loading the page on launch - Stack Overflow

I would like to stop the page loading when I click the extension icon. I have a background page , I nee

I would like to stop the page loading when I click the extension icon. I have a background page , I need an option similar to window.stop() when I click the extension Icon. If the main page is still in loading condition stop loading and load the content JavaScript of extension should work.

I would like to stop the page loading when I click the extension icon. I have a background page , I need an option similar to window.stop() when I click the extension Icon. If the main page is still in loading condition stop loading and load the content JavaScript of extension should work.

Share Improve this question asked Sep 30, 2014 at 10:55 abhilashabhilash 86512 silver badges21 bronze badges 1
  • If Xan's solution does not work, try chrome.tabs.update(tab.id, {url:'javascript:void window.stop();'}) . – Rob W Commented Sep 30, 2014 at 15:59
Add a ment  | 

2 Answers 2

Reset to default 4

You can always do this (requires host permissions or activeTab):

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id, {
        code: "window.stop();",
        runAt: "document_start"
    });
});

I am not sure what happens with manifest-defined content scripts. It's possible that scripts defined with run_at other than document_start will not be injected; in this case you can use executeScript's callback to inject them in any case, and put in some guard code in the content script to prevent it from executing twice:

// content.js
if(contentInjected) return;
var contentInjected = true;

/* rest of the code */

Since manifest version 3 you need to use scripting. This requires permissions ["scripting", "tabs"] and the host_permissions must be allowed for the current URL.

function stopLoading(tab) {
    console.debug('Stopping loading of tab ' + tab.id);
    chrome.scripting.executeScript({
        target: {tabId: tab.id},
        func: function() {
            window.stop();
        }
    });
}

chrome.action.onClicked.addListener(() => {
    chrome.tabs.query({ active: true, currentWindow: true })
        .then(tabs => stopLoading(tabs[0]));
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745065150a4609210.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信