Safari 扩展,直接从工具栏访问页面/内容?

发布于 2024-12-06 00:50:44 字数 662 浏览 0 评论 0原文

是否可以直接从 (Safari) 工具栏访问网页内容?我现在可以从上下文菜单访问它,但不知道如何在工具栏上获得相同的功能。

这就是我得到的:

// injected
document.addEventListener("contextmenu", handleMessage, false);
function handleMessage(msgEvent) {
     var sel = '';
     sel = window.parent.getSelection()+'';
     safari.self.tab.setContextMenuEventUserInfo(msgEvent, sel);
}

// global
safari.application .addEventListener("command", performCommand, false); 
function performCommand(event) {
console.log('performCommand');
     if (event.command == "abc") {
          var query = event.userInfo;
          console.log(query);
          alert(query);
     }
}

但是我如何直接从工具栏获取此内容?

IS it possible to access web content directly from the (Safari) toolbar? I can now access it from a contextmenu, but no idea how i get the same functionaliy to a toolbar.

This is what i got:

// injected
document.addEventListener("contextmenu", handleMessage, false);
function handleMessage(msgEvent) {
     var sel = '';
     sel = window.parent.getSelection()+'';
     safari.self.tab.setContextMenuEventUserInfo(msgEvent, sel);
}

// global
safari.application .addEventListener("command", performCommand, false); 
function performCommand(event) {
console.log('performCommand');
     if (event.command == "abc") {
          var query = event.userInfo;
          console.log(query);
          alert(query);
     }
}

But how do i this content directly from the toolbar ??

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

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

发布评论

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

评论(2

说好的呢 2024-12-13 00:50:44

好的,基本上它的工作原理是这样的:

  • 在全局中,调用performCommand(通过单击工具栏),调度一个事件,
  • 该事件在handleGextText中的handleGextText中捕获
  • ,您执行您需要执行的操作并调用safari.self.tab.dispatchMessage这会将事件分派回全局,
  • 在全局中,事件被handleEvent

>捕获

// Global Script
safari.application.addEventListener("command", performCommand, false);  
safari.application.addEventListener("message", handleEvent, false);

// send message
function performCommand(event) {
    console.log('command:' + event.command);

    if (event.command == "abc") {
        console.log("msg: my message");
        safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("msg", "do-something");           
    }       
}

function handleEvent(event) {
    var messageName = event.name;
    console.log("evenname:" + event.name);      
    if (messageName === "did-something") { 

        var msg = event.message;

        // do something
    }       
}


// Injected Script
if (window.top === window) { // inject only once!
    console.log("add event listners [injected.js]");
    safari.self.addEventListener("message", handleGextText, false);
}

function handleGextText(event) {
    console.log("evenname:" + event.name);
    console.log("evenmsg :" + event.message);

    var messageName = event.name;
    var messageData = event.message;
    if (messageName === "msg") { 
        if (messageData === "do-something") {           
            console.log('msg received: ' + event.name);
            var sel = '';

            // do what you need to do and  dispatch message back to Global
            console.log("send message to toolbar");             
            safari.self.tab.dispatchMessage("did-something", sel);          
        }
     }
}

OK, basically it works like this:

  • in global the performCommand get called (by clicking on the toolbar), dispatching a event
  • this event is caught in handleGextText
  • in handleGextText, you do what you need to do and call safari.self.tab.dispatchMessage this dispatches a event back to global
  • in global you the event get caught by handleEvent

>

// Global Script
safari.application.addEventListener("command", performCommand, false);  
safari.application.addEventListener("message", handleEvent, false);

// send message
function performCommand(event) {
    console.log('command:' + event.command);

    if (event.command == "abc") {
        console.log("msg: my message");
        safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("msg", "do-something");           
    }       
}

function handleEvent(event) {
    var messageName = event.name;
    console.log("evenname:" + event.name);      
    if (messageName === "did-something") { 

        var msg = event.message;

        // do something
    }       
}


// Injected Script
if (window.top === window) { // inject only once!
    console.log("add event listners [injected.js]");
    safari.self.addEventListener("message", handleGextText, false);
}

function handleGextText(event) {
    console.log("evenname:" + event.name);
    console.log("evenmsg :" + event.message);

    var messageName = event.name;
    var messageData = event.message;
    if (messageName === "msg") { 
        if (messageData === "do-something") {           
            console.log('msg received: ' + event.name);
            var sel = '';

            // do what you need to do and  dispatch message back to Global
            console.log("send message to toolbar");             
            safari.self.tab.dispatchMessage("did-something", sel);          
        }
     }
}
稍尽春風 2024-12-13 00:50:44

好的,很好找到了。我用消息解决了。

我在“全局”中发送一条消息,该消息由注入的脚本捕获。该函数获取选定的文本(将其放入用户信息中),并将消息发送回全局。

就是这样。

OK, well found it. I solved it with messages.

I send in 'global' a message, which is catches by the injected script. That function get the selected text (puts it in userinfo), and sends a message back to global.

Thats it.

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