export interface Message { type : string ; link ? : string ; code ? : string ; string ? : string ; } export interface ResponseMessage { type : string ; text ? : string ; } export function getCurrentTab ( ) { return new Promise ( ( resolve ) => { chrome . windows . getCurrent ( { populate : true , windowTypes : [ "normal" ] , } , function ( w ) { const t = w . tabs . find ( t => t . active ) ; resolve ( t ) ; } ) ; } ) ; } export function sendMessageToTab ( tabId , message ) { return new Promise ( function ( resolve , reject ) { chrome . tabs . sendMessage ( tabId , message , function ( res ) { if ( ! res && chrome . runtime . lastError ) { reject ( chrome . runtime . lastError ) ; } else { resolve ( res ) ; } } ) ; } ) ; } export function showInputDialog ( tabId ) { return sendMessageToTab ( tabId , { type : 'showInputDialog' } ) ; } export function getSelectionText ( tabId ) { return sendMessageToTab ( tabId , { type : 'selectedText' } ) ; } export function copyToClipboard ( document , text ) { let itemParams = { 'text/plain' : new Blob ( [ text ] , { type : "text/plain" } ) , } ; try { const div = document . createElement ( 'div' ) ; div . innerHTML = text ; const a = div . querySelector ( 'a' ) ; if ( a ) { itemParams [ 'text/html' ] = new Blob ( [ text ] , { type : "text/html" } ) ; } const items = [ new ClipboardItem ( itemParams ) ] ; return navigator . clipboard . write ( items ) . then ( ( ) => { console . log ( 'copied' , itemParams ) ; } , ( reason ) => { console . log ( 'Clipboard API failed' , reason ) ; copyToClipboardHTML ( document , text ) ; } ) ; } catch ( e ) { console . log ( "falling back to execCommand('copy')" , e ) ; copyToClipboardHTML ( document , text ) ; } } function copyToClipboardHTML ( document , text ) { const textarea = document . createElement ( 'textarea' ) ; textarea . setAttribute ( 'style' , `
        position: absolute;
        width: 0.1px;
        height: 0.1px;
        right: 200%;
        opacity: 0.1;
      ` ) ; textarea . setAttribute ( 'id' , 'clipboard_object' ) ; document . body . appendChild ( textarea ) ; textarea . appendChild ( document . createTextNode ( text ) ) ; textarea . select ( ) ; document . execCommand ( "copy" ) ; textarea . parentNode . removeChild ( textarea ) ; return text ; }