darktrojan / openwith

Open With add-on for Firefox
https://addons.mozilla.org/addon/open-with/
412 stars 71 forks source link

cannot find connection with external servers - can't run on Chromium Linux #239

Closed omega3 closed 4 years ago

omega3 commented 5 years ago

How to install to make it work.

I have python installed.

I did

chmod u+x open_with_linux.py
./open_with_linux.py install

and added as browser /usr/bin/mpv but it doesn't work Also with /usr/bin/vlc it doesn't work. What to do to make it work?

I use Chromium version 73.0.3683.86 on Manjaro. Open with version 7.2

By the way I use Chromium with firejail

firejail --private=/home/user/jail/ /usr/bin/chromium

Can this be a cause of the problem. I don't think so, because chromium itself runs in firejail and I run installation from path /home/user/jail/ so it act as a home folder. Installation script doesn't give any feedback / error message.

Chromium find errors shows

background.js:79 (error_listener)

        console.error(error, chrome.runtime.lastError);
background.js:79 (error_listener)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 
/* globals chrome, compare_versions, get_version_warn, ERROR_COLOUR, WARNING_COLOUR */ var browsers, icons, menu_contexts; var max_browser_id = 0; var max_icon_id = 0;  chrome.commands.onCommand.addListener(function(command) {  chrome.tabs.query({currentWindow: true, active: true}, tabs => {        let browser = browsers.find(b => b.shortcut == command);        if (browser) {          open_browser(browser.id, tabs[0].url);      }   }); });  function get_target_url(info) {    if (info.linkUrl) {         return info.linkUrl;    } else if (info.srcUrl) { // Is the target an img, audio, or video element?         return info.srcUrl;     } else if (info.frameUrl) { // Is the element an iframe?        return info.frameUrl;   } else if (info.selectionText && /^(?:https?|ftp):/i.test(info.selectionText)) { // Is there an URL selected?       return info.selectionText;  }   return info.pageUrl; }  function context_menu_clicked(info) {   let browser_id = parseInt(info.menuItemId.substring(8), 10);    let url = get_target_url(info);     if ('modifiers' in info && info.modifiers.includes('Ctrl')) {       url = null;     }   open_browser(browser_id, url); }  function open_browser(browser_id, url) {  function split_args(argString) {        let args = [];          let temp = '';      let inQuotes = false;       for (let c of argString) {          if (c == '"') {                 if (temp.endsWith('\\')) {                  temp = temp.substring(0, temp.length - 1) + c;              } else {                    inQuotes = !inQuotes;               }           } else if (c == ' ' && !inQuotes) {                 args.push(temp);                temp = '';          } else {                temp += c;          }       }       if (temp.length > 0) {          args.push(temp);        }       return args;    }   let browser = browsers.find(b => b.id == browser_id);   let command = split_args(browser.command);  let found = false;  for (let i = 0; i < command.length; i++) {      if (command[i].includes('%s')) {            command[i] = command[i].replace('%s', url ? url : '');          found = true;       }   }   if (url && !found) {        command.push(url);  }   function error_listener(error) {        console.error(error, chrome.runtime.lastError);     }   let port = chrome.runtime.connectNative('open_with');   port.onDisconnect.addListener(error_listener);  port.onMessage.addListener(function() {         port.onDisconnect.removeListener(error_listener);       port.disconnect();  });     port.postMessage(command); }  chrome.storage.local.get({    'browsers': [],     'icons': [],    'menu_contexts': null }, result => {    browsers = result.browsers;     icons = result.icons;   menu_contexts = result.menu_contexts;   sort_browsers();    make_menus();   max_icon_id = icons.reduce(function(previous, item) {       return Math.max(previous, item.id);     }, 0); });  function make_menus() {     chrome.contextMenus.removeAll();    if (menu_contexts === null) {       menu_contexts = ['page', 'link', 'image'];      if (navigator.userAgent.includes('Firefox')) {          menu_contexts.push('tab');      }   }   let real_menu_contexts = [];    if (menu_contexts.includes('page')) {       real_menu_contexts.push('page');        real_menu_contexts.push('frame');   }   if (menu_contexts.includes('link')) {       real_menu_contexts.push('link');        real_menu_contexts.push('selection');   }   if (menu_contexts.includes('image')) {      real_menu_contexts.push('image');       real_menu_contexts.push('video');       real_menu_contexts.push('audio');   }   if (menu_contexts.includes('tab')) {        real_menu_contexts.push('tab');     }   if (real_menu_contexts.length === 0) {      return;     }   for (let b of browsers) {       max_browser_id = Math.max(max_browser_id, b.id);        if (b.hidden) {             continue;       }       let item = {            id: 'browser_' + b.id,          title: b.name,          contexts: real_menu_contexts,           documentUrlPatterns: ['<all_urls>', 'file:///*'],           onclick: context_menu_clicked       };      if (navigator.userAgent.includes('Firefox')) {          if (b.icon && b.icon.startsWith('user_icon_')) {                let icon = icons.find(i => i.id == parseInt(b.icon.substring(10)));                 item.icons = {                  16: icon['16'],                     32: icon['32']              };          } else if (b.icon) {                item.icons = {                  16: 'icons/' + b.icon + '_16x16.png',                   32: 'icons/' + b.icon + '_32x32.png'                };          }       }       chrome.contextMenus.create(item);   } }  chrome.storage.onChanged.addListener(changes => {  if ('menu_contexts' in changes) {       menu_contexts = changes.menu_contexts.newValue;         make_menus();   } });  chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {   let {data} = message;   let browser;    switch (message.action) {   case 'open_browser':        open_browser(message.id, message.url);      return;     case 'get_browsers':        sendResponse(browsers);         return true;    case 'add_browser':         if (data.shortcut) {            let existing = browsers.find(b => b.shortcut == data.shortcut);             if (existing) {                 delete existing.shortcut;           }       }       data.id = ++max_browser_id;         browsers.push(data);        chrome.storage.local.set({browsers}, function() {           make_menus();           sendResponse(data.id);      });         return true;    case 'remove_browser':      let removed = false;        for (let i = 0; i < browsers.length; i++) {             let b = browsers[i];            if (b.id == message.id) {               browsers.splice(i, 1);              removed = true;                 break;          }       }       chrome.storage.local.set({browsers}, function() {           make_menus();           sendResponse(removed);      });         return true;    case 'update_browser':      // Update the existing object to keep any stray stuff.      browser = browsers.find(b => b.id == data.id);      if (browser) {          browser.name = data.name;           browser.command = data.command;             if (data.icon) {                browser.icon = data.icon;           } else {                delete browser.icon;            }           if (data.shortcut) {                if (browser.shortcut != data.shortcut) {                    let existing = browsers.find(b => b.shortcut == data.shortcut);                     if (existing) {                         delete existing.shortcut;                   }                   browser.shortcut = data.shortcut;               }           } else {                delete browser.shortcut;            }           chrome.storage.local.set({browsers}, function() {               make_menus();               sendResponse(true);             });         }       return true;    case 'hide_browser':        // Update the existing object to keep any stray stuff.      browser = browsers.find(b => b.id == message.id);       if (browser) {          if (message.hidden) {               browser.hidden = true;          } else {                delete browser.hidden;          }           chrome.storage.local.set({browsers}, function() {               make_menus();               sendResponse(true);             });         }       return true;    case 'order_browsers':      for (let b of browsers) {           b.order = message.order.indexOf(b.id);      }       sort_browsers();        chrome.storage.local.set({browsers}, function() {           make_menus();           sendResponse(true);         });         return true;    case 'get_icons':       sendResponse(icons);        return true;    case 'add_icon':        data.id = ++max_icon_id;        icons.push(data);       chrome.storage.local.set({icons}, function() {          sendResponse(data.id);      });         return true;    case 'remove_icon':         for (let i = 0; i < icons.length; i++) {            let b = icons[i];           if (b.id == message.id) {               icons.splice(i, 1);                 break;          }       }       chrome.storage.local.set({icons}, function() {          sendResponse();         });         return true;    } });  function sort_browsers() {   browsers.sort(function(a, b) {      if (isNaN(a.order)) {           return isNaN(b.order) ? 0 : 1;      }       return isNaN(b.order) ? -1 : a.order - b.order;     }); }  chrome.storage.local.get({'menu_contexts': null, 'version': -1}, function({menu_contexts, version: previousVersion}) {   chrome.management.getSelf(function({version: currentVersion}) {         if (previousVersion != currentVersion) { // This is an upgrade or downgrade.            let newPrefs = {'version': currentVersion};             if (previousVersion != -1 &&                    compare_versions(currentVersion, previousVersion) > 0 &&                    (currentVersion.includes('b') || parseFloat(currentVersion, 10) != parseFloat(previousVersion, 10))) {              newPrefs.versionLastUpdate = new Date().toJSON();           }           if (compare_versions(previousVersion, '7.1') <= 0 && Array.isArray(menu_contexts)) {                newPrefs.menu_contexts = menu_contexts;                 newPrefs.menu_contexts.push('image');           }           chrome.storage.local.set(newPrefs);         }       get_version_warn().then(function(version_warn) {            function error_listener() {                 if (previousVersion == -1) { // This is a new install.                  chrome.browserAction.setPopup({popup: 'installed.html'});                   return;                 }               chrome.browserAction.setBadgeText({text: '!'});                 chrome.browserAction.setBadgeBackgroundColor({color: ERROR_COLOUR});            }           let port = chrome.runtime.connectNative('open_with');           port.onDisconnect.addListener(error_listener);          port.onMessage.addListener(function(message) {              if (message) {                  if (compare_versions(message.version, version_warn) < 0) {                      chrome.browserAction.setBadgeText({text: '!'});                         chrome.browserAction.setBadgeBackgroundColor({color: WARNING_COLOUR});                  }               } else {                    error_listener();               }               port.onDisconnect.removeListener(error_listener);               port.disconnect();          });             port.postMessage('ping');       });     }); });  
omega3 commented 5 years ago

https://pastebin.com/fnep5pKA


340
341
342
343
/* globals chrome, compare_versions, get_version_warn, ERROR_COLOUR, WARNING_COLOUR */
var browsers, icons, menu_contexts;
var max_browser_id = 0;
var max_icon_id = 0;

chrome.commands.onCommand.addListener(function(command) {
    chrome.tabs.query({currentWindow: true, active: true}, tabs => {
        let browser = browsers.find(b => b.shortcut == command);
        if (browser) {
            open_browser(browser.id, tabs[0].url);
        }
    });
});

function get_target_url(info) {
    if (info.linkUrl) {
        return info.linkUrl;
    } else if (info.srcUrl) { // Is the target an img, audio, or video element?
        return info.srcUrl;
    } else if (info.frameUrl) { // Is the element an iframe?
        return info.frameUrl;
    } else if (info.selectionText && /^(?:https?|ftp):/i.test(info.selectionText)) { // Is there an URL selected?
        return info.selectionText;
    }
    return info.pageUrl;
}

function context_menu_clicked(info) {
    let browser_id = parseInt(info.menuItemId.substring(8), 10);
    let url = get_target_url(info);
    if ('modifiers' in info && info.modifiers.includes('Ctrl')) {
        url = null;
    }
    open_browser(browser_id, url);
}

function open_browser(browser_id, url) {
    function split_args(argString) {
        let args = [];

        let temp = '';
        let inQuotes = false;
        for (let c of argString) {
            if (c == '"') {
                if (temp.endsWith('\\')) {
                    temp = temp.substring(0, temp.length - 1) + c;
                } else {
                    inQuotes = !inQuotes;
                }
            } else if (c == ' ' && !inQuotes) {
                args.push(temp);
                temp = '';
            } else {
                temp += c;
            }
        }

        if (temp.length > 0) {
            args.push(temp);
        }

        return args;
    }

    let browser = browsers.find(b => b.id == browser_id);
    let command = split_args(browser.command);
    let found = false;
    for (let i = 0; i < command.length; i++) {
        if (command[i].includes('%s')) {
            command[i] = command[i].replace('%s', url ? url : '');
            found = true;
        }
    }
    if (url && !found) {
        command.push(url);
    }

    function error_listener(error) {
        console.error(error, chrome.runtime.lastError);
    }
    let port = chrome.runtime.connectNative('open_with');
    port.onDisconnect.addListener(error_listener);
    port.onMessage.addListener(function() {
        port.onDisconnect.removeListener(error_listener);
        port.disconnect();
    });
    port.postMessage(command);
}

chrome.storage.local.get({
    'browsers': [],
    'icons': [],
    'menu_contexts': null
}, result => {
    browsers = result.browsers;
    icons = result.icons;
    menu_contexts = result.menu_contexts;
    sort_browsers();
    make_menus();
    max_icon_id = icons.reduce(function(previous, item) {
        return Math.max(previous, item.id);
    }, 0);
});

function make_menus() {
    chrome.contextMenus.removeAll();
    if (menu_contexts === null) {
        menu_contexts = ['page', 'link', 'image'];
        if (navigator.userAgent.includes('Firefox')) {
            menu_contexts.push('tab');
        }
    }

    let real_menu_contexts = [];
    if (menu_contexts.includes('page')) {
        real_menu_contexts.push('page');
        real_menu_contexts.push('frame');
    }
    if (menu_contexts.includes('link')) {
        real_menu_contexts.push('link');
        real_menu_contexts.push('selection');
    }
    if (menu_contexts.includes('image')) {
        real_menu_contexts.push('image');
        real_menu_contexts.push('video');
        real_menu_contexts.push('audio');
    }
    if (menu_contexts.includes('tab')) {
        real_menu_contexts.push('tab');
    }
    if (real_menu_contexts.length === 0) {
        return;
    }

    for (let b of browsers) {
        max_browser_id = Math.max(max_browser_id, b.id);
        if (b.hidden) {
            continue;
        }
        let item = {
            id: 'browser_' + b.id,
            title: b.name,
            contexts: real_menu_contexts,
            documentUrlPatterns: ['<all_urls>', 'file:///*'],
            onclick: context_menu_clicked
        };
        if (navigator.userAgent.includes('Firefox')) {
            if (b.icon && b.icon.startsWith('user_icon_')) {
                let icon = icons.find(i => i.id == parseInt(b.icon.substring(10)));
                item.icons = {
                    16: icon['16'],
                    32: icon['32']
                };
            } else if (b.icon) {
                item.icons = {
                    16: 'icons/' + b.icon + '_16x16.png',
                    32: 'icons/' + b.icon + '_32x32.png'
                };
            }
        }
        chrome.contextMenus.create(item);
    }
}

chrome.storage.onChanged.addListener(changes => {
    if ('menu_contexts' in changes) {
        menu_contexts = changes.menu_contexts.newValue;
        make_menus();
    }
});

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    let {data} = message;
    let browser;
    switch (message.action) {
    case 'open_browser':
        open_browser(message.id, message.url);
        return;
    case 'get_browsers':
        sendResponse(browsers);
        return true;
    case 'add_browser':
        if (data.shortcut) {
            let existing = browsers.find(b => b.shortcut == data.shortcut);
            if (existing) {
                delete existing.shortcut;
            }
        }

        data.id = ++max_browser_id;
        browsers.push(data);
        chrome.storage.local.set({browsers}, function() {
            make_menus();
            sendResponse(data.id);
        });
        return true;
    case 'remove_browser':
        let removed = false;
        for (let i = 0; i < browsers.length; i++) {
            let b = browsers[i];
            if (b.id == message.id) {
                browsers.splice(i, 1);
                removed = true;
                break;
            }
        }
        chrome.storage.local.set({browsers}, function() {
            make_menus();
            sendResponse(removed);
        });
        return true;
    case 'update_browser':
        // Update the existing object to keep any stray stuff.
        browser = browsers.find(b => b.id == data.id);
        if (browser) {
            browser.name = data.name;
            browser.command = data.command;
            if (data.icon) {
                browser.icon = data.icon;
            } else {
                delete browser.icon;
            }
            if (data.shortcut) {
                if (browser.shortcut != data.shortcut) {
                    let existing = browsers.find(b => b.shortcut == data.shortcut);
                    if (existing) {
                        delete existing.shortcut;
                    }
                    browser.shortcut = data.shortcut;
                }
            } else {
                delete browser.shortcut;
            }
            chrome.storage.local.set({browsers}, function() {
                make_menus();
                sendResponse(true);
            });
        }
        return true;
    case 'hide_browser':
        // Update the existing object to keep any stray stuff.
        browser = browsers.find(b => b.id == message.id);
        if (browser) {
            if (message.hidden) {
                browser.hidden = true;
            } else {
                delete browser.hidden;
            }
            chrome.storage.local.set({browsers}, function() {
                make_menus();
                sendResponse(true);
            });
        }
        return true;
    case 'order_browsers':
        for (let b of browsers) {
            b.order = message.order.indexOf(b.id);
        }
        sort_browsers();
        chrome.storage.local.set({browsers}, function() {
            make_menus();
            sendResponse(true);
        });
        return true;
    case 'get_icons':
        sendResponse(icons);
        return true;
    case 'add_icon':
        data.id = ++max_icon_id;
        icons.push(data);
        chrome.storage.local.set({icons}, function() {
            sendResponse(data.id);
        });
        return true;
    case 'remove_icon':
        for (let i = 0; i < icons.length; i++) {
            let b = icons[i];
            if (b.id == message.id) {
                icons.splice(i, 1);
                break;
            }
        }
        chrome.storage.local.set({icons}, function() {
            sendResponse();
        });
        return true;
    }
});

function sort_browsers() {
    browsers.sort(function(a, b) {
        if (isNaN(a.order)) {
            return isNaN(b.order) ? 0 : 1;
        }
        return isNaN(b.order) ? -1 : a.order - b.order;
    });
}

chrome.storage.local.get({'menu_contexts': null, 'version': -1}, function({menu_contexts, version: previousVersion}) {
    chrome.management.getSelf(function({version: currentVersion}) {
        if (previousVersion != currentVersion) { // This is an upgrade or downgrade.
            let newPrefs = {'version': currentVersion};
            if (previousVersion != -1 &&
                    compare_versions(currentVersion, previousVersion) > 0 &&
                    (currentVersion.includes('b') || parseFloat(currentVersion, 10) != parseFloat(previousVersion, 10))) {
                newPrefs.versionLastUpdate = new Date().toJSON();
            }
            if (compare_versions(previousVersion, '7.1') <= 0 && Array.isArray(menu_contexts)) {
                newPrefs.menu_contexts = menu_contexts;
                newPrefs.menu_contexts.push('image');
            }
            chrome.storage.local.set(newPrefs);
        }

        get_version_warn().then(function(version_warn) {
            function error_listener() {
                if (previousVersion == -1) { // This is a new install.
                    chrome.browserAction.setPopup({popup: 'installed.html'});
                    return;
                }
                chrome.browserAction.setBadgeText({text: '!'});
                chrome.browserAction.setBadgeBackgroundColor({color: ERROR_COLOUR});
            }

            let port = chrome.runtime.connectNative('open_with');
            port.onDisconnect.addListener(error_listener);
            port.onMessage.addListener(function(message) {
                if (message) {
                    if (compare_versions(message.version, version_warn) < 0) {
                        chrome.browserAction.setBadgeText({text: '!'});
                        chrome.browserAction.setBadgeBackgroundColor({color: WARNING_COLOUR});
                    }
                } else {
                    error_listener();
                }
                port.onDisconnect.removeListener(error_listener);
                port.disconnect();
            });
            port.postMessage('ping');
        });
    });
});