让 Pagemod 和 Page-worker 模块一起工作?

发布于 2024-10-20 19:20:26 字数 2155 浏览 1 评论 0原文

我正在开发一个 Firefox 扩展项目,该项目要求我使用 TAB 键切换页面上的所有链接、提取其 URL、获取其源代码并解析它。

目前,我可以使用 TAB 键切换页面以通过以下方式获取其 URL:

exports.main = function() {};
var pageMod = require("page-mod");

pageMod.PageMod({
  include: "*.org",
  contentScriptWhen: 'ready',
  contentScript:

  'document.onkeydown = function(ev) {var key; ev = ev || event; key = ev.keyCode; if (key == 9) { setTimeout (function(){console.log(document.activeElement.href);'+
  '}, 1000 ); } };'+

  'var a = "Access Tab URL Extraction: New Page Found."; console.log(a);'
});

... 并使用以下命令获取任何给定 URL 的源代码(例如维基百科):

exports.main = function() {};

console.log("Start");
var pageWorkers = require("page-worker");

var script = "var elements = document.body.innerHTML; " +

             "  postMessage(elements) " ;

// Create a page worker that loads Wikipedia:
pageWorkers.Page({
  contentURL: "http://en.wikipedia.org/wiki/Internet",
  contentScript: script,
  contentScriptWhen: "ready",
  onMessage: function(message) {
    console.log(message);
  }
});

那么,我怎样才能将从Pagemod(在本例中为document.activeElement.href)获得的URL传递到Page-worker的contentURL中,并使其在每次我按TAB键时获取并显示源代码,因为新的URL不断在Pagemod中提取。

我尝试了以下方法,但它不起作用,似乎无法调用 page-worker 作为 page-mod 的 contentScript 的一部分?

exports.main = function() {};
var pageMod = require("page-mod");
var pageWorkers = require("page-worker");


pageMod.PageMod({
  include: "*.org",
  contentScriptWhen: 'ready',
  contentScript:

//Get New URLs by Pressing the Tab Key

'document.onkeydown = function(ev) { var key; ev = ev || event; key = ev.keyCode; if (key == 9) { setTimeout (function(){console.log(document.activeElement.href);'+

  '}, 1000 ); } };'+



//Fetch new URL source code

'document.onkeyup = function(ev) { var key; ev = ev || event; key = ev.keyCode; if (key == 9) {'+

'pageWorkers.Page({'+
'contentURL: "document.activeElement.href",'+
'contentScript: "var elements = document.body.innerHTML;"+"postMessage(elements)",'+
'contentScriptWhen: "ready",'+
'onMessage: function(message) {'+
'console.log(message);'+
'}'+
'});'+

   ' } };'

});

任何帮助将不胜感激! :)

I'm working on a Firefox extension project that requires me to use the TAB key to toggle through all the links on the page, extract the its URL, fetch its source code, and parse it.

Currently, I'm able to use TAB key to toggle through the page to get its URL by the following:

exports.main = function() {};
var pageMod = require("page-mod");

pageMod.PageMod({
  include: "*.org",
  contentScriptWhen: 'ready',
  contentScript:

  'document.onkeydown = function(ev) {var key; ev = ev || event; key = ev.keyCode; if (key == 9) { setTimeout (function(){console.log(document.activeElement.href);'+
  '}, 1000 ); } };'+

  'var a = "Access Tab URL Extraction: New Page Found."; console.log(a);'
});

... and get fetch the source code of any give URL with the following (Wikipedia for example):

exports.main = function() {};

console.log("Start");
var pageWorkers = require("page-worker");

var script = "var elements = document.body.innerHTML; " +

             "  postMessage(elements) " ;

// Create a page worker that loads Wikipedia:
pageWorkers.Page({
  contentURL: "http://en.wikipedia.org/wiki/Internet",
  contentScript: script,
  contentScriptWhen: "ready",
  onMessage: function(message) {
    console.log(message);
  }
});

So, how can I pass in the URL I got from the Pagemod (in this case document.activeElement.href) into Page-worker's contentURL and make it fetch and display the source code everytime I press the TAB key as new URLs keep being extracted in Pagemod.

I tried the following but it does not work, seems one cannot call page-worker as part of the page-mod's contentScript?

exports.main = function() {};
var pageMod = require("page-mod");
var pageWorkers = require("page-worker");


pageMod.PageMod({
  include: "*.org",
  contentScriptWhen: 'ready',
  contentScript:

//Get New URLs by Pressing the Tab Key

'document.onkeydown = function(ev) { var key; ev = ev || event; key = ev.keyCode; if (key == 9) { setTimeout (function(){console.log(document.activeElement.href);'+

  '}, 1000 ); } };'+



//Fetch new URL source code

'document.onkeyup = function(ev) { var key; ev = ev || event; key = ev.keyCode; if (key == 9) {'+

'pageWorkers.Page({'+
'contentURL: "document.activeElement.href",'+
'contentScript: "var elements = document.body.innerHTML;"+"postMessage(elements)",'+
'contentScriptWhen: "ready",'+
'onMessage: function(message) {'+
'console.log(message);'+
'}'+
'});'+

   ' } };'

});

Any help will be greatly appreciated! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2024-10-27 19:20:26

内容脚本无法使用 addon-sdk 资源,因为它们在网页上下文中运行。所以你必须使用消息传递。您的按键处理程序应该将一条消息传递回插件,其中包含类似的内容

  self.port.emit('url', document.activeElement.href)

,然后在您的 page-mod 声明中,您将收到该消息并创建一个 page-worker 来获取源代码:

onAttach: function(worker) {
  worker.port.on('url', function(url) {
    pageWorkers.Page({
      contentURL: url,
      contentScript: script,
      contentScriptWhen: "ready",
      onMessage: function(message) {
        console.log(message);
      }
    });
  });
}

请注意,您可能需要一些验证,以便您仅将 URL 传递回插件,并且您可能希望处理在页面工作人员中加载页面的错误。

Content scripts can't use addon-sdk resources, as they run in the context of the webpage. So you have to use message passing. Your keypress handler should pass a message back to the addon with something like

  self.port.emit('url', document.activeElement.href)

and then in your page-mod declaration you'd receive the message and create a page-worker to get the source:

onAttach: function(worker) {
  worker.port.on('url', function(url) {
    pageWorkers.Page({
      contentURL: url,
      contentScript: script,
      contentScriptWhen: "ready",
      onMessage: function(message) {
        console.log(message);
      }
    });
  });
}

Note that you'd probably want some validation so you only pass URLs back to the addon, and you'd probably want to deal with errors loading the page in the page-worker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文