简体   繁体   English

页面重定向后如何调用Chrome扩展功能?

[英]How To Call Chrome Extension Function After Page Redirect?

I am working on building a Javascript (in-browser) Instagram bot.我正在构建一个 Javascript(浏览器内)Instagram 机器人。 However, I ran into a problem.但是,我遇到了一个问题。

If you run this script, the first function will be called and the page will be redirected to " https://www.instagram.com/explore/tags/samplehashtag/ " and the second function will be called immediately after (on the previous URL before the page changes to the new URL).如果你运行这个脚本,第一个函数将被调用,页面将被重定向到“ https://www.instagram.com/explore/tags/samplehashtag/ ”,第二个函数将在之后立即调用(在上一个页面更改为新 URL 之前的 URL)。 Is there a way to make the second function be called after this second URL has been loaded completely?有没有办法在第二个 URL 完全加载后调用第二个函数?

I have tried setting it to a Window setInterval() Method for an extended time period, window.onload and a couple of other methods.我曾尝试将其设置为 Window setInterval() 方法延长时间,window.onload 和其他几种方法。 However, I can't seem to get anything to work.但是,我似乎无法做任何事情。 Any chance someone has a solution?有人有解决方案的机会吗?

This is my first chrome extension and my first real project, so I may be missing something simple..这是我的第一个 chrome 扩展,也是我的第一个真正的项目,所以我可能会遗漏一些简单的东西..

manifest.json清单文件

    {
  "name": "Inject Me",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Injecting stuff",
  "homepage_url": "http://danharper.me",
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": true
  },
  "browser_action": {
    "default_title": "Inject!"
  },
  "permissions": [
    "https://*/*",
    "http://*/*",
    "tabs"
  ]
}

inject.js注入.js

  (function() {

  let findUrl = () => {
    let hashtag = "explore/tags/samplehashtag/";
    location.replace("https://www.instagram.com/" + hashtag);
  }

  findUrl();
})();

background.js背景.js

    // this is the background code...

// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function(tab) {
  // for the current tab, inject the "inject.js" file & execute it
  chrome.tabs.executeScript(tab.ib, {
    file: 'inject.js'
  });

});

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {

  chrome.tabs.executeScript(tab.ib, {
    file: 'inject2.js'
  });


});

inject2.js注入2.js

(function() {

if (window.location.href.indexOf("https://www.instagram.com/explore/tags/samplehashtag/") != -1){
  let likeAndRepeat = () => {
    let counter = 0;
    let grabPhoto = document.querySelector('._9AhH0');

    grabPhoto.click();

    let likeAndSkip = function() {
      let heart = document.querySelector('.glyphsSpriteHeart__outline__24__grey_9.u-__7');
      let arrow = document.querySelector('a.coreSpriteRightPaginationArrow');

      if (heart) {
        heart.click();
        counter++;
        console.log(`You have liked ${counter} photographs`)
      }
      arrow.click();
    }

    setInterval(likeAndSkip, 3000);
    //alert('likeAndRepeat Inserted');
  };

  likeAndRepeat();

}
})();

It is not clear from the question and the example, when you want to run your function.从问题和示例中不清楚何时要运行函数。 But in chrome extension there is something called Message Passing但是在 chrome 扩展中有一种叫做消息传递的东西

https://developer.chrome.com/extensions/messaging https://developer.chrome.com/extensions/messaging

With message passing you can pass messages from one file to another, and similarly listen for messages.通过消息传递,您可以将消息从一个文件传递到另一个文件,并类似地侦听消息。 So as it looks from your use case, you can listen for a particular message and then fire your method.因此,从您的用例来看,您可以侦听特定消息,然后触发您的方法。

For example例如

background.js

chrome.runtime.sendMessage({message: "FIRE_SOME_METHOD"})


popup.js

    chrome.runtime.onMessage.addListener(
    function(request) {

    if (request.message == "FIRE_SOME_METHOD")
      someMethod();
  });

EDIT Also if you want to listen for the URL changes, you can simply put a listener provided as in the documentation .编辑此外,如果您想监听 URL 更改,您可以简单地放置一个在文档中提供的监听器。

  chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
      console.log('updated tab');
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM