javascript - Safari Extension, accessing pagecontent directly from toolbar? - Stack Overflow

IS it possible to access web content directly from the (Safari) toolbar? I can now access it from a con

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("mand", performCommand, false); 
function performCommand(event) {
console.log('performCommand');
     if (eventmand == "abc") {
          var query = event.userInfo;
          console.log(query);
          alert(query);
     }
}

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

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("mand", performCommand, false); 
function performCommand(event) {
console.log('performCommand');
     if (event.mand == "abc") {
          var query = event.userInfo;
          console.log(query);
          alert(query);
     }
}

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

Share Improve this question asked Sep 23, 2011 at 7:41 RogerRoger 7,6405 gold badges44 silver badges65 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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("mand", performCommand, false);  
safari.application.addEventListener("message", handleEvent, false);

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

    if (event.mand == "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, 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.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信